Repository navigation
Expand file tree
/
Copy pathRequest.java
More file actions
158 lines (135 loc) · 3.57 KB
/
Copy pathRequest.java
File metadata and controls
158 lines (135 loc) · 3.57 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package io.getstream.core.http;
import com.google.common.base.MoreObjects;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;
public final class Request {
private final Token token;
private final URL url;
private final Method method;
private final RequestBody body;
private Request(Builder builder) throws MalformedURLException {
token = builder.token;
url = builder.uri.toURL();
method = builder.method;
body = builder.body;
}
public Token getToken() {
return token;
}
public URL getURL() {
return url;
}
public Method getMethod() {
return method;
}
public RequestBody getBody() {
return body;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Request request = (Request) o;
return Objects.equals(token, request.token)
&& Objects.equals(url, request.url)
&& method == request.method
&& Objects.equals(body, request.body);
}
@Override
public int hashCode() {
return Objects.hash(token, url, method, body);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("token", this.token)
.add("url", this.url)
.add("method", this.method)
.add("body", this.body)
.toString();
}
public static Builder builder() {
return new Builder();
}
public enum Method {
GET,
POST,
PUT,
DELETE
}
public static final class Builder {
private Token token;
private URI uri;
private StringBuilder query;
private Method method;
private RequestBody body;
public Builder token(Token token) {
this.token = token;
return this;
}
public Builder url(URL url) throws URISyntaxException {
uri = url.toURI();
if (uri.getQuery() != null) {
query = new StringBuilder(uri.getQuery());
} else {
query = new StringBuilder();
}
return this;
}
public Builder addQueryParameter(String key, String value) {
if (query.length() > 0) {
query.append('&');
}
query.append(key);
query.append('=');
query.append(value);
return this;
}
public Builder get() {
this.method = Method.GET;
this.body = null;
return this;
}
public Builder post(byte[] body) {
this.method = Method.POST;
this.body = new RequestBody(body, RequestBody.Type.JSON);
return this;
}
public Builder multiPartPost(String fileName, byte[] body) {
this.method = Method.POST;
this.body = new RequestBody(fileName, body, RequestBody.Type.MULTI_PART);
return this;
}
public Builder multiPartPost(File body) {
this.method = Method.POST;
this.body = new RequestBody(body, RequestBody.Type.MULTI_PART);
return this;
}
public Builder put(byte[] body) {
this.method = Method.PUT;
this.body = new RequestBody(body, RequestBody.Type.JSON);
return this;
}
public Builder delete() {
this.method = Method.DELETE;
this.body = null;
return this;
}
public Request build() throws MalformedURLException, URISyntaxException {
this.uri =
new URI(
uri.getScheme(),
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
uri.getPath(),
query.toString(),
null);
return new Request(this);
}
}
}