Skip to content

Commit 635da70

Browse files
authored
[client] Support Static URLs (#623)
* [client] allow blank baseUrl * Update ApiClient.java * support static http endpoint
1 parent 0a2c5b4 commit 635da70

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

http-client/src/main/java/io/avaje/http/client/DHttpClientBuilder.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package io.avaje.http.client;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import io.avaje.inject.BeanScope;
5-
import io.avaje.jsonb.Jsonb;
6-
7-
import javax.net.ssl.SSLContext;
8-
import javax.net.ssl.SSLParameters;
3+
import java.lang.invoke.MethodHandles;
4+
import java.lang.invoke.MethodType;
95
import java.net.Authenticator;
106
import java.net.CookieHandler;
117
import java.net.CookieManager;
@@ -21,15 +17,18 @@
2117
import java.util.concurrent.Executors;
2218
import java.util.function.Function;
2319

24-
import static java.util.Objects.requireNonNull;
20+
import javax.net.ssl.SSLContext;
21+
import javax.net.ssl.SSLParameters;
2522

26-
import java.lang.invoke.MethodHandles;
27-
import java.lang.invoke.MethodType;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
25+
import io.avaje.inject.BeanScope;
26+
import io.avaje.jsonb.Jsonb;
2827

2928
final class DHttpClientBuilder implements HttpClient.Builder, HttpClient.Builder.State {
3029

3130
private java.net.http.HttpClient client;
32-
private String baseUrl;
31+
private String baseUrl = "";
3332
private boolean requestLogging = true;
3433
private Duration connectionTimeout = Duration.ofSeconds(20);
3534
private Duration requestTimeout = Duration.ofSeconds(20);
@@ -168,8 +167,6 @@ private boolean detectTypeExists(String className) {
168167
}
169168

170169
private DHttpClientContext buildClient() {
171-
requireNonNull(baseUrl, "baseUrl is not specified");
172-
requireNonNull(requestTimeout, "requestTimeout is not specified");
173170
final var httpClient = client != null ? client : defaultClient();
174171
if (requestLogging) {
175172
// register the built-in request/response logging

http-client/src/test/java/org/example/dinject/ConfigureWithDITest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void configureWith() {
2020

2121
HttpClient.Builder builder = HttpClient.builder();
2222
HttpClient.Builder.State state = builder.state();
23-
assertThat(state.baseUrl()).isNull();
23+
assertThat(state.baseUrl()).isEmpty();
2424
assertThat(state.bodyAdapter()).isNull();
2525
assertThat(state.client()).isNull();
2626
assertThat(state.requestLogging()).isTrue();

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ private void writePaths(Set<PathSegments.Segment> segments) {
427427
} else {
428428
// If we have accumulated literals, write them out first
429429
if (combinedLiterals.length() > 0) {
430-
writer.append(".path(\"").append(combinedLiterals.toString()).append("\")");
430+
writeLiteral(combinedLiterals);
431431
combinedLiterals.setLength(0); // Clear the buffer
432432
}
433433
// Write the non-literal segment
@@ -441,14 +441,23 @@ private void writePaths(Set<PathSegments.Segment> segments) {
441441

442442
// Write any remaining accumulated literals
443443
if (combinedLiterals.length() > 0) {
444-
writer.append(".path(\"").append(combinedLiterals.toString()).append("\")");
444+
writeLiteral(combinedLiterals);
445445
}
446446

447447
if (!segments.isEmpty()) {
448448
writer.eol();
449449
}
450450
}
451451

452+
private void writeLiteral(StringBuilder combinedLiterals) {
453+
String path =
454+
combinedLiterals.toString().replace("http:/", "http://").replace("https:/", "https://");
455+
writer
456+
.append(path.startsWith("http:") || path.startsWith("https:") ? ".url(\"" : ".path(\"")
457+
.append(path)
458+
.append("\")");
459+
}
460+
452461
private boolean isMap(MethodParam param) {
453462
return isMap(param.utype().mainType());
454463
}

http-generator-client/src/test/java/io/avaje/http/generator/client/clients/ApiClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ public interface ApiClient {
1515

1616
@Get("/consecutive/paths/{accept}/generate")
1717
String consecutive(String accept);
18+
19+
@Get("http://localhost:6969/test")
20+
String staticUrl();
1821
}

http-generator-core/src/main/java/io/avaje/http/generator/core/Util.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ static String combinePath(String beanPath, String webMethodPath) {
9898
if (!webMethodPath.isEmpty() && !webMethodPath.startsWith("/")) {
9999
sb.append("/");
100100
}
101+
102+
if (webMethodPath.startsWith("https:") || webMethodPath.startsWith("http:")) {
103+
return webMethodPath;
104+
}
101105
sb.append(trimTrailingSlash(webMethodPath));
102106
}
103107
return sb.toString();

0 commit comments

Comments
 (0)