Search in sources :

Example 1 with UserInfo

use of org.liberty.android.fantastischmemo.downloader.dropbox.entity.UserInfo in project AnyMemo by helloworld1.

the class DropboxApiHelper method getUserInfo.

public Single<UserInfo> getUserInfo(@NonNull final String token) {
    return Single.create(new SingleOnSubscribe<UserInfo>() {

        @Override
        public void subscribe(@NonNull final SingleEmitter<UserInfo> emitter) throws Exception {
            RequestBody requestBody = RequestBody.create(null, new byte[0]);
            Request request = new Request.Builder().url(USER_INFO_ENDPOINT).addHeader("Authorization", "Bearer " + token).post(requestBody).build();
            okHttpClient.newCall(request).enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    emitter.onError(e);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (!response.isSuccessful()) {
                        emitter.onError(new IOException(getResponseErrorString(call.request(), response)));
                        return;
                    }
                    UserInfo userInfo = new UserInfo();
                    try {
                        JSONObject userInfoObject = new JSONObject(response.body().string());
                        userInfo.accountId = userInfoObject.getString("account_id");
                        userInfo.email = userInfoObject.getString("email");
                        JSONObject nameObject = userInfoObject.getJSONObject("name");
                        userInfo.displayName = nameObject.getString("display_name");
                        emitter.onSuccess(userInfo);
                    } catch (JSONException e) {
                        emitter.onError(e);
                    }
                }
            });
        }
    });
}
Also used : Call(okhttp3.Call) Request(okhttp3.Request) JSONException(org.json.JSONException) UserInfo(org.liberty.android.fantastischmemo.downloader.dropbox.entity.UserInfo) IOException(java.io.IOException) JSONException(org.json.JSONException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Response(okhttp3.Response) Callback(okhttp3.Callback) JSONObject(org.json.JSONObject) RequestBody(okhttp3.RequestBody)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 UserInfo (org.liberty.android.fantastischmemo.downloader.dropbox.entity.UserInfo)1