|
| 1 | +package dev.axme.sdk; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.URI; |
| 7 | +import java.net.URLEncoder; |
| 8 | +import java.net.http.HttpClient; |
| 9 | +import java.net.http.HttpRequest; |
| 10 | +import java.net.http.HttpResponse; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public final class AxmeClient { |
| 16 | + private final String baseUrl; |
| 17 | + private final String apiKey; |
| 18 | + private final HttpClient httpClient; |
| 19 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 20 | + |
| 21 | + public AxmeClient(AxmeClientConfig config) { |
| 22 | + this(config, HttpClient.newHttpClient()); |
| 23 | + } |
| 24 | + |
| 25 | + public AxmeClient(AxmeClientConfig config, HttpClient httpClient) { |
| 26 | + this.baseUrl = config.getBaseUrl(); |
| 27 | + this.apiKey = config.getApiKey(); |
| 28 | + this.httpClient = httpClient; |
| 29 | + } |
| 30 | + |
| 31 | + public Map<String, Object> registerNick(Map<String, Object> payload, RequestOptions options) |
| 32 | + throws IOException, InterruptedException { |
| 33 | + return requestJson("POST", "/v1/users/register-nick", Map.of(), payload, normalizeOptions(options)); |
| 34 | + } |
| 35 | + |
| 36 | + public Map<String, Object> checkNick(String nick, RequestOptions options) |
| 37 | + throws IOException, InterruptedException { |
| 38 | + return requestJson("GET", "/v1/users/check-nick", Map.of("nick", nick), null, normalizeOptions(options)); |
| 39 | + } |
| 40 | + |
| 41 | + public Map<String, Object> renameNick(Map<String, Object> payload, RequestOptions options) |
| 42 | + throws IOException, InterruptedException { |
| 43 | + return requestJson("POST", "/v1/users/rename-nick", Map.of(), payload, normalizeOptions(options)); |
| 44 | + } |
| 45 | + |
| 46 | + public Map<String, Object> getUserProfile(String ownerAgent, RequestOptions options) |
| 47 | + throws IOException, InterruptedException { |
| 48 | + return requestJson( |
| 49 | + "GET", |
| 50 | + "/v1/users/profile", |
| 51 | + Map.of("owner_agent", ownerAgent), |
| 52 | + null, |
| 53 | + normalizeOptions(options)); |
| 54 | + } |
| 55 | + |
| 56 | + public Map<String, Object> updateUserProfile(Map<String, Object> payload, RequestOptions options) |
| 57 | + throws IOException, InterruptedException { |
| 58 | + return requestJson("POST", "/v1/users/profile/update", Map.of(), payload, normalizeOptions(options)); |
| 59 | + } |
| 60 | + |
| 61 | + private Map<String, Object> requestJson( |
| 62 | + String method, |
| 63 | + String path, |
| 64 | + Map<String, String> query, |
| 65 | + Map<String, Object> payload, |
| 66 | + RequestOptions options) |
| 67 | + throws IOException, InterruptedException { |
| 68 | + HttpRequest.Builder builder = |
| 69 | + HttpRequest.newBuilder() |
| 70 | + .uri(URI.create(buildUrl(path, query))) |
| 71 | + .method(method, payload == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(payload))) |
| 72 | + .header("Authorization", "Bearer " + apiKey) |
| 73 | + .header("Accept", "application/json"); |
| 74 | + |
| 75 | + if (payload != null) { |
| 76 | + builder.header("Content-Type", "application/json"); |
| 77 | + } |
| 78 | + if (!isBlank(options.getIdempotencyKey())) { |
| 79 | + builder.header("Idempotency-Key", options.getIdempotencyKey()); |
| 80 | + } |
| 81 | + if (!isBlank(options.getTraceId())) { |
| 82 | + builder.header("X-Trace-Id", options.getTraceId()); |
| 83 | + } |
| 84 | + |
| 85 | + HttpResponse<String> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString()); |
| 86 | + if (response.statusCode() < 200 || response.statusCode() >= 300) { |
| 87 | + throw new AxmeHttpException(response.statusCode(), response.body()); |
| 88 | + } |
| 89 | + if (response.body() == null || response.body().trim().isEmpty()) { |
| 90 | + return Map.of(); |
| 91 | + } |
| 92 | + |
| 93 | + return objectMapper.readValue(response.body(), new TypeReference<Map<String, Object>>() {}); |
| 94 | + } |
| 95 | + |
| 96 | + private String buildUrl(String path, Map<String, String> query) { |
| 97 | + if (query == null || query.isEmpty()) { |
| 98 | + return baseUrl + path; |
| 99 | + } |
| 100 | + |
| 101 | + Map<String, String> filtered = new LinkedHashMap<>(); |
| 102 | + for (Map.Entry<String, String> entry : query.entrySet()) { |
| 103 | + if (!isBlank(entry.getValue())) { |
| 104 | + filtered.put(entry.getKey(), entry.getValue()); |
| 105 | + } |
| 106 | + } |
| 107 | + if (filtered.isEmpty()) { |
| 108 | + return baseUrl + path; |
| 109 | + } |
| 110 | + |
| 111 | + StringBuilder builder = new StringBuilder(baseUrl).append(path).append("?"); |
| 112 | + boolean first = true; |
| 113 | + for (Map.Entry<String, String> entry : filtered.entrySet()) { |
| 114 | + if (!first) { |
| 115 | + builder.append("&"); |
| 116 | + } |
| 117 | + first = false; |
| 118 | + builder |
| 119 | + .append(urlEncode(entry.getKey())) |
| 120 | + .append("=") |
| 121 | + .append(urlEncode(entry.getValue())); |
| 122 | + } |
| 123 | + return builder.toString(); |
| 124 | + } |
| 125 | + |
| 126 | + private static String urlEncode(String value) { |
| 127 | + return URLEncoder.encode(value, StandardCharsets.UTF_8); |
| 128 | + } |
| 129 | + |
| 130 | + private static RequestOptions normalizeOptions(RequestOptions options) { |
| 131 | + return options == null ? RequestOptions.none() : options; |
| 132 | + } |
| 133 | + |
| 134 | + private static boolean isBlank(String value) { |
| 135 | + return value == null || value.trim().isEmpty(); |
| 136 | + } |
| 137 | +} |
0 commit comments