-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe46460
commit 908d899
Showing
155 changed files
with
6,982 additions
and
12,634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 101 additions & 50 deletions
151
src/main/java/com/codecombat/api/CodecombatApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,118 @@ | ||
package com.codecombat.api; | ||
|
||
import com.codecombat.api.client.auth.authServiceClient; | ||
import com.codecombat.api.client.clans.clansServiceClient; | ||
import com.codecombat.api.client.classrooms.classroomsServiceClient; | ||
import com.codecombat.api.client.stats.statsServiceClient; | ||
import com.codecombat.api.client.users.usersServiceClient; | ||
import com.codecombat.api.core.BasicAuth; | ||
import com.codecombat.api.core.Environment; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import com.codecombat.api.core.ApiError; | ||
import com.codecombat.api.core.ClientOptions; | ||
import com.codecombat.api.core.ObjectMappers; | ||
import com.codecombat.api.core.RequestOptions; | ||
import com.codecombat.api.core.Suppliers; | ||
import com.codecombat.api.requests.PostUsersHandleOAuthIdentitiesRequest; | ||
import com.codecombat.api.resources.auth.AuthClient; | ||
import com.codecombat.api.resources.clans.ClansClient; | ||
import com.codecombat.api.resources.classrooms.ClassroomsClient; | ||
import com.codecombat.api.resources.stats.StatsClient; | ||
import com.codecombat.api.resources.users.UsersClient; | ||
import com.codecombat.api.types.UserResponse; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
import okhttp3.Headers; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.MediaType; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
|
||
public final class CodecombatApiClient { | ||
private final Supplier<authServiceClient> authServiceClient; | ||
public class CodecombatApiClient { | ||
protected final ClientOptions clientOptions; | ||
|
||
private final Supplier<clansServiceClient> clansServiceClient; | ||
protected final Supplier<AuthClient> authClient; | ||
|
||
private final Supplier<classroomsServiceClient> classroomsServiceClient; | ||
protected final Supplier<ClansClient> clansClient; | ||
|
||
private final Supplier<statsServiceClient> statsServiceClient; | ||
protected final Supplier<ClassroomsClient> classroomsClient; | ||
|
||
private final Supplier<usersServiceClient> usersServiceClient; | ||
protected final Supplier<StatsClient> statsClient; | ||
|
||
public CodecombatApiClient(BasicAuth auth) { | ||
this(Environment.PRODUCTION, auth); | ||
} | ||
protected final Supplier<UsersClient> usersClient; | ||
|
||
public CodecombatApiClient(Environment environment, BasicAuth auth) { | ||
this.authServiceClient = memoize(() -> new authServiceClient(environment.getUrl(), auth)); | ||
this.clansServiceClient = memoize(() -> new clansServiceClient(environment.getUrl(), auth)); | ||
this.statsServiceClient = memoize(() -> new statsServiceClient(environment.getUrl(), auth)); | ||
this.classroomsServiceClient = memoize(() -> new classroomsServiceClient(environment.getUrl(), auth)); | ||
this.usersServiceClient = memoize(() -> new usersServiceClient(environment.getUrl(), auth)); | ||
} | ||
public CodecombatApiClient(ClientOptions clientOptions) { | ||
this.clientOptions = clientOptions; | ||
this.authClient = Suppliers.memoize(() -> new AuthClient(clientOptions)); | ||
this.clansClient = Suppliers.memoize(() -> new ClansClient(clientOptions)); | ||
this.classroomsClient = Suppliers.memoize(() -> new ClassroomsClient(clientOptions)); | ||
this.statsClient = Suppliers.memoize(() -> new StatsClient(clientOptions)); | ||
this.usersClient = Suppliers.memoize(() -> new UsersClient(clientOptions)); | ||
} | ||
|
||
public final authServiceClient auth() { | ||
return this.authServiceClient.get(); | ||
} | ||
public UserResponse postUsersHandleOAuthIdentities(String handle, PostUsersHandleOAuthIdentitiesRequest request) { | ||
return postUsersHandleOAuthIdentities(handle, request, null); | ||
} | ||
|
||
public final clansServiceClient clans() { | ||
return this.clansServiceClient.get(); | ||
} | ||
public UserResponse postUsersHandleOAuthIdentities( | ||
String handle, PostUsersHandleOAuthIdentitiesRequest request, RequestOptions requestOptions) { | ||
HttpUrl _httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) | ||
.newBuilder() | ||
.addPathSegments("users") | ||
.addPathSegment(handle) | ||
.addPathSegments("o-auth-identities") | ||
.build(); | ||
Map<String, Object> _requestBodyProperties = new HashMap<>(); | ||
_requestBodyProperties.put("provider", request.getProvider()); | ||
if (request.getAccessToken().isPresent()) { | ||
_requestBodyProperties.put("accessToken", request.getAccessToken()); | ||
} | ||
if (request.getCode().isPresent()) { | ||
_requestBodyProperties.put("code", request.getCode()); | ||
} | ||
RequestBody _requestBody; | ||
try { | ||
_requestBody = RequestBody.create( | ||
ObjectMappers.JSON_MAPPER.writeValueAsBytes(_requestBodyProperties), | ||
MediaType.parse("application/json")); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
Request.Builder _requestBuilder = new Request.Builder() | ||
.url(_httpUrl) | ||
.method("POST", _requestBody) | ||
.headers(Headers.of(clientOptions.headers(requestOptions))) | ||
.addHeader("Content-Type", "application/json"); | ||
Request _request = _requestBuilder.build(); | ||
try { | ||
Response _response = clientOptions.httpClient().newCall(_request).execute(); | ||
if (_response.isSuccessful()) { | ||
return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), UserResponse.class); | ||
} | ||
throw new ApiError( | ||
_response.code(), | ||
ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), Object.class)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public final classroomsServiceClient classrooms() { | ||
return this.classroomsServiceClient.get(); | ||
} | ||
public AuthClient auth() { | ||
return this.authClient.get(); | ||
} | ||
|
||
public final statsServiceClient stats() { | ||
return this.statsServiceClient.get(); | ||
} | ||
public ClansClient clans() { | ||
return this.clansClient.get(); | ||
} | ||
|
||
public final usersServiceClient users() { | ||
return this.usersServiceClient.get(); | ||
} | ||
public ClassroomsClient classrooms() { | ||
return this.classroomsClient.get(); | ||
} | ||
|
||
private static <T> Supplier<T> memoize(Supplier<T> delegate) { | ||
AtomicReference<T> value = new AtomicReference<>(); | ||
return () -> { | ||
T val = value.get(); | ||
if (val == null) { | ||
val = value.updateAndGet(cur -> cur == null ? Objects.requireNonNull(delegate.get()) : cur); | ||
} | ||
return val; | ||
} ; | ||
} | ||
public StatsClient stats() { | ||
return this.statsClient.get(); | ||
} | ||
|
||
public UsersClient users() { | ||
return this.usersClient.get(); | ||
} | ||
|
||
public static CodecombatApiClientBuilder builder() { | ||
return new CodecombatApiClientBuilder(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/codecombat/api/CodecombatApiClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.codecombat.api; | ||
|
||
import com.codecombat.api.core.ClientOptions; | ||
import com.codecombat.api.core.Environment; | ||
import java.util.Base64; | ||
|
||
public final class CodecombatApiClientBuilder { | ||
private ClientOptions.Builder clientOptionsBuilder = ClientOptions.builder(); | ||
|
||
private Environment environment = Environment.DEFAULT; | ||
|
||
public CodecombatApiClientBuilder credentials(String username, String password) { | ||
String unencodedToken = username + ":" + password; | ||
String encodedToken = Base64.getEncoder().encodeToString(unencodedToken.getBytes()); | ||
this.clientOptionsBuilder.addHeader("Authorization", "Basic " + encodedToken); | ||
return this; | ||
} | ||
|
||
public CodecombatApiClientBuilder environment(Environment environment) { | ||
this.environment = environment; | ||
return this; | ||
} | ||
|
||
public CodecombatApiClientBuilder url(String url) { | ||
this.environment = Environment.custom(url); | ||
return this; | ||
} | ||
|
||
public CodecombatApiClient build() { | ||
clientOptionsBuilder.environment(this.environment); | ||
return new CodecombatApiClient(clientOptionsBuilder.build()); | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
src/main/java/com/codecombat/api/client/auth/authService.java
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
src/main/java/com/codecombat/api/client/auth/authServiceClient.java
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
src/main/java/com/codecombat/api/client/auth/authServiceErrorDecoder.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.