-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAsyncRawAuthenticationClient.java
More file actions
126 lines (120 loc) · 6.42 KB
/
AsyncRawAuthenticationClient.java
File metadata and controls
126 lines (120 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.skyflow.common.generated.authentication;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.skyflow.common.generated.authentication.requests.V1GetAuthTokenRequest;
import com.skyflow.common.generated.core.ApiClientApiException;
import com.skyflow.v2.generated.rest.core.ApiClientException;
import com.skyflow.v2.generated.rest.core.ApiClientHttpResponse;
import com.skyflow.v2.generated.rest.core.ClientOptions;
import com.skyflow.v2.generated.rest.core.MediaTypes;
import com.skyflow.v2.generated.rest.core.ObjectMappers;
import com.skyflow.v2.generated.rest.core.RequestOptions;
import com.skyflow.v2.generated.rest.errors.BadRequestError;
import com.skyflow.v2.generated.rest.errors.NotFoundError;
import com.skyflow.v2.generated.rest.errors.UnauthorizedError;
import com.skyflow.common.generated.types.V1GetAuthTokenResponse;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
public class AsyncRawAuthenticationClient {
protected final ClientOptions clientOptions;
public AsyncRawAuthenticationClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* <p><p>Generates a Bearer Token to authenticate with Skyflow. This method doesn't require the <code>Authorization</code> header.</p><p><b>Note:</b> For recommended ways to authenticate, see <a href='/api-authentication/'>API authentication</a>.</p></p>
*/
public CompletableFuture<ApiClientHttpResponse<V1GetAuthTokenResponse>> authenticationServiceGetAuthToken(
V1GetAuthTokenRequest request) {
return authenticationServiceGetAuthToken(request, null);
}
/**
* <p><p>Generates a Bearer Token to authenticate with Skyflow. This method doesn't require the <code>Authorization</code> header.</p><p><b>Note:</b> For recommended ways to authenticate, see <a href='/api-authentication/'>API authentication</a>.</p></p>
*/
public CompletableFuture<ApiClientHttpResponse<V1GetAuthTokenResponse>> authenticationServiceGetAuthToken(
V1GetAuthTokenRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v1/auth/sa/oauth/token")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new ApiClientException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture<ApiClientHttpResponse<V1GetAuthTokenResponse>> future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(new ApiClientHttpResponse<>(
ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), V1GetAuthTokenResponse.class),
response));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
try {
switch (response.code()) {
case 400:
future.completeExceptionally(new BadRequestError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
response));
return;
case 401:
future.completeExceptionally(new UnauthorizedError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
response));
return;
case 404:
future.completeExceptionally(new NotFoundError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
response));
return;
}
} catch (JsonProcessingException ignored) {
// unable to map error response, throwing generic error
}
future.completeExceptionally(new ApiClientApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
response));
return;
} catch (IOException e) {
future.completeExceptionally(new ApiClientException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new ApiClientException("Network error executing HTTP request", e));
}
});
return future;
}
}