diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..0c5af22 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/MintakaJvm"] + path = vendor/MintakaJvm + url = https://github.com/Tribufu/MintakaJvm diff --git a/lib/build.gradle b/lib/build.gradle index 53c1cdd..cf35f2a 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -11,8 +11,9 @@ repositories { dependencies { api "org.apache.commons:commons-math3:3.6.1" - implementation "com.google.guava:guava:31.1-jre" implementation "com.google.code.gson:gson:2.8.9" + implementation "com.google.guava:guava:31.1-jre" + implementation project(':MintakaJvm') testImplementation "org.junit.jupiter:junit-jupiter:5.9.1" } diff --git a/lib/src/main/java/com/tribufu/TribufuApi.java b/lib/src/main/java/com/tribufu/TribufuApi.java index 063660f..745c882 100644 --- a/lib/src/main/java/com/tribufu/TribufuApi.java +++ b/lib/src/main/java/com/tribufu/TribufuApi.java @@ -2,16 +2,15 @@ package com.tribufu; -import com.tribufu.http.TribufuHttp; import com.tribufu.types.games.Game; import com.tribufu.types.servers.Server; import com.tribufu.types.users.Profile; - +import dev.mintaka.http.HttpClient; import java.util.ArrayList; +import java.util.concurrent.CompletableFuture; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.CompletableFuture; /** * Tribufu API @@ -30,7 +29,7 @@ public class TribufuApi { protected final String baseUrl; protected final TribufuApiOptions options; - protected final TribufuHttp http; + protected final HttpClient http; /** * Create a TribufuApi with the default options. @@ -58,7 +57,7 @@ public class TribufuApi { public TribufuApi(TribufuApiOptions options) { this.options = options; this.baseUrl = TribufuApi.getBaseUrl(); - this.http = new TribufuHttp(baseUrl, TribufuApi.defaultHeaders(), true, TribufuApi.API_URL); + this.http = new HttpClient(baseUrl, TribufuApi.defaultHeaders(), true, TribufuApi.API_URL); } /** diff --git a/lib/src/main/java/com/tribufu/http/TribufuHttp.java b/lib/src/main/java/com/tribufu/http/TribufuHttp.java deleted file mode 100644 index 19497cf..0000000 --- a/lib/src/main/java/com/tribufu/http/TribufuHttp.java +++ /dev/null @@ -1,213 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.http; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; - -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.net.URI; -import java.util.concurrent.CompletableFuture; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -/** - * Tribufu Http - * - * Helper class to make HTTP requests to the Tribufu API. - */ -public class TribufuHttp { - private static final Gson gson = new GsonBuilder().create(); - - private final HttpClient inner; - protected final TribufuHttpOptions options; - - public TribufuHttp() { - this(new TribufuHttpOptions()); - } - - public TribufuHttp(String baseUrl, Map headers, boolean logEnabled, String logTarget) { - this(new TribufuHttpOptions(baseUrl, headers, logEnabled, logTarget)); - } - - public TribufuHttp(TribufuHttpOptions options) { - this.options = options; - this.inner = HttpClient.newBuilder().build(); - } - - /** - * Helper method to send an asynchronous HTTP request. - */ - private CompletableFuture sendRequest(HttpRequest request) { - return this.inner.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body); - } - - /** - * Helper method to send an asynchronous HTTP request. - */ - private CompletableFuture sendRequest(HttpRequest request, Class returnType) { - return this.inner.sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(response -> gson.fromJson(response.body(), returnType)); - } - - /** - * Helper method to build form URL-encoded data from a Map - */ - private HttpRequest.BodyPublisher buildFormDataFromMap(Map data) { - return HttpRequest.BodyPublishers.ofString( - data.entrySet().stream() - .map(entry -> entry.getKey() + "=" + entry.getValue()) - .reduce((param1, param2) -> param1 + "&" + param2) - .orElse("")); - } - - /** - * Make a GET request to the Tribufu API and deserialize the response JSON. - * - * @param path The path of the resource. - * @param headers Optional headers. - * @param returnType The type to deserialize the response into. - * @return A CompletableFuture containing the deserialized response object. - */ - public CompletableFuture get(String path, Map headers, Class returnType) { - var requestBuilder = HttpRequest.newBuilder(); - - for (var entry : headers.entrySet()) { - requestBuilder.header(entry.getKey(), entry.getValue()); - } - - var request = requestBuilder - .uri(URI.create(this.options.baseUrl + path)) - .GET() - .build(); - - return sendRequest(request, returnType); - } - - /** - * Make a GET request to the Tribufu API and deserialize the response JSON. - * - * @param path The path of the resource. - * @param headers Optional headers. - * @param returnType The type to deserialize the response into. - * @return A CompletableFuture containing the deserialized response object. - */ - public List getArray(String path, Map headers, Class returnType) { - var requestBuilder = HttpRequest.newBuilder(); - - for (var entry : headers.entrySet()) { - requestBuilder.header(entry.getKey(), entry.getValue()); - } - - var request = requestBuilder - .uri(URI.create(this.options.baseUrl + path)) - .GET() - .build(); - - var response = sendRequest(request); - T[] array = new Gson().fromJson(response.join(), returnType); - - return Arrays.asList(array); - } - - /** - * Make a POST request to the Tribufu API and deserialize the response JSON. - * - * @param path The path of the resource. - * @param body The request body. - * @param headers Optional headers. - * @param returnType The type to deserialize the response into. - * @return A CompletableFuture containing the deserialized response object. - */ - public CompletableFuture post(String path, S body, Map headers, Class returnType) { - var requestBuilder = HttpRequest.newBuilder(); - - for (var entry : headers.entrySet()) { - requestBuilder.header(entry.getKey(), entry.getValue()); - } - - var request = requestBuilder - .uri(URI.create(this.options.baseUrl + path)) - .POST(HttpRequest.BodyPublishers.ofString(gson.toJson(body))) - .build(); - - return sendRequest(request, returnType); - } - - /** - * Make a POST request to the Tribufu API with form URL-encoded data - * and deserialize the response JSON. - * - * @param path The path of the resource. - * @param formData The form data as a Map. - * @param headers Optional headers. - * @param returnType The type to deserialize the response into. - * @return A CompletableFuture containing the deserialized response object. - */ - public CompletableFuture postFormUrlEncoded(String path, Map formData, - Map headers, Class returnType) { - var requestBuilder = HttpRequest.newBuilder(); - - for (var entry : headers.entrySet()) { - requestBuilder.header(entry.getKey(), entry.getValue()); - } - - var request = requestBuilder - .uri(URI.create(this.options.baseUrl + path)) - .POST(buildFormDataFromMap(formData)) - .build(); - - return sendRequest(request, returnType); - } - - /** - * Make a PUT request to the Tribufu API and deserialize the response JSON. - * - * @param path The path of the resource. - * @param body The request body. - * @param headers Optional headers. - * @param returnType The type to deserialize the response into. - * @return A CompletableFuture containing the deserialized response object. - */ - public CompletableFuture put(String path, S body, Map headers, Class returnType) { - var requestBuilder = HttpRequest.newBuilder(); - - for (var entry : headers.entrySet()) { - requestBuilder.header(entry.getKey(), entry.getValue()); - } - - var request = requestBuilder - .uri(URI.create(this.options.baseUrl + path)) - .PUT(HttpRequest.BodyPublishers.ofString(gson.toJson(body))) - .build(); - - return sendRequest(request, returnType); - } - - /** - * Make a DELETE request to the Tribufu API. - * - * @param path The path of the resource. - * @param headers Optional headers. - * @return A CompletableFuture containing the response body as a String. - */ - public CompletableFuture delete(String path, Map headers) { - var requestBuilder = HttpRequest.newBuilder(); - - for (var entry : headers.entrySet()) { - requestBuilder.header(entry.getKey(), entry.getValue()); - } - - var request = requestBuilder - .uri(URI.create(this.options.baseUrl + path)) - .DELETE() - .build(); - - return sendRequest(request); - } -} diff --git a/lib/src/main/java/com/tribufu/http/TribufuHttpOptions.java b/lib/src/main/java/com/tribufu/http/TribufuHttpOptions.java deleted file mode 100644 index 83be6d9..0000000 --- a/lib/src/main/java/com/tribufu/http/TribufuHttpOptions.java +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.http; - -import java.util.HashMap; -import java.util.Map; - -public class TribufuHttpOptions { - public String baseUrl; - public Map headers; - public boolean logEnabled; - public String logTarget; - - public TribufuHttpOptions() { - this(null); - } - - public TribufuHttpOptions(String baseUrl) { - this(baseUrl, new HashMap<>(), false, "TribufuHttp"); - } - - public TribufuHttpOptions(String baseUrl, Map headers, boolean logEnabled, String logTarget) { - this.baseUrl = baseUrl; - this.headers = headers; - this.logEnabled = logEnabled; - this.logTarget = logTarget; - } -} diff --git a/lib/src/main/java/com/tribufu/oauth2/OAuth2AuthorizeRequest.java b/lib/src/main/java/com/tribufu/oauth2/OAuth2AuthorizeRequest.java deleted file mode 100644 index 26967dd..0000000 --- a/lib/src/main/java/com/tribufu/oauth2/OAuth2AuthorizeRequest.java +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.oauth2; - -public class OAuth2AuthorizeRequest { - public final OAuth2ResponseType responseType; - public final String clientId; - public final String scope; - public final String redirectUri; - public final String state; - - public OAuth2AuthorizeRequest(OAuth2ResponseType responseType, String clientId, String scope, String redirectUri, - String state) { - this.responseType = responseType; - this.clientId = clientId; - this.scope = scope; - this.redirectUri = redirectUri; - this.state = state; - } -} diff --git a/lib/src/main/java/com/tribufu/oauth2/OAuth2CodeResponse.java b/lib/src/main/java/com/tribufu/oauth2/OAuth2CodeResponse.java deleted file mode 100644 index c7a0241..0000000 --- a/lib/src/main/java/com/tribufu/oauth2/OAuth2CodeResponse.java +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.oauth2; - -public class OAuth2CodeResponse { - public final String code; - public final String state; - - public OAuth2CodeResponse(String code, String state) { - this.code = code; - this.state = state; - } -} diff --git a/lib/src/main/java/com/tribufu/oauth2/OAuth2GrantType.java b/lib/src/main/java/com/tribufu/oauth2/OAuth2GrantType.java deleted file mode 100644 index eb1cb8c..0000000 --- a/lib/src/main/java/com/tribufu/oauth2/OAuth2GrantType.java +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.oauth2; - -public enum OAuth2GrantType { - AuthorizationCode, - RefreshToken, - Password, - ClientCredentials, - DeviceCode, -} diff --git a/lib/src/main/java/com/tribufu/oauth2/OAuth2ResponseType.java b/lib/src/main/java/com/tribufu/oauth2/OAuth2ResponseType.java deleted file mode 100644 index 6309cad..0000000 --- a/lib/src/main/java/com/tribufu/oauth2/OAuth2ResponseType.java +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.oauth2; - -public enum OAuth2ResponseType { - Code, - Token, -} diff --git a/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenRequest.java b/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenRequest.java deleted file mode 100644 index 045907f..0000000 --- a/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenRequest.java +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.oauth2; - -public class OAuth2TokenRequest { - public final OAuth2GrantType grantType; - public final String code; - public final String refreshToken; - public final String username; - public final String password; - public final String clientId; - public final String clientSecret; - public final String redirectUri; - - public OAuth2TokenRequest(OAuth2GrantType grantType, String code, String refreshToken, String username, - String password, String clientId, String clientSecret, String redirectUri) { - this.grantType = grantType; - this.code = code; - this.refreshToken = refreshToken; - this.username = username; - this.password = password; - this.clientId = clientId; - this.clientSecret = clientSecret; - this.redirectUri = redirectUri; - } -} diff --git a/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenResponse.java b/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenResponse.java deleted file mode 100644 index 3e452f2..0000000 --- a/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenResponse.java +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.oauth2; - -public class OAuth2TokenResponse { - public final OAuth2TokenType tokenType; - public final String accessToken; - public final String refreshToken; - public final String scope; - public final int expiresIn; - - public OAuth2TokenResponse(OAuth2TokenType tokenType, String accessToken, String refreshToken, String scope, - int expiresIn) { - this.tokenType = tokenType; - this.accessToken = accessToken; - this.refreshToken = refreshToken; - this.scope = scope; - this.expiresIn = expiresIn; - } -} diff --git a/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenType.java b/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenType.java deleted file mode 100644 index 4a7321c..0000000 --- a/lib/src/main/java/com/tribufu/oauth2/OAuth2TokenType.java +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (c) Tribufu. All Rights Reserved. - -package com.tribufu.oauth2; - -public enum OAuth2TokenType { - Bearer, -} diff --git a/scripts/build.ps1 b/scripts/build.ps1 new file mode 100644 index 0000000..ca61285 --- /dev/null +++ b/scripts/build.ps1 @@ -0,0 +1,8 @@ +#!/usr/bin/env pwsh + +.\gradlew build + +New-Item "build/libs" -ItemType Directory -Force +Remove-Item -Path "build/libs/*" -Force +Copy-Item -Path "./lib/build/libs/*.jar" -Destination "./build/libs" -Force +Copy-Item -Path "./vendor/MintakaJvm/lib/build/libs/*.jar" -Destination "./build/libs" -Force diff --git a/settings.gradle b/settings.gradle index 901c9f6..e488104 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,3 +5,6 @@ plugins { rootProject.name = 'TribufuJvm' include('lib') include('examples') + +include 'MintakaJvm' +project(':MintakaJvm').projectDir = file('vendor/MintakaJvm/lib') diff --git a/vendor/MintakaJvm b/vendor/MintakaJvm new file mode 160000 index 0000000..9fbb227 --- /dev/null +++ b/vendor/MintakaJvm @@ -0,0 +1 @@ +Subproject commit 9fbb227fec3a0feef978b2bd3ee86f3b3771f2e5