All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.shell.apitest.controllers.OAuthAuthorizationController Maven / Gradle / Ivy

/*
 * ShellCardManagementAPIsLib
 *
 * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
 */

package com.shell.apitest.controllers;

import com.shell.apitest.ApiHelper;
import com.shell.apitest.Server;
import com.shell.apitest.exceptions.ApiException;
import com.shell.apitest.exceptions.OAuthProviderException;
import com.shell.apitest.http.request.HttpMethod;
import com.shell.apitest.models.OAuthToken;
import io.apimatic.core.ApiCall;
import io.apimatic.core.ErrorCase;
import io.apimatic.core.GlobalConfiguration;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

/**
 * This class lists all the endpoints of the groups.
 */
public final class OAuthAuthorizationController extends BaseController {

    /**
     * Initializes the controller.
     * @param globalConfig    Configurations added in client.
     */
    public OAuthAuthorizationController(GlobalConfiguration globalConfig) {
        super(globalConfig);
    }

    /**
     * Create a new OAuth 2 token.
     * @param  authorization  Required parameter: Authorization header in Basic auth format
     * @param  scope  Optional parameter: Requested scopes as a space-delimited list.
     * @param    fieldParameters    Additional optional form parameters are supported by this endpoint
     * @return    Returns the OAuthToken response from the API call
     * @throws    ApiException    Represents error response from the server.
     * @throws    IOException    Signals that an I/O exception of some sort has occurred.
     */
    public OAuthToken requestTokenBearerToken(
            final String authorization,
            final String scope,
            final Map fieldParameters) throws ApiException, IOException {
        return prepareRequestTokenBearerTokenRequest(authorization, scope,
                fieldParameters).execute();
    }

    /**
     * Create a new OAuth 2 token.
     * @param  authorization  Required parameter: Authorization header in Basic auth format
     * @param  scope  Optional parameter: Requested scopes as a space-delimited list.
     * @param    fieldParameters    Additional optional form parameters are supported by this endpoint
     * @return    Returns the OAuthToken response from the API call
     */
    public CompletableFuture requestTokenBearerTokenAsync(
            final String authorization,
            final String scope,
            final Map fieldParameters) {
        try { 
            return prepareRequestTokenBearerTokenRequest(authorization, scope,
            fieldParameters).executeAsync(); 
        } catch (Exception e) {  
            throw new CompletionException(e); 
        }
    }

    /**
     * Builds the ApiCall object for requestTokenBearerToken.
     */
    private ApiCall prepareRequestTokenBearerTokenRequest(
            final String authorization,
            final String scope,
            final Map fieldParameters) throws IOException {
        return new ApiCall.Builder()
                .globalConfig(getGlobalConfiguration())
                .requestBuilder(requestBuilder -> requestBuilder
                        .server(Server.OAUTH_SERVER.value())
                        .path("/v1/oauth/token")
                        .formParam(param -> param.key("grant_type")
                                .value("client_credentials").isRequired(false))
                        .formParam(param -> param.key("scope")
                                .value(scope).isRequired(false))
                        .formParam(fieldParameters)
                        .headerParam(param -> param.key("Authorization")
                                .value(authorization).isRequired(false))
                        .headerParam(param -> param.key("accept").value("application/json"))
                        .httpMethod(HttpMethod.POST))
                .responseHandler(responseHandler -> responseHandler
                        .deserializer(
                                response -> ApiHelper.deserialize(response, OAuthToken.class))
                        .nullify404(false)
                        .localErrorCase("400",
                                 ErrorCase.setReason("OAuth 2 provider returned an error.",
                                (reason, context) -> new OAuthProviderException(reason, context)))
                        .localErrorCase("401",
                                 ErrorCase.setReason("OAuth 2 provider says client authentication failed.",
                                (reason, context) -> new OAuthProviderException(reason, context)))
                        .globalErrorCase(GLOBAL_ERROR_CASES))
                .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy