Skip to content

Instantly share code, notes, and snippets.

@enginebai
Last active November 18, 2016 03:56
Show Gist options
  • Save enginebai/ad01b4064f50413c89b6749f0057a520 to your computer and use it in GitHub Desktop.
Save enginebai/ad01b4064f50413c89b6749f0057a520 to your computer and use it in GitHub Desktop.
public static Observable<String> getUserInfo(final Context context,
final String userId) {
return Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> subscriber) throws Exception {
try {
HttpUrl url = new HttpUrl.Builder()
.scheme(SCHEME_HTTPS)
.host(API_HOST)
.addPathSegment(API_VERSION).addPathSegment(MODULE_NAME).addPathSegment(PATH_GET_USER_INFO)
.addQueryParameter(User.FIELD_USER_ID, userId)
.build();
subscriber.onNext(ApiService.get(context, url));
}
catch (Exception e) {
subscriber.onError(e);
}
}
});
}
public static String get(Context context, HttpUrl url) throws IOException {
Request request = ApiService.createRequestBuilder(context)
.url(url)
.build();
return ((GlobalContext) context.getApplicationContext())
.getHttpClientDebug(HttpLoggingInterceptor.Level.BODY)
.newCall(request)
.execute().body().string();
}
public OkHttpClient getHttpClientDebug(HttpLoggingInterceptor.Level debugLevel) {
if (isDebugMode())
return new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(debugLevel))
.addNetworkInterceptor(new StethoInterceptor())
.build();
else
return getHttpClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment