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);
}
}
});
}
});
}
Aggregations