Skip to content

Instantly share code, notes, and snippets.

@ognis1205
Created December 12, 2024 14:33
Show Gist options
  • Save ognis1205/526371ddaed10cb2e96ab83ccc24012b to your computer and use it in GitHub Desktop.
Save ognis1205/526371ddaed10cb2e96ab83ccc24012b to your computer and use it in GitHub Desktop.
OAuth2 API created by OpenAPI Generator
/*
* Unity Control API
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 0.1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package io.unitycatalog.control.api;
import io.unitycatalog.control.ApiClient;
import io.unitycatalog.control.ApiException;
import io.unitycatalog.control.ApiResponse;
import io.unitycatalog.control.Pair;
import io.unitycatalog.control.model.GrantType;
import io.unitycatalog.control.model.OAuthTokenExchangeResponse;
import io.unitycatalog.control.model.TokenType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.http.HttpRequest;
import java.nio.channels.Channels;
import java.nio.channels.Pipe;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.ArrayList;
import java.util.StringJoiner;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.9.0")
public class OAuth2Api {
private final HttpClient memberVarHttpClient;
private final ObjectMapper memberVarObjectMapper;
private final String memberVarBaseUri;
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
private final Duration memberVarReadTimeout;
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
public OAuth2Api() {
this(new ApiClient());
}
public OAuth2Api(ApiClient apiClient) {
memberVarHttpClient = apiClient.getHttpClient();
memberVarObjectMapper = apiClient.getObjectMapper();
memberVarBaseUri = apiClient.getBaseUri();
memberVarInterceptor = apiClient.getRequestInterceptor();
memberVarReadTimeout = apiClient.getReadTimeout();
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
}
protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
String body = response.body() == null ? null : new String(response.body().readAllBytes());
String message = formatExceptionMessage(operationId, response.statusCode(), body);
return new ApiException(response.statusCode(), message, response.headers(), body);
}
private String formatExceptionMessage(String operationId, int statusCode, String body) {
if (body == null || body.isEmpty()) {
body = "[no body]";
}
return operationId + " call failed with: " + statusCode + " - " + body;
}
/**
* Get a token using an OAuth2 flow
* Exchanges credentials for a token using the OAuth2 token exchange.
* @param grantType (required)
* @param subjectToken Subject token for token exchange request. (required)
* @param subjectTokenType (required)
* @param scope Authorization scope for client credentials or token exchange. (optional)
* @param requestedTokenType (optional)
* @param actorToken Actor token for token exchange request. (optional)
* @param actorTokenType (optional)
* @return OAuthTokenExchangeResponse
* @throws ApiException if fails to make API call
*/
public OAuthTokenExchangeResponse getToken(GrantType grantType, String subjectToken, TokenType subjectTokenType, String scope, TokenType requestedTokenType, String actorToken, TokenType actorTokenType) throws ApiException {
ApiResponse<OAuthTokenExchangeResponse> localVarResponse = getTokenWithHttpInfo(grantType, subjectToken, subjectTokenType, scope, requestedTokenType, actorToken, actorTokenType);
return localVarResponse.getData();
}
/**
* Get a token using an OAuth2 flow
* Exchanges credentials for a token using the OAuth2 token exchange.
* @param grantType (required)
* @param subjectToken Subject token for token exchange request. (required)
* @param subjectTokenType (required)
* @param scope Authorization scope for client credentials or token exchange. (optional)
* @param requestedTokenType (optional)
* @param actorToken Actor token for token exchange request. (optional)
* @param actorTokenType (optional)
* @return ApiResponse&lt;OAuthTokenExchangeResponse&gt;
* @throws ApiException if fails to make API call
*/
public ApiResponse<OAuthTokenExchangeResponse> getTokenWithHttpInfo(GrantType grantType, String subjectToken, TokenType subjectTokenType, String scope, TokenType requestedTokenType, String actorToken, TokenType actorTokenType) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = getTokenRequestBuilder(grantType, subjectToken, subjectTokenType, scope, requestedTokenType, actorToken, actorTokenType);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofInputStream());
if (memberVarResponseInterceptor != null) {
memberVarResponseInterceptor.accept(localVarResponse);
}
try {
if (localVarResponse.statusCode()/ 100 != 2) {
throw getApiException("getToken", localVarResponse);
}
return new ApiResponse<OAuthTokenExchangeResponse>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
localVarResponse.body() == null ? null : memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<OAuthTokenExchangeResponse>() {}) // closes the InputStream
);
} finally {
}
} catch (IOException e) {
throw new ApiException(e);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ApiException(e);
}
}
private HttpRequest.Builder getTokenRequestBuilder(GrantType grantType, String subjectToken, TokenType subjectTokenType, String scope, TokenType requestedTokenType, String actorToken, TokenType actorTokenType) throws ApiException {
// verify the required parameter 'grantType' is set
if (grantType == null) {
throw new ApiException(400, "Missing the required parameter 'grantType' when calling getToken");
}
// verify the required parameter 'subjectToken' is set
if (subjectToken == null) {
throw new ApiException(400, "Missing the required parameter 'subjectToken' when calling getToken");
}
// verify the required parameter 'subjectTokenType' is set
if (subjectTokenType == null) {
throw new ApiException(400, "Missing the required parameter 'subjectTokenType' when calling getToken");
}
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
String localVarPath = "/auth/tokens";
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
localVarRequestBuilder.header("Accept", "application/json");
List<NameValuePair> formValues = new ArrayList<>();
if (grantType != null) {
formValues.add(new BasicNameValuePair("grant_type", grantType.toString()));
}
if (scope != null) {
formValues.add(new BasicNameValuePair("scope", scope.toString()));
}
if (requestedTokenType != null) {
formValues.add(new BasicNameValuePair("requested_token_type", requestedTokenType.toString()));
}
if (subjectToken != null) {
formValues.add(new BasicNameValuePair("subject_token", subjectToken.toString()));
}
if (subjectTokenType != null) {
formValues.add(new BasicNameValuePair("subject_token_type", subjectTokenType.toString()));
}
if (actorToken != null) {
formValues.add(new BasicNameValuePair("actor_token", actorToken.toString()));
}
if (actorTokenType != null) {
formValues.add(new BasicNameValuePair("actor_token_type", actorTokenType.toString()));
}
HttpEntity entity = new UrlEncodedFormEntity(formValues, java.nio.charset.StandardCharsets.UTF_8);
ByteArrayOutputStream formOutputStream = new ByteArrayOutputStream();
try {
entity.writeTo(formOutputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
localVarRequestBuilder
.header("Content-Type", entity.getContentType().getValue())
.method("POST", HttpRequest.BodyPublishers
.ofInputStream(() -> new ByteArrayInputStream(formOutputStream.toByteArray())));
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
return localVarRequestBuilder;
}
/**
* Expires the access token
* Expires the authorixation cookie.
* @param body (optional)
* @throws ApiException if fails to make API call
*/
public void logout(Object body) throws ApiException {
logoutWithHttpInfo(body);
}
/**
* Expires the access token
* Expires the authorixation cookie.
* @param body (optional)
* @return ApiResponse&lt;Void&gt;
* @throws ApiException if fails to make API call
*/
public ApiResponse<Void> logoutWithHttpInfo(Object body) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = logoutRequestBuilder(body);
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofInputStream());
if (memberVarResponseInterceptor != null) {
memberVarResponseInterceptor.accept(localVarResponse);
}
try {
if (localVarResponse.statusCode()/ 100 != 2) {
throw getApiException("logout", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
null
);
} finally {
// Drain the InputStream
while (localVarResponse.body().read() != -1) {
// Ignore
}
localVarResponse.body().close();
}
} catch (IOException e) {
throw new ApiException(e);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ApiException(e);
}
}
private HttpRequest.Builder logoutRequestBuilder(Object body) throws ApiException {
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
String localVarPath = "/auth/logout";
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
localVarRequestBuilder.header("Content-Type", "application/json");
localVarRequestBuilder.header("Accept", "application/json");
try {
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
} catch (IOException e) {
throw new ApiException(e);
}
if (memberVarReadTimeout != null) {
localVarRequestBuilder.timeout(memberVarReadTimeout);
}
if (memberVarInterceptor != null) {
memberVarInterceptor.accept(localVarRequestBuilder);
}
return localVarRequestBuilder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment