Back to project page etalio-android-sdk.
The source code is released under:
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions...
If you think the Android project etalio-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2014 Ericsson AB/* w ww. j av a2s . c om*/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.etalio.android; import android.content.Context; import android.content.SharedPreferences; import com.etalio.android.client.HttpRequest; import com.etalio.android.client.exception.EtalioHttpException; import com.etalio.android.client.exception.EtalioTokenException; import com.etalio.android.client.models.Status; import com.etalio.android.client.models.User; import com.etalio.android.client.models.UserUpdate; import java.io.IOException; import java.io.InputStream; import java.text.ParseException; /** * Etalio API requests wrapper class. */ public class EtalioAPI extends EtalioBase { //Api url protected static final String API_VERSION = "v1"; //Uri map keys protected static final String MYPROFILE = "myprofile"; protected static final String ADD_PROFILE_IMAGE = "addProfileImage"; protected static final String STATUS = "status"; protected static final String PROFILE = "profile"; private final SharedPreferences store; public EtalioAPI(Context context, String clientID, String clientSecret) { this(context, clientID, clientSecret, API_URL, API_VERSION); } public EtalioAPI(Context context, String clientID, String clientSecret, String apiUrl, String apiVersion) { super(clientID, clientSecret, context, apiUrl); String baseUrl = apiUrl + apiVersion; domainMap.put(MYPROFILE, baseUrl + "/profile/me"); domainMap.put(PROFILE, baseUrl + "/profile"); domainMap.put(ADD_PROFILE_IMAGE, baseUrl + "/profile/me/image"); domainMap.put(STATUS, baseUrl + "/status"); store = context.getSharedPreferences(clientID, Context.MODE_PRIVATE); } /** * Get profile for the currently signed in user. * @return * @throws IOException * @throws EtalioTokenException * @throws EtalioHttpException */ public User getCurrentProfile() throws IOException, EtalioTokenException, EtalioHttpException { return apiCall(MYPROFILE, HttpRequest.HttpMethod.GET, null, User.class); } public String updateProfileImage(String fileName, InputStream stream) throws IOException, ParseException, EtalioHttpException { return multipartRequest(getUrl(ADD_PROFILE_IMAGE,null), stream, "image", fileName); } /** * Get profile for a specific a user id. * @param id The unique id for the user * @return * @throws IOException * @throws EtalioTokenException * @throws EtalioHttpException */ public User getProfile(String id) throws IOException, EtalioTokenException, EtalioHttpException { if(id == null) { throw new IllegalArgumentException("User id can not be null"); } String domain = addToDomainMap("profile-"+id,domainMap.get(PROFILE)+"/"+id); return apiCall(domain, HttpRequest.HttpMethod.GET, null, User.class); } public User updateProfile(User user, String oldPassword, String newPassword) throws EtalioTokenException, IOException, EtalioHttpException { if(user == null || user.getId() == null) { throw new IllegalArgumentException("User or user id can not be null"); } String domain = addToDomainMap("profile-" + user.getId(),domainMap.get(PROFILE)+"/"+user.getId()); UserUpdate updatedUser = new UserUpdate(user.getOperator(), user.getEmail(), user.getName()); updatedUser.setPassword(oldPassword,newPassword); return apiCall(domain, HttpRequest.HttpMethod.PUT, updatedUser, User.class); } public User updateProfile(User user) throws IOException, EtalioTokenException, EtalioHttpException { if(user == null || user.getId() == null) { throw new IllegalArgumentException("User or user id can not be null"); } String domain = addToDomainMap("profile-" + user.getId(),domainMap.get(PROFILE)+"/"+user.getId()); UserUpdate updatedUser = new UserUpdate(user.getOperator(), user.getEmail(), user.getName()); return apiCall(domain, HttpRequest.HttpMethod.PUT, updatedUser, User.class); } public User updateProfileSecurity(User user) throws EtalioTokenException, IOException, EtalioHttpException { if(user == null || user.getId() == null) { throw new IllegalArgumentException("User or user id can not be null"); } String domain = addToDomainMap("profile-" + user.getId(),domainMap.get(PROFILE)+"/"+user.getId()); UserUpdate updatedUser = new UserUpdate(); updatedUser.setTwoFactor(user.get2FactorSecurity()); return apiCall(domain, HttpRequest.HttpMethod.PUT, updatedUser, User.class); } public User updateProfileMsisdn(User user, String msisdn, String requestCode) throws EtalioTokenException, IOException, EtalioHttpException{ if(user == null || user.getId() == null) { throw new IllegalArgumentException("User or user id can not be null"); } String domain = addToDomainMap("profile-" + user.getId(),domainMap.get(PROFILE)+"/"+user.getId()); UserUpdate updatedUser = new UserUpdate(); updatedUser.setMsisdnAndCode(msisdn, requestCode); return apiCall(domain, HttpRequest.HttpMethod.PUT, updatedUser, User.class); } public Status getApiStatus() throws EtalioTokenException, IOException, EtalioHttpException { return apiCall(STATUS, HttpRequest.HttpMethod.GET, null, Status.class); } /** * Request to check if a valid session is active on the server. * @return true if there is an authenticated user on the server. false if session never existed, is outdated or revoked. */ public boolean isAuthenticated() { try { User user = getCurrentProfile(); return user != null && !Utils.isNullOrEmpty(user.getId()); } catch (IOException e) { return false; } catch (EtalioTokenException e) { return false; } catch (EtalioHttpException e) { return false; } } @Override public void setPersistentData(SupportedKey key, String val) { store.edit().putString(key.name(),val).commit(); } @Override public String getPersistentData(SupportedKey key) { return store.getString(key.name(),null); } @Override public void clearPersistentData(SupportedKey key) { store.edit().remove(key.name()).commit(); } @Override public void clearAllPersistentData() { SharedPreferences.Editor edit = store.edit(); for(SupportedKey key : SupportedKey.class.getEnumConstants()) { edit.remove(key.name()); } edit.commit(); } protected String addToDomainMap(String key, String val) { domainMap.put(key,val); return key; } }