Skip to content

Commit 6b50884

Browse files
committed
feat: Add support for raw endpoints (without component paths)
1 parent 9be8b4e commit 6b50884

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

src/main/java/com/godiddy/cli/clistorage/cliconfig/CLIConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public static void setEndpoint(String endpoint) {
3131
}
3232
}
3333

34+
public static Boolean getEndpointRaw() {
35+
return CLIStorage.get("endpointRaw") == null ? null : Boolean.parseBoolean(CLIStorage.get("endpointRaw"));
36+
}
37+
38+
public static void setEndpointRaw(Boolean endpointRaw) {
39+
if (endpointRaw == null) {
40+
CLIStorage.remove("endpointRaw");
41+
} else {
42+
CLIStorage.put("endpointRaw", endpointRaw.toString());
43+
}
44+
}
45+
3446
public static Kms.Value getKms() {
3547
return CLIStorage.get("kms") == null ? null : Kms.Value.valueOf(CLIStorage.get("kms"));
3648
}

src/main/java/com/godiddy/cli/commands/config/ConfigEndpointCommand.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,27 @@ public class ConfigEndpointCommand extends ConfigAbstractCommand implements Call
2424
)
2525
String endpoint;
2626

27+
@CommandLine.Option(
28+
names = {"-r", "--raw"},
29+
description = "Whether or not to use raw endpoints that omit the component paths, e.g. /universal-resolver/, /universal-registrar/."
30+
)
31+
Boolean endpointRaw;
32+
2733
@Override
2834
public Integer call() throws Exception {
29-
log.trace("Parameter 'endpoint': " + this.endpoint);
35+
log.trace("Parameter 'endpoint': " + this.endpoint + ", 'endpointRaw': " + this.endpointRaw);
3036
if (Boolean.TRUE.equals(this.delete)) {
3137
CLIConfig.setEndpoint(null);
38+
CLIConfig.setEndpointRaw(null);
3239
System.out.println("Endpoint setting successfully deleted.");
3340
} else {
3441
if (this.endpoint == null) {
3542
String endpoint = CLIConfig.getEndpoint();
43+
Boolean endpointRaw = CLIConfig.getEndpointRaw();
3644
if (endpoint == null) {
3745
System.out.println("No endpoint set.");
3846
} else {
39-
System.out.println("Endpoint: " + endpoint);
47+
System.out.println("Endpoint: " + endpoint + " (raw=" + endpointRaw + ")");
4048
}
4149
} else {
4250
String endpoint = this.endpoint;
@@ -46,7 +54,13 @@ public Integer call() throws Exception {
4654
endpoint = predefinedEndpoint;
4755
}
4856
CLIConfig.setEndpoint(endpoint);
49-
System.out.println("Endpoint successfully set: " + endpoint);
57+
if (this.endpointRaw == null) {
58+
System.out.println("Endpoint successfully set: " + endpoint);
59+
} else {
60+
Boolean endpointRaw = this.endpointRaw;
61+
CLIConfig.setEndpointRaw(endpointRaw);
62+
System.out.println("Endpoint successfully set: " + endpoint + " (raw=" + endpointRaw + ")");
63+
}
5064
}
5165
}
5266
return 0;

src/main/java/com/godiddy/cli/config/Api.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import uniregistrar.openapi.RFC3339DateFormat;
2020

2121
import java.io.*;
22+
import java.net.URI;
2223
import java.net.http.HttpHeaders;
2324
import java.net.http.HttpRequest;
2425
import java.net.http.HttpResponse;
@@ -48,9 +49,9 @@ public class Api {
4849

4950
public static ApiClient apiClient() {
5051
ApiClient apiClient = new ApiClient();
51-
String endpoint = Endpoint.getEndpoint();
52-
if (endpoint.endsWith("/")) endpoint = endpoint.substring(0, endpoint.length()-1);
53-
apiClient.updateBaseUri(endpoint);
52+
String endpointWithoutSlash = Endpoint.getEndpoint();
53+
if (endpointWithoutSlash.endsWith("/")) endpointWithoutSlash = endpointWithoutSlash.substring(0, endpointWithoutSlash.length()-1);
54+
apiClient.updateBaseUri(endpointWithoutSlash);
5455
apiClient.setRequestInterceptor(requestInterceptor);
5556
apiClient.setResponseInterceptor(responseInterceptor);
5657
return apiClient;
@@ -105,8 +106,20 @@ public void onComplete() { }
105106
};
106107

107108
private static final Consumer<HttpRequest.Builder> requestInterceptor = builder -> {
109+
Boolean endpointRaw = Endpoint.getEndpointRaw();
110+
if (Boolean.TRUE.equals(endpointRaw)) {
111+
String endpointWithSlash = Endpoint.getEndpoint();
112+
if (! endpointWithSlash.endsWith("/")) endpointWithSlash += "/";
113+
String httpRequestUri = builder.build().uri().toString();
114+
String httpRequestRelativeUri = httpRequestUri.substring(endpointWithSlash.length());
115+
httpRequestRelativeUri = httpRequestRelativeUri.substring(httpRequestRelativeUri.indexOf("/")+1);
116+
httpRequestUri = endpointWithSlash + httpRequestRelativeUri;
117+
builder.uri(URI.create(httpRequestUri));
118+
}
108119
String apiKey = ApiKey.getApiKey();
109-
builder.header("Authorization", "Bearer " + apiKey);
120+
if (apiKey != null && ! apiKey.isBlank()) {
121+
builder.header("Authorization", "Bearer " + apiKey);
122+
}
110123
HttpRequest httpRequest = builder.build();
111124
System.out.println();
112125
System.out.println(">>> " + httpRequest.method() + " " + httpRequest.uri());

src/main/java/com/godiddy/cli/config/Endpoint.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ public class Endpoint {
1919
"godiddy-dev", "https://api.dev.godiddy.com/1.0.0/",
2020
"dif-universalresolver", "https://dev.uniresolver.io/1.0/",
2121
"dif-universalregistrar", "https://uniregistrar.io/1.0/",
22-
"local-universalresolver", "http://localhost:8080/1.0",
23-
"local-universalregistrar", "http://localhost:9090/1.0",
22+
"local-universalresolver", "http://localhost:8080/1.0/",
23+
"local-universalregistrar", "http://localhost:9080/1.0/",
2424
"local-businesswallet", "http://localhost:21080/1.0.0/"
2525
);
2626

27+
public static final Boolean DEFAULT_ENDPOINTRAW = Boolean.FALSE;
28+
2729
public static String getEndpoint() {
2830
String endpoint = Objects.requireNonNullElse(CLIConfig.getEndpoint(), DEFAULT_ENDPOINT);
2931
if (DEFAULT_ENDPOINT.equals(endpoint)) {
3032
log.debug("Using default endpoint: " + DEFAULT_ENDPOINT);
3133
}
3234
return endpoint;
3335
}
36+
37+
public static Boolean getEndpointRaw() {
38+
Boolean endpointRaw = Objects.requireNonNullElse(CLIConfig.getEndpointRaw(), DEFAULT_ENDPOINTRAW);
39+
return endpointRaw;
40+
}
3441
}

0 commit comments

Comments
 (0)