github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/okhttp-gson/auth/OAuthOkHttpClient.mustache (about)

     1  {{#hasOAuthMethods}}
     2  package {{invokerPackage}}.auth;
     3  
     4  import okhttp3.OkHttpClient;
     5  import okhttp3.MediaType;
     6  import okhttp3.Request;
     7  import okhttp3.RequestBody;
     8  import okhttp3.Response;
     9  
    10  import org.apache.oltu.oauth2.client.HttpClient;
    11  import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
    12  import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
    13  import org.apache.oltu.oauth2.client.response.OAuthClientResponseFactory;
    14  import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
    15  import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
    16  
    17  import java.io.IOException;
    18  import java.util.Map;
    19  import java.util.Map.Entry;
    20  
    21  public class OAuthOkHttpClient implements HttpClient {
    22      private OkHttpClient client;
    23  
    24      public OAuthOkHttpClient() {
    25          this.client = new OkHttpClient();
    26      }
    27  
    28      public OAuthOkHttpClient(OkHttpClient client) {
    29          this.client = client;
    30      }
    31  
    32      @Override
    33      public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
    34                                                       String requestMethod, Class<T> responseClass)
    35              throws OAuthSystemException, OAuthProblemException {
    36  
    37          MediaType mediaType = MediaType.parse("application/json");
    38          Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());
    39  
    40          if(headers != null) {
    41              for (Entry<String, String> entry : headers.entrySet()) {
    42                  if (entry.getKey().equalsIgnoreCase("Content-Type")) {
    43                      mediaType = MediaType.parse(entry.getValue());
    44                  } else {
    45                      requestBuilder.addHeader(entry.getKey(), entry.getValue());
    46                  }
    47              }
    48          }
    49  
    50          RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    51          requestBuilder.method(requestMethod, body);
    52  
    53          try {
    54              Response response = client.newCall(requestBuilder.build()).execute();
    55              return OAuthClientResponseFactory.createCustomResponse(
    56                      response.body().string(),
    57                      response.body().contentType().toString(),
    58                      response.code(),
    59                      responseClass);
    60          } catch (IOException e) {
    61              throw new OAuthSystemException(e);
    62          }
    63      }
    64  
    65      @Override
    66      public void shutdown() {
    67          // Nothing to do here
    68      }
    69  }
    70  {{/hasOAuthMethods}}