Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
HttpRequestSpec.java
Go to the documentation of this file.
1package dev.rafex.ether.http.client.model;
2
3/*-
4 * #%L
5 * ether-http-client
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.net.URI;
30import java.nio.charset.StandardCharsets;
31import java.time.Duration;
32import java.util.ArrayList;
33import java.util.LinkedHashMap;
34import java.util.List;
35import java.util.Map;
36
37import dev.rafex.ether.json.JsonUtils;
38
39public record HttpRequestSpec(HttpMethod method, URI uri, Map<String, List<String>> headers, byte[] body,
40 Duration timeout) {
41
42 public HttpRequestSpec {
43 headers = normalizeHeaders(headers);
44 body = body == null ? new byte[0] : body.clone();
45 }
46
47 public static Builder builder(final HttpMethod method, final URI uri) {
48 return new Builder(method, uri);
49 }
50
51 public static Builder get(final URI uri) {
52 return builder(HttpMethod.GET, uri);
53 }
54
55 public static Builder post(final URI uri) {
56 return builder(HttpMethod.POST, uri);
57 }
58
59 public static Builder put(final URI uri) {
60 return builder(HttpMethod.PUT, uri);
61 }
62
63 public static Builder patch(final URI uri) {
64 return builder(HttpMethod.PATCH, uri);
65 }
66
67 public static Builder delete(final URI uri) {
68 return builder(HttpMethod.DELETE, uri);
69 }
70
71 public String bodyAsString() {
72 return new String(body, StandardCharsets.UTF_8);
73 }
74
75 private static Map<String, List<String>> normalizeHeaders(final Map<String, List<String>> raw) {
76 if (raw == null || raw.isEmpty()) {
77 return Map.of();
78 }
79 final var copy = new LinkedHashMap<String, List<String>>();
80 for (final var entry : raw.entrySet()) {
81 copy.put(entry.getKey(), List.copyOf(entry.getValue()));
82 }
83 return Map.copyOf(copy);
84 }
85
86 public static final class Builder {
87 private final HttpMethod method;
88 private final URI uri;
89 private final Map<String, List<String>> headers = new LinkedHashMap<>();
90 private byte[] body;
91 private Duration timeout;
92 private String contentType;
93
94 private Builder(final HttpMethod method, final URI uri) {
95 this.method = method;
96 this.uri = uri;
97 }
98
99 public Builder header(final String name, final String value) {
100 headers.computeIfAbsent(name, key -> new ArrayList<>()).add(value);
101 return this;
102 }
103
104 public Builder contentType(final String contentType) {
105 this.contentType = contentType;
106 return this;
107 }
108
109 public Builder body(final byte[] body) {
110 this.body = body == null ? null : body.clone();
111 return this;
112 }
113
114 public Builder body(final String content) {
115 this.body = content == null ? null : content.getBytes(StandardCharsets.UTF_8);
116 return this;
117 }
118
119 public Builder jsonBody(final Object value) {
120 this.body = JsonUtils.toJsonBytes(value);
121 this.contentType = "application/json";
122 return this;
123 }
124
125 public Builder jsonBody(final Object value, final dev.rafex.ether.json.JsonCodec codec) {
126 this.body = (codec == null ? JsonUtils.codec() : codec).toJsonBytes(value);
127 this.contentType = "application/json";
128 return this;
129 }
130
131 public Builder timeout(final Duration timeout) {
132 this.timeout = timeout;
133 return this;
134 }
135
136 public HttpRequestSpec build() {
137 if (contentType != null && !headers.containsKey("Content-Type")) {
138 header("Content-Type", contentType);
139 }
140 return new HttpRequestSpec(method, uri, headers, body, timeout);
141 }
142 }
143}
static byte[] toJsonBytes(final Object value)
record HttpRequestSpec(HttpMethod method, URI uri, Map< String, List< String > > headers, byte[] body, Duration timeout)