43 lines
1.5 KiB
Java
43 lines
1.5 KiB
Java
package builder;
|
|
|
|
public class Main {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// Simple GET — only the required field
|
|
HttpRequest ping = new HttpRequest.Builder()
|
|
.url("https://api.example.com/health")
|
|
.build();
|
|
|
|
System.out.println("Simple GET: " + ping);
|
|
|
|
// Full POST with authentication and retry
|
|
HttpRequest post = new HttpRequest.Builder()
|
|
.url("https://api.example.com/orders")
|
|
.method("POST")
|
|
.body("{\"item\":\"book\",\"qty\":1}")
|
|
.header("Content-Type", "application/json")
|
|
.header("Authorization", "Bearer eyJ...")
|
|
.timeoutMs(10_000) // 10 seconds — name is self-documenting
|
|
.maxRetries(3)
|
|
.build();
|
|
|
|
System.out.println("POST: " + post);
|
|
|
|
// build() throws if url is missing — validation at construction time
|
|
try {
|
|
HttpRequest bad = new HttpRequest.Builder().build(); // no URL!
|
|
} catch (IllegalStateException e) {
|
|
System.out.println("Caught: " + e.getMessage()); // url() is required
|
|
}
|
|
|
|
// Using the Director for the two standard presets defined above
|
|
HttpClientConfig config = new HttpClientConfig();
|
|
HttpRequest healthCheck = config.buildHealthCheck("https://api.example.com");
|
|
System.out.println("Health check: " + healthCheck);
|
|
|
|
HttpRequest order = config.buildResilientPost("https://api.example.com/orders", "{\"qty\":1}");
|
|
System.out.println("Order: " + order);
|
|
}
|
|
}
|