Search in sources :

Example 66 with Header

use of retrofit2.http.Header in project autorest-clientruntime-for-java by Azure.

the class UserAgentTests method defaultUserAgentTests.

@Test
public void defaultUserAgentTests() throws Exception {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().addInterceptor(new UserAgentInterceptor()).addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            String header = chain.request().header("User-Agent");
            Assert.assertEquals("AutoRest-Java", header);
            return new Response.Builder().request(chain.request()).code(200).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
        }
    });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) {
    };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) Test(org.junit.Test)

Example 67 with Header

use of retrofit2.http.Header in project android-diplicity by zond.

the class RetrofitActivity method onActivityResult.

@Override
protected void onActivityResult(final int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == LOGIN_REQUEST) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result != null) {
            if (result.isSuccess()) {
                final GoogleSignInAccount acct = result.getSignInAccount();
                Observable.create(new Observable.OnSubscribe<String>() {

                    @Override
                    public void call(final Subscriber<? super String> subscriber) {
                        Response response = null;
                        try {
                            String url = getBaseURL() + "Auth/OAuth2Callback?code=" + URLEncoder.encode(acct.getServerAuthCode(), "UTF-8") + "&approve-redirect=true&state=" + URLEncoder.encode("https://android-diplicity", "UTF-8");
                            Request request = new Request.Builder().url(url).build();
                            response = new OkHttpClient.Builder().followRedirects(false).followSslRedirects(false).connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).build().newCall(request).execute();
                            if (response.code() < 300 || response.code() > 399) {
                                throw new RuntimeException("Unsuccessful response " + response.body().string());
                            }
                            url = response.headers().get("Location");
                            if (url == null) {
                                throw new RuntimeException("No Location header in response " + response.body().string());
                            }
                            Uri parsedURI = Uri.parse(url);
                            if (parsedURI == null) {
                                throw new RuntimeException("Unparseable Location header " + url + " in response");
                            }
                            subscriber.onNext(parsedURI.getQueryParameter("token"));
                            subscriber.onCompleted();
                        } catch (RuntimeException e) {
                            throw e;
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<String>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        App.firebaseCrashReport("Unable to log in", e);
                        Toast.makeText(RetrofitActivity.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(String token) {
                        PreferenceManager.getDefaultSharedPreferences(RetrofitActivity.this).edit().putString(AUTH_TOKEN_PREF_KEY, token).apply();
                        performLogin();
                    }
                });
            } else {
                App.firebaseCrashReport("Login failed: " + result.getStatus().getStatus());
                Toast.makeText(this, R.string.login_failed, Toast.LENGTH_SHORT).show();
            }
        } else {
            App.firebaseCrashReport("Login failed, null result");
            Toast.makeText(this, R.string.login_failed, Toast.LENGTH_SHORT).show();
        }
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) Uri(android.net.Uri) GoogleSignInResult(com.google.android.gms.auth.api.signin.GoogleSignInResult) HttpException(retrofit2.adapter.rxjava.HttpException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) Response(okhttp3.Response) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) Subscriber(rx.Subscriber)

Aggregations

Response (retrofit2.Response)39 ServiceResponse (com.microsoft.rest.ServiceResponse)30 TypeToken (com.google.common.reflect.TypeToken)29 Product (fixtures.lro.models.Product)29 Request (okhttp3.Request)23 Retrofit (retrofit2.Retrofit)21 OkHttpClient (okhttp3.OkHttpClient)19 IOException (java.io.IOException)15 Test (org.junit.Test)14 SubProduct (fixtures.lro.models.SubProduct)12 Interceptor (okhttp3.Interceptor)12 Response (okhttp3.Response)10 ResponseBody (okhttp3.ResponseBody)9 Header (retrofit2.http.Header)6 ArrayList (java.util.ArrayList)4 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)4 GsonBuilder (com.google.gson.GsonBuilder)3 HttpException (retrofit2.HttpException)3 Context (android.content.Context)2 Nullable (android.support.annotation.Nullable)2