Skip to content

Commit

Permalink
Release 0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] committed Aug 29, 2023
1 parent fe46460 commit 908d899
Show file tree
Hide file tree
Showing 155 changed files with 6,982 additions and 12,634 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,24 @@ jobs:
- name: Compile
run: ./gradlew compileJava

publish:
test:
needs: [ compile ]
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Set up Java
id: setup-jre
uses: actions/setup-java@v1
with:
java-version: "11"
architecture: x64

- name: Test
run: ./gradlew test
publish:
needs: [ compile, test ]
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
runs-on: ubuntu-latest

Expand Down
20 changes: 12 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ repositories {
}

dependencies {
api 'io.github.fern-api:jersey-utils:0.0.82'
api 'com.squareup.okhttp3:okhttp:4.9.3'
api 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
api 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.12.3'
api 'io.github.openfeign:feign-jackson:11.8'
api 'io.github.openfeign:feign-core:11.8'
api 'io.github.openfeign:feign-jaxrs2:11.8'
api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.3'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

spotless {
java {
googleJavaFormat()
palantirJavaFormat()
}
}

Expand All @@ -31,12 +31,16 @@ java {
withJavadocJar()
}

test {
useJUnitPlatform()
}

publishing {
publications {
maven(MavenPublication) {
groupId = 'io.github.fern-api'
artifactId = 'codecombat'
version = '0.0.9'
groupId = 'com.codecombat'
artifactId = 'codecombat-java'
version = '0.1.7'
from components.java
}
}
Expand Down
151 changes: 101 additions & 50 deletions src/main/java/com/codecombat/api/CodecombatApiClient.java
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 src/main/java/com/codecombat/api/CodecombatApiClientBuilder.java
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 src/main/java/com/codecombat/api/client/auth/authService.java

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 908d899

Please sign in to comment.