|
1 | 1 | package com.codecombat.api; |
2 | 2 |
|
3 | | -import com.codecombat.api.client.auth.authServiceClient; |
4 | | -import com.codecombat.api.client.clans.clansServiceClient; |
5 | | -import com.codecombat.api.client.classrooms.classroomsServiceClient; |
6 | | -import com.codecombat.api.client.stats.statsServiceClient; |
7 | | -import com.codecombat.api.client.users.usersServiceClient; |
8 | | -import com.codecombat.api.core.BasicAuth; |
9 | | -import com.codecombat.api.core.Environment; |
10 | | -import java.util.Objects; |
11 | | -import java.util.concurrent.atomic.AtomicReference; |
| 3 | +import com.codecombat.api.core.ApiError; |
| 4 | +import com.codecombat.api.core.ClientOptions; |
| 5 | +import com.codecombat.api.core.ObjectMappers; |
| 6 | +import com.codecombat.api.core.RequestOptions; |
| 7 | +import com.codecombat.api.core.Suppliers; |
| 8 | +import com.codecombat.api.requests.PostUsersHandleOAuthIdentitiesRequest; |
| 9 | +import com.codecombat.api.resources.auth.AuthClient; |
| 10 | +import com.codecombat.api.resources.clans.ClansClient; |
| 11 | +import com.codecombat.api.resources.classrooms.ClassroomsClient; |
| 12 | +import com.codecombat.api.resources.stats.StatsClient; |
| 13 | +import com.codecombat.api.resources.users.UsersClient; |
| 14 | +import com.codecombat.api.types.UserResponse; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
12 | 18 | import java.util.function.Supplier; |
| 19 | +import okhttp3.Headers; |
| 20 | +import okhttp3.HttpUrl; |
| 21 | +import okhttp3.MediaType; |
| 22 | +import okhttp3.Request; |
| 23 | +import okhttp3.RequestBody; |
| 24 | +import okhttp3.Response; |
13 | 25 |
|
14 | | -public final class CodecombatApiClient { |
15 | | - private final Supplier<authServiceClient> authServiceClient; |
| 26 | +public class CodecombatApiClient { |
| 27 | + protected final ClientOptions clientOptions; |
16 | 28 |
|
17 | | - private final Supplier<clansServiceClient> clansServiceClient; |
| 29 | + protected final Supplier<AuthClient> authClient; |
18 | 30 |
|
19 | | - private final Supplier<classroomsServiceClient> classroomsServiceClient; |
| 31 | + protected final Supplier<ClansClient> clansClient; |
20 | 32 |
|
21 | | - private final Supplier<statsServiceClient> statsServiceClient; |
| 33 | + protected final Supplier<ClassroomsClient> classroomsClient; |
22 | 34 |
|
23 | | - private final Supplier<usersServiceClient> usersServiceClient; |
| 35 | + protected final Supplier<StatsClient> statsClient; |
24 | 36 |
|
25 | | - public CodecombatApiClient(BasicAuth auth) { |
26 | | - this(Environment.PRODUCTION, auth); |
27 | | - } |
| 37 | + protected final Supplier<UsersClient> usersClient; |
28 | 38 |
|
29 | | - public CodecombatApiClient(Environment environment, BasicAuth auth) { |
30 | | - this.authServiceClient = memoize(() -> new authServiceClient(environment.getUrl(), auth)); |
31 | | - this.clansServiceClient = memoize(() -> new clansServiceClient(environment.getUrl(), auth)); |
32 | | - this.statsServiceClient = memoize(() -> new statsServiceClient(environment.getUrl(), auth)); |
33 | | - this.classroomsServiceClient = memoize(() -> new classroomsServiceClient(environment.getUrl(), auth)); |
34 | | - this.usersServiceClient = memoize(() -> new usersServiceClient(environment.getUrl(), auth)); |
35 | | - } |
| 39 | + public CodecombatApiClient(ClientOptions clientOptions) { |
| 40 | + this.clientOptions = clientOptions; |
| 41 | + this.authClient = Suppliers.memoize(() -> new AuthClient(clientOptions)); |
| 42 | + this.clansClient = Suppliers.memoize(() -> new ClansClient(clientOptions)); |
| 43 | + this.classroomsClient = Suppliers.memoize(() -> new ClassroomsClient(clientOptions)); |
| 44 | + this.statsClient = Suppliers.memoize(() -> new StatsClient(clientOptions)); |
| 45 | + this.usersClient = Suppliers.memoize(() -> new UsersClient(clientOptions)); |
| 46 | + } |
36 | 47 |
|
37 | | - public final authServiceClient auth() { |
38 | | - return this.authServiceClient.get(); |
39 | | - } |
| 48 | + public UserResponse postUsersHandleOAuthIdentities(String handle, PostUsersHandleOAuthIdentitiesRequest request) { |
| 49 | + return postUsersHandleOAuthIdentities(handle, request, null); |
| 50 | + } |
40 | 51 |
|
41 | | - public final clansServiceClient clans() { |
42 | | - return this.clansServiceClient.get(); |
43 | | - } |
| 52 | + public UserResponse postUsersHandleOAuthIdentities( |
| 53 | + String handle, PostUsersHandleOAuthIdentitiesRequest request, RequestOptions requestOptions) { |
| 54 | + HttpUrl _httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) |
| 55 | + .newBuilder() |
| 56 | + .addPathSegments("users") |
| 57 | + .addPathSegment(handle) |
| 58 | + .addPathSegments("o-auth-identities") |
| 59 | + .build(); |
| 60 | + Map<String, Object> _requestBodyProperties = new HashMap<>(); |
| 61 | + _requestBodyProperties.put("provider", request.getProvider()); |
| 62 | + if (request.getAccessToken().isPresent()) { |
| 63 | + _requestBodyProperties.put("accessToken", request.getAccessToken()); |
| 64 | + } |
| 65 | + if (request.getCode().isPresent()) { |
| 66 | + _requestBodyProperties.put("code", request.getCode()); |
| 67 | + } |
| 68 | + RequestBody _requestBody; |
| 69 | + try { |
| 70 | + _requestBody = RequestBody.create( |
| 71 | + ObjectMappers.JSON_MAPPER.writeValueAsBytes(_requestBodyProperties), |
| 72 | + MediaType.parse("application/json")); |
| 73 | + } catch (Exception e) { |
| 74 | + throw new RuntimeException(e); |
| 75 | + } |
| 76 | + Request.Builder _requestBuilder = new Request.Builder() |
| 77 | + .url(_httpUrl) |
| 78 | + .method("POST", _requestBody) |
| 79 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 80 | + .addHeader("Content-Type", "application/json"); |
| 81 | + Request _request = _requestBuilder.build(); |
| 82 | + try { |
| 83 | + Response _response = clientOptions.httpClient().newCall(_request).execute(); |
| 84 | + if (_response.isSuccessful()) { |
| 85 | + return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), UserResponse.class); |
| 86 | + } |
| 87 | + throw new ApiError( |
| 88 | + _response.code(), |
| 89 | + ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), Object.class)); |
| 90 | + } catch (IOException e) { |
| 91 | + throw new RuntimeException(e); |
| 92 | + } |
| 93 | + } |
44 | 94 |
|
45 | | - public final classroomsServiceClient classrooms() { |
46 | | - return this.classroomsServiceClient.get(); |
47 | | - } |
| 95 | + public AuthClient auth() { |
| 96 | + return this.authClient.get(); |
| 97 | + } |
48 | 98 |
|
49 | | - public final statsServiceClient stats() { |
50 | | - return this.statsServiceClient.get(); |
51 | | - } |
| 99 | + public ClansClient clans() { |
| 100 | + return this.clansClient.get(); |
| 101 | + } |
52 | 102 |
|
53 | | - public final usersServiceClient users() { |
54 | | - return this.usersServiceClient.get(); |
55 | | - } |
| 103 | + public ClassroomsClient classrooms() { |
| 104 | + return this.classroomsClient.get(); |
| 105 | + } |
56 | 106 |
|
57 | | - private static <T> Supplier<T> memoize(Supplier<T> delegate) { |
58 | | - AtomicReference<T> value = new AtomicReference<>(); |
59 | | - return () -> { |
60 | | - T val = value.get(); |
61 | | - if (val == null) { |
62 | | - val = value.updateAndGet(cur -> cur == null ? Objects.requireNonNull(delegate.get()) : cur); |
63 | | - } |
64 | | - return val; |
65 | | - } ; |
66 | | - } |
| 107 | + public StatsClient stats() { |
| 108 | + return this.statsClient.get(); |
| 109 | + } |
| 110 | + |
| 111 | + public UsersClient users() { |
| 112 | + return this.usersClient.get(); |
| 113 | + } |
| 114 | + |
| 115 | + public static CodecombatApiClientBuilder builder() { |
| 116 | + return new CodecombatApiClientBuilder(); |
| 117 | + } |
67 | 118 | } |
0 commit comments