use of retrofit2.http.Header in project Retrofit-Android-Basic by basil2style.
the class ServiceGenerator method createService.
public static <S> S createService(Class<S> serviceClass, final String authToken) {
if (authToken != null) {
httpClient.interceptors().add(new Interceptor() {
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder().header("Authorization", authToken).method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
}
OkHttpClient client = httpClient.build();
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);
}
use of retrofit2.http.Header in project Varis-Android by dkhmelenko.
the class BuildsDetailsPresenter method startLoadingLog.
/**
* Starts loading log file
*
* @param jobId Job ID
*/
public void startLoadingLog(long jobId) {
mJobId = jobId;
String accessToken = AppSettings.getAccessToken();
Single<String> responseSingle;
if (TextUtils.isEmpty(accessToken)) {
responseSingle = mRawClient.getApiService().getLog(String.valueOf(mJobId));
} else {
String auth = String.format("token %1$s", AppSettings.getAccessToken());
responseSingle = mRawClient.getApiService().getLog(auth, String.valueOf(mJobId));
}
Disposable subscription = responseSingle.subscribeOn(Schedulers.io()).map(s -> mRawClient.getLogUrl(mJobId)).onErrorResumeNext(new Function<Throwable, SingleSource<String>>() {
@Override
public SingleSource<String> apply(@NonNull Throwable throwable) throws Exception {
String redirectUrl = "";
HttpException httpException = (HttpException) throwable;
Headers headers = httpException.response().headers();
for (String header : headers.names()) {
if (header.equals("Location")) {
redirectUrl = headers.get(header);
break;
}
}
return Single.just(redirectUrl);
}
}).retry(LOAD_LOG_MAX_ATTEMPT).observeOn(AndroidSchedulers.mainThread()).subscribe((logUrl, throwable) -> {
if (throwable == null) {
getView().setLogUrl(logUrl);
} else {
getView().showLogError();
getView().showLoadingError(throwable.getMessage());
}
});
mSubscriptions.add(subscription);
}
use of retrofit2.http.Header in project retrofit by square.
the class RequestBuilderTest method headerParamList.
@Test
public void headerParamList() {
class Example {
//
@GET("/foo/bar/")
Call<ResponseBody> method(@Header("foo") List<String> kit) {
return null;
}
}
Request request = buildRequest(Example.class, Arrays.asList("bar", null, "baz"));
assertThat(request.method()).isEqualTo("GET");
okhttp3.Headers headers = request.headers();
assertThat(headers.size()).isEqualTo(2);
assertThat(headers.values("foo")).containsExactly("bar", "baz");
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
use of retrofit2.http.Header in project retrofit by square.
the class RequestBuilderTest method contentTypeParameterHeaderOverrides.
@Test
public void contentTypeParameterHeaderOverrides() {
class Example {
//
@POST("/")
Call<ResponseBody> method(@Header("Content-Type") String contentType, @Body RequestBody body) {
return null;
}
}
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "Plain");
Request request = buildRequest(Example.class, "text/not-plain", body);
assertThat(request.body().contentType().toString()).isEqualTo("text/not-plain");
}
use of retrofit2.http.Header in project retrofit by square.
the class RequestBuilderTest method headerParamArray.
@Test
public void headerParamArray() {
class Example {
//
@GET("/foo/bar/")
Call<ResponseBody> method(@Header("foo") String[] kit) {
return null;
}
}
Request request = buildRequest(Example.class, (Object) new String[] { "bar", null, "baz" });
assertThat(request.method()).isEqualTo("GET");
okhttp3.Headers headers = request.headers();
assertThat(headers.size()).isEqualTo(2);
assertThat(headers.values("foo")).containsExactly("bar", "baz");
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
Aggregations