helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.44k stars 562 forks source link

4.x: Add io.helidon.webclient.api.HttpClient#baseURI method #8918

Closed jbescos closed 4 days ago

jbescos commented 5 days ago

Environment Details


Problem Description

HttpClient needs to be instanced with a pre-configured base URI: HttpClient<?> httpClient = Http1Client.builder().baseUri(uri).build();

This is different approach than jakarta.ws.rs.client.Client, where you are able to create the client without specifying the base URI: Client client = ClientBuilder.newClient()

This forces you to create a new HttpClient for every different base URI, and this brings some usability issues. For example, imagine you have a class that makes HTTP requests to a bank URI. This class, lets say BankClient, knows the base URI of the bank. Problems:

  1. If you want to pass an existing HttpClient in this class, you need to set the base URI in in advance (note the URI is known by BankClient and you are forcing the users of this class to know this URI).
  2. Or in case you don't pass the existing HttpClient, and you let the BankClient to create it, there are other problems:
    • You cannot mock the client for testing
    • You cannot have multiple BankClient instances for the same HttpClient instance.
    • BankClient will need to care about closing the HttpClient.
    • BankClient will need to allow configuration of the HttpClient, like proxies, etc.

A solution could be that HttpClient is a wrapper of multiple HttpClient per URI.

I noticed this issue while I was working on https://github.com/helidon-io/helidon/issues/8909 . There, I have to create a class that speaks with OpenAI.

Steps to reproduce

jbescos commented 4 days ago

Closing because you can specify the full URL in httpClient.post(uri.toString())