Skip to content

[client] Support Static URLs #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package io.avaje.http.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.avaje.inject.BeanScope;
import io.avaje.jsonb.Jsonb;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.CookieManager;
Expand All @@ -21,15 +17,18 @@
import java.util.concurrent.Executors;
import java.util.function.Function;

import static java.util.Objects.requireNonNull;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.avaje.inject.BeanScope;
import io.avaje.jsonb.Jsonb;

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

private java.net.http.HttpClient client;
private String baseUrl;
private String baseUrl = "";
private boolean requestLogging = true;
private Duration connectionTimeout = Duration.ofSeconds(20);
private Duration requestTimeout = Duration.ofSeconds(20);
Expand Down Expand Up @@ -168,8 +167,6 @@ private boolean detectTypeExists(String className) {
}

private DHttpClientContext buildClient() {
requireNonNull(baseUrl, "baseUrl is not specified");
requireNonNull(requestTimeout, "requestTimeout is not specified");
final var httpClient = client != null ? client : defaultClient();
if (requestLogging) {
// register the built-in request/response logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void configureWith() {

HttpClient.Builder builder = HttpClient.builder();
HttpClient.Builder.State state = builder.state();
assertThat(state.baseUrl()).isNull();
assertThat(state.baseUrl()).isEmpty();
assertThat(state.bodyAdapter()).isNull();
assertThat(state.client()).isNull();
assertThat(state.requestLogging()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ private void writePaths(Set<PathSegments.Segment> segments) {
} else {
// If we have accumulated literals, write them out first
if (combinedLiterals.length() > 0) {
writer.append(".path(\"").append(combinedLiterals.toString()).append("\")");
writeLiteral(combinedLiterals);
combinedLiterals.setLength(0); // Clear the buffer
}
// Write the non-literal segment
Expand All @@ -441,14 +441,23 @@ private void writePaths(Set<PathSegments.Segment> segments) {

// Write any remaining accumulated literals
if (combinedLiterals.length() > 0) {
writer.append(".path(\"").append(combinedLiterals.toString()).append("\")");
writeLiteral(combinedLiterals);
}

if (!segments.isEmpty()) {
writer.eol();
}
}

private void writeLiteral(StringBuilder combinedLiterals) {
String path =
combinedLiterals.toString().replace("http:/", "http://").replace("https:/", "https://");
writer
.append(path.startsWith("http:") || path.startsWith("https:") ? ".url(\"" : ".path(\"")
.append(path)
.append("\")");
}

private boolean isMap(MethodParam param) {
return isMap(param.utype().mainType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface ApiClient {

@Get("/consecutive/paths/{accept}/generate")
String consecutive(String accept);

@Get("http://localhost:6969/test")
String staticUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ static String combinePath(String beanPath, String webMethodPath) {
if (!webMethodPath.isEmpty() && !webMethodPath.startsWith("/")) {
sb.append("/");
}

if (webMethodPath.startsWith("https:") || webMethodPath.startsWith("http:")) {
return webMethodPath;
}
sb.append(trimTrailingSlash(webMethodPath));
}
return sb.toString();
Expand Down