-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestBuilder.java
More file actions
115 lines (106 loc) · 3.98 KB
/
RequestBuilder.java
File metadata and controls
115 lines (106 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package io.github.ModularEnigma;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
public class RequestBuilder {
private String url;
private Request.Method requestMethod;
private String requestBody;
private Duration timeout;
private final Map<String, String> headers;
public RequestBuilder() {
headers = new HashMap<>();
}
/**
* Add a header to the request. Note: Accept and Content-Type are already set to use JSON.
*
* @param header Header to set
* @param value Value of the header
* @return {@link RequestBuilder}
* @throws IllegalArgumentException If the header or value is null
*/
public RequestBuilder addHeader(String header, String value) {
if (header == null) {
throw new IllegalArgumentException("Header cannot be null.");
} else if (value == null) {
throw new IllegalArgumentException("Header value cannot be null.");
}
headers.put(header, value);
return this;
}
/**
* Set the url of the Request. Validity of the url is checked in {@link RequestBuilder#build()}.
*
* @param url The url of the website we will be sending a request to
* @return {@link RequestBuilder}
* @throws IllegalArgumentException If the url is null
*/
public RequestBuilder setURL(String url) throws IllegalArgumentException {
if (url == null) {
throw new IllegalArgumentException("url cannot be null.");
}
this.url = url;
return this;
}
/**
* Sets the request method for the request
*
* @param requestMethod The Method of the request.
* @return {@link RequestBuilder}
*/
public RequestBuilder setMethod(Request.Method requestMethod) throws IllegalArgumentException {
if (requestMethod == null) {
throw new IllegalArgumentException("requestMethod cannot be null.");
}
this.requestMethod = requestMethod;
return this;
}
/**
* Set the contents of the request. Only useful for requests that support content
* such as POST requests.
*
* @param body The body of the request
* @return {@link RequestBuilder}
* @throws IllegalArgumentException If body is null
*/
public RequestBuilder setRequestBody(String body) throws IllegalArgumentException {
if (body == null) {
throw new IllegalArgumentException("body cannot be null.");
}
this.requestBody = body;
return this;
}
/**
* Sets a timeout for the request. If the server does not respond within this duration,
* the request will throw a {@link java.net.http.HttpTimeoutException}.
*
* @param timeout The maximum time to wait for a response
* @return {@link RequestBuilder}
* @throws IllegalArgumentException If timeout is null or non-positive
*/
public RequestBuilder setTimeout(Duration timeout) {
if (timeout == null) {
throw new IllegalArgumentException("timeout cannot be null.");
} else if (timeout.isNegative() || timeout.isZero()) {
throw new IllegalArgumentException("timeout must be positive.");
}
this.timeout = timeout;
return this;
}
/**
* Builds the {@link Request} object if the {@link RequestBuilder} has been given enough
* information to do so.
*
* @return A {@link Request} instance containing all data added to the {@link RequestBuilder}
* @throws IllegalStateException If the url has not been set. If the requestMethod has not been
* set.
*/
public Request build() throws IllegalStateException {
if (url == null) {
throw new IllegalStateException("url is required.");
} else if (requestMethod == null) {
throw new IllegalStateException("requestMethod is required.");
}
return new Request(url, requestMethod, requestBody, headers, timeout);
}
}