Search in sources :

Example 1 with PUT

use of retrofit2.http.PUT in project Tusky by Vavassor.

the class LoginActivity method onStart.

@Override
protected void onStart() {
    super.onStart();
    /* Check if we are resuming during authorization by seeing if the intent contains the
         * redirect that was given to the server. If so, its response is here! */
    Uri uri = getIntent().getData();
    String redirectUri = getOauthRedirectUri();
    preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
    if (preferences.getString("accessToken", null) != null && preferences.getString("domain", null) != null) {
        // We are already logged in, go to MainActivity
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
        return;
    }
    if (uri != null && uri.toString().startsWith(redirectUri)) {
        // This should either have returned an authorization code or an error.
        String code = uri.getQueryParameter("code");
        String error = uri.getQueryParameter("error");
        if (code != null) {
            /* During the redirect roundtrip this Activity usually dies, which wipes out the
                 * instance variables, so they have to be recovered from where they were saved in
                 * SharedPreferences. */
            domain = preferences.getString("domain", null);
            clientId = preferences.getString("clientId", null);
            clientSecret = preferences.getString("clientSecret", null);
            setLoading(true);
            /* Since authorization has succeeded, the final step to log in is to exchange
                 * the authorization code for an access token. */
            Callback<AccessToken> callback = new Callback<AccessToken>() {

                @Override
                public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
                    if (response.isSuccessful()) {
                        onLoginSuccess(response.body().accessToken);
                    } else {
                        setLoading(false);
                        editText.setError(getString(R.string.error_retrieving_oauth_token));
                        Log.e(TAG, String.format("%s %s", getString(R.string.error_retrieving_oauth_token), response.message()));
                    }
                }

                @Override
                public void onFailure(Call<AccessToken> call, Throwable t) {
                    setLoading(false);
                    editText.setError(getString(R.string.error_retrieving_oauth_token));
                    Log.e(TAG, String.format("%s %s", getString(R.string.error_retrieving_oauth_token), t.getMessage()));
                }
            };
            getApiFor(domain).fetchOAuthToken(clientId, clientSecret, redirectUri, code, "authorization_code").enqueue(callback);
        } else if (error != null) {
            /* Authorization failed. Put the error response where the user can read it and they
                 * can try again. */
            setLoading(false);
            editText.setError(getString(R.string.error_authorization_denied));
            Log.e(TAG, getString(R.string.error_authorization_denied) + error);
        } else {
            setLoading(false);
            // This case means a junk response was received somehow.
            editText.setError(getString(R.string.error_authorization_unknown));
        }
    }
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) AccessToken(com.keylesspalace.tusky.entity.AccessToken) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) Uri(android.net.Uri)

Example 2 with PUT

use of retrofit2.http.PUT in project retrofit by square.

the class RequestBuilderTest method put.

@Test
public void put() {
    class Example {

        // 
        @PUT("/foo/bar/")
        Call<ResponseBody> method(@Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
    Request request = buildRequest(Example.class, body);
    assertThat(request.method()).isEqualTo("PUT");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertBody(request.body(), "hi");
}
Also used : Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 3 with PUT

use of retrofit2.http.PUT in project autorest.java by Azure.

the class LRORetrysImpl method put201CreatingSucceeded200WithServiceResponseAsync.

/**
 * Long running put request, service returns a 500, then a 201 to the initial request, with an entity that contains ProvisioningState=’Creating’.  Polls return this value until the last poll returns a ‘200’ with ProvisioningState=’Succeeded’.
 *
 * @throws IllegalArgumentException thrown if parameters fail the validation
 * @return the observable for the request
 */
public Observable<ServiceResponse<Product>> put201CreatingSucceeded200WithServiceResponseAsync() {
    final Product product = null;
    Observable<Response<ResponseBody>> observable = service.put201CreatingSucceeded200(product, this.client.acceptLanguage(), this.client.userAgent());
    return client.getAzureClient().getPutOrPatchResultAsync(observable, new TypeToken<Product>() {
    }.getType());
}
Also used : Response(retrofit2.Response) ServiceResponse(com.microsoft.rest.ServiceResponse) TypeToken(com.google.common.reflect.TypeToken) Product(fixtures.lro.models.Product)

Example 4 with PUT

use of retrofit2.http.PUT in project autorest.java by Azure.

the class LROsCustomHeadersImpl method putAsyncRetrySucceededWithServiceResponseAsync.

/**
 * x-ms-client-request-id = 9C4D50EE-2D56-4CD3-8152-34347DC9F2B0 is required message header for all requests. Long running put request, service returns a 200 to the initial request, with an entity that contains ProvisioningState=’Creating’. Poll the endpoint indicated in the Azure-AsyncOperation header for operation status.
 *
 * @throws IllegalArgumentException thrown if parameters fail the validation
 * @return the observable for the request
 */
public Observable<ServiceResponseWithHeaders<Product, LROsCustomHeaderPutAsyncRetrySucceededHeaders>> putAsyncRetrySucceededWithServiceResponseAsync() {
    final Product product = null;
    Observable<Response<ResponseBody>> observable = service.putAsyncRetrySucceeded(product, this.client.acceptLanguage(), this.client.userAgent());
    return client.getAzureClient().getPutOrPatchResultWithHeadersAsync(observable, new TypeToken<Product>() {
    }.getType(), LROsCustomHeaderPutAsyncRetrySucceededHeaders.class);
}
Also used : ServiceResponse(com.microsoft.rest.ServiceResponse) Response(retrofit2.Response) TypeToken(com.google.common.reflect.TypeToken) Product(fixtures.lro.models.Product)

Example 5 with PUT

use of retrofit2.http.PUT in project autorest.java by Azure.

the class LROSADsImpl method putAsyncRelativeRetryNoStatusPayloadWithServiceResponseAsync.

/**
 * Long running put request, service returns a 200 to the initial request, with an entity that contains ProvisioningState=’Creating’. Poll the endpoint indicated in the Azure-AsyncOperation header for operation status.
 *
 * @throws IllegalArgumentException thrown if parameters fail the validation
 * @return the observable for the request
 */
public Observable<ServiceResponseWithHeaders<Product, LROSADsPutAsyncRelativeRetryNoStatusPayloadHeaders>> putAsyncRelativeRetryNoStatusPayloadWithServiceResponseAsync() {
    final Product product = null;
    Observable<Response<ResponseBody>> observable = service.putAsyncRelativeRetryNoStatusPayload(product, this.client.acceptLanguage(), this.client.userAgent());
    return client.getAzureClient().getPutOrPatchResultWithHeadersAsync(observable, new TypeToken<Product>() {
    }.getType(), LROSADsPutAsyncRelativeRetryNoStatusPayloadHeaders.class);
}
Also used : Response(retrofit2.Response) ServiceResponse(com.microsoft.rest.ServiceResponse) TypeToken(com.google.common.reflect.TypeToken) Product(fixtures.lro.models.Product)

Aggregations

Response (retrofit2.Response)36 ServiceResponse (com.microsoft.rest.ServiceResponse)33 TypeToken (com.google.common.reflect.TypeToken)31 Product (fixtures.lro.models.Product)27 SubProduct (fixtures.lro.models.SubProduct)15 Intent (android.content.Intent)2 Uri (android.net.Uri)2 ResponseBody (okhttp3.ResponseBody)2 ValueAnimator (android.animation.ValueAnimator)1 ActionBar (android.app.ActionBar)1 Activity (android.app.Activity)1 TaskStackBuilder (android.app.TaskStackBuilder)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 Intent.createChooser (android.content.Intent.createChooser)1 PackageManager (android.content.pm.PackageManager)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Color (android.graphics.Color)1