mirror of
https://github.com/tribufu/tribufu-java
synced 2026-06-01 09:42:37 +00:00
Inline mintaka library classes
This commit is contained in:
parent
1839b59589
commit
02969aa0db
12 changed files with 364 additions and 3 deletions
|
|
@ -14,7 +14,6 @@ dependencies {
|
||||||
api "org.apache.commons:commons-math3:3.6.1"
|
api "org.apache.commons:commons-math3:3.6.1"
|
||||||
implementation "com.google.code.gson:gson:2.8.9"
|
implementation "com.google.code.gson:gson:2.8.9"
|
||||||
implementation "com.google.guava:guava:31.1-jre"
|
implementation "com.google.guava:guava:31.1-jre"
|
||||||
implementation "dev.mintaka:Mintaka:0.1.0"
|
|
||||||
testImplementation "org.junit.jupiter:junit-jupiter:5.9.1"
|
testImplementation "org.junit.jupiter:junit-jupiter:5.9.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ package com.tribufu;
|
||||||
import com.tribufu.types.games.Game;
|
import com.tribufu.types.games.Game;
|
||||||
import com.tribufu.types.servers.Server;
|
import com.tribufu.types.servers.Server;
|
||||||
import com.tribufu.types.users.Profile;
|
import com.tribufu.types.users.Profile;
|
||||||
import dev.mintaka.http.HttpClient;
|
import com.tribufu.http.HttpClient;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
|
||||||
225
lib/src/main/java/com/tribufu/http/HttpClient.java
Normal file
225
lib/src/main/java/com/tribufu/http/HttpClient.java
Normal file
|
|
@ -0,0 +1,225 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
package com.tribufu.http;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Http Client
|
||||||
|
*
|
||||||
|
* Helper class to make HTTP requests.
|
||||||
|
*/
|
||||||
|
public class HttpClient {
|
||||||
|
private static final Gson gson = new GsonBuilder().create();
|
||||||
|
|
||||||
|
private final java.net.http.HttpClient inner;
|
||||||
|
protected final HttpClientOptions options;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a default HttpClient instance.
|
||||||
|
*/
|
||||||
|
public HttpClient() {
|
||||||
|
this(new HttpClientOptions());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a HttpClient instance with the specified options.
|
||||||
|
*
|
||||||
|
* @param baseUrl
|
||||||
|
* @param headers
|
||||||
|
* @param logEnabled
|
||||||
|
* @param logTarget
|
||||||
|
*/
|
||||||
|
public HttpClient(String baseUrl, Map<String, String> headers, boolean logEnabled, String logTarget) {
|
||||||
|
this(new HttpClientOptions(baseUrl, headers, logEnabled, logTarget));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a HttpClient instance with the specified options.
|
||||||
|
*
|
||||||
|
* @param options
|
||||||
|
*/
|
||||||
|
public HttpClient(HttpClientOptions options) {
|
||||||
|
this.options = options;
|
||||||
|
this.inner = java.net.http.HttpClient.newBuilder().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to send an asynchronous HTTP request.
|
||||||
|
*/
|
||||||
|
private CompletableFuture<String> sendRequest(HttpRequest request) {
|
||||||
|
return this.inner.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to send an asynchronous HTTP request.
|
||||||
|
*/
|
||||||
|
private <T> CompletableFuture<T> sendRequest(HttpRequest request, Class<T> 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<String, String> data) {
|
||||||
|
return HttpRequest.BodyPublishers.ofString(
|
||||||
|
data.entrySet().stream()
|
||||||
|
.map(entry -> entry.getKey() + "=" + entry.getValue())
|
||||||
|
.reduce((param1, param2) -> param1 + "&" + param2)
|
||||||
|
.orElse(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a GET request 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 <T> CompletableFuture<T> get(String path, Map<String, String> headers, Class<T> 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 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 <T> List<T> getArray(String path, Map<String, String> headers, Class<T[]> 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 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 <S, T> CompletableFuture<T> post(String path, S body, Map<String, String> headers, Class<T> 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 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 <T> CompletableFuture<T> postFormUrlEncoded(String path, Map<String, String> formData,
|
||||||
|
Map<String, String> headers, Class<T> 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 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 <S, T> CompletableFuture<T> put(String path, S body, Map<String, String> headers, Class<T> 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.
|
||||||
|
*
|
||||||
|
* @param path The path of the resource.
|
||||||
|
* @param headers Optional headers.
|
||||||
|
* @return A CompletableFuture containing the response body as a String.
|
||||||
|
*/
|
||||||
|
public CompletableFuture<String> delete(String path, Map<String, String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
lib/src/main/java/com/tribufu/http/HttpClientOptions.java
Normal file
33
lib/src/main/java/com/tribufu/http/HttpClientOptions.java
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
package com.tribufu.http;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Http Client Options
|
||||||
|
*
|
||||||
|
* Options for creating a HttpClient instance.
|
||||||
|
*/
|
||||||
|
public class HttpClientOptions {
|
||||||
|
public String baseUrl;
|
||||||
|
public Map<String, String> headers;
|
||||||
|
public boolean logEnabled;
|
||||||
|
public String logTarget;
|
||||||
|
|
||||||
|
public HttpClientOptions() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpClientOptions(String baseUrl) {
|
||||||
|
this(baseUrl, new HashMap<>(), false, "HttpClient");
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpClientOptions(String baseUrl, Map<String, String> headers, boolean logEnabled, String logTarget) {
|
||||||
|
this.baseUrl = baseUrl;
|
||||||
|
this.headers = headers;
|
||||||
|
this.logEnabled = logEnabled;
|
||||||
|
this.logTarget = logTarget;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
lib/src/main/java/com/tribufu/oauth2/OAuth2CodeResponse.java
Normal file
13
lib/src/main/java/com/tribufu/oauth2/OAuth2CodeResponse.java
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
lib/src/main/java/com/tribufu/oauth2/OAuth2GrantType.java
Normal file
11
lib/src/main/java/com/tribufu/oauth2/OAuth2GrantType.java
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
package com.tribufu.oauth2;
|
||||||
|
|
||||||
|
public enum OAuth2GrantType {
|
||||||
|
AuthorizationCode,
|
||||||
|
RefreshToken,
|
||||||
|
Password,
|
||||||
|
ClientCredentials,
|
||||||
|
DeviceCode,
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
package com.tribufu.oauth2;
|
||||||
|
|
||||||
|
public enum OAuth2ResponseType {
|
||||||
|
Code,
|
||||||
|
Token,
|
||||||
|
}
|
||||||
26
lib/src/main/java/com/tribufu/oauth2/OAuth2TokenRequest.java
Normal file
26
lib/src/main/java/com/tribufu/oauth2/OAuth2TokenRequest.java
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
package com.tribufu.oauth2;
|
||||||
|
|
||||||
|
public enum OAuth2TokenType {
|
||||||
|
Bearer,
|
||||||
|
}
|
||||||
1
vendor/MintakaJvm
vendored
1
vendor/MintakaJvm
vendored
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit 9fbb227fec3a0feef978b2bd3ee86f3b3771f2e5
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue