forked from stripe/stripe-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStripeRequest.java
More file actions
323 lines (279 loc) · 10.3 KB
/
StripeRequest.java
File metadata and controls
323 lines (279 loc) · 10.3 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.stripe.net;
import com.stripe.Stripe;
import com.stripe.exception.ApiConnectionException;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.StripeException;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.experimental.Accessors;
/** A request to Stripe's API. */
@Value
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Accessors(fluent = true)
public class StripeRequest {
/** The HTTP method for the request (GET, POST or DELETE). */
ApiResource.RequestMethod method;
/**
* The URL for the request. If this is a GET or DELETE request, the URL also includes the request
* parameters in its query string.
*/
URL url;
/**
* The body of the request. For POST requests, this will be either a {@code
* application/x-www-form-urlencoded} or a {@code multipart/form-data} payload. For non-POST
* requests, this will be {@code null}.
*/
HttpContent content;
/**
* The HTTP headers of the request ({@code Authorization}, {@code Stripe-Version}, {@code
* Stripe-Account}, {@code Idempotency-Key}...).
*/
HttpHeaders headers;
/** The parameters of the request (as an unmodifiable map). */
Map<String, Object> params;
/** The special modifiers of the request. */
RequestOptions options;
/** The version of the API (ApiMode.V1 or ApiMode.V2) */
ApiMode apiMode;
/**
* Initializes a new instance of the {@link StripeRequest} class.
*
* @param method the HTTP method
* @param url the URL of the request
* @param content the body of the request
* @param params the parameters of the request
* @param options the special modifiers of the request
* @throws StripeException if the request cannot be initialized for any reason
*/
private StripeRequest(
ApiResource.RequestMethod method,
String url,
HttpContent content,
Map<String, Object> params,
RequestOptions options,
ApiMode apiMode)
throws StripeException {
try {
this.content = content;
this.params = (params != null) ? Collections.unmodifiableMap(params) : null;
this.options = (options != null) ? options : RequestOptions.getDefault();
this.method = method;
this.url = buildURL(method, url, params);
this.headers = buildHeaders(method, this.options, this.content, apiMode);
this.apiMode = apiMode;
} catch (IOException e) {
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
}
/**
* Initializes a new instance of the {@link StripeRequest} class.
*
* @param method the HTTP method
* @param url the URL of the request
* @param params the parameters of the request
* @param options the special modifiers of the request
* @param apiMode version of the API
* @throws StripeException if the request cannot be initialized for any reason
*/
StripeRequest(
ApiResource.RequestMethod method,
String url,
Map<String, Object> params,
RequestOptions options,
ApiMode apiMode)
throws StripeException {
try {
this.params = (params != null) ? Collections.unmodifiableMap(params) : null;
this.options = options;
this.method = method;
this.url = buildURL(method, url, params);
this.content = buildContent(method, params, apiMode);
this.headers = buildHeaders(method, this.options, this.content, apiMode);
this.apiMode = apiMode;
} catch (IOException e) {
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
}
/**
* Initializes a new instance of the {@link StripeRequest} class.
*
* @param method the HTTP method
* @param url the URL of the request
* @param params the parameters of the request
* @param options the special modifiers of the request
* @throws StripeException if the request cannot be initialized for any reason
*/
public static StripeRequest create(
ApiResource.RequestMethod method,
String url,
Map<String, Object> params,
RequestOptions options,
ApiMode apiMode)
throws StripeException {
if (options == null) {
throw new IllegalArgumentException("options parameter should not be null");
}
StripeRequest request = new StripeRequest(method, url, params, options, apiMode);
Authenticator authenticator = options.getAuthenticator();
if (authenticator == null) {
throw new AuthenticationException(
"No API key provided. Set your API key using `Stripe.apiKey = \"<API-KEY>\"`. You can "
+ "generate API keys from the Stripe Dashboard. See "
+ "https://stripe.com/docs/api/authentication for details or contact support at "
+ "https://support.stripe.com/email if you have any questions.",
null,
null,
0);
}
request = request.options().getAuthenticator().authenticate(request);
return request;
}
/**
* Initializes a new instance of the {@link StripeRequest} class.
*
* @param method the HTTP method
* @param url the URL of the request
* @param content the body of the request
* @param options the special modifiers of the request
* @throws StripeException if the request cannot be initialized for any reason
*/
public static StripeRequest createWithStringContent(
ApiResource.RequestMethod method,
String url,
String content,
RequestOptions options,
ApiMode apiMode)
throws StripeException {
StripeRequest request =
new StripeRequest(
method, url, buildContentFromString(method, content, apiMode), null, options, apiMode);
Authenticator authenticator = options.getAuthenticator();
if (authenticator == null) {
throw new AuthenticationException(
"No API key provided. Set your API key using `Stripe.apiKey = \"<API-KEY>\"`. You can "
+ "generate API keys from the Stripe Dashboard. See "
+ "https://stripe.com/docs/api/authentication for details or contact support at "
+ "https://support.stripe.com/email if you have any questions.",
null,
null,
0);
}
request = request.options().getAuthenticator().authenticate(request);
return request;
}
/**
* Returns a new {@link StripeRequest} instance with an additional header.
*
* @param name the additional header's name
* @param value the additional header's value
* @return the new {@link StripeRequest} instance
*/
public StripeRequest withAdditionalHeader(String name, String value) {
return new StripeRequest(
this.method,
this.url,
this.content,
this.headers.withAdditionalHeader(name, value),
this.params,
this.options,
this.apiMode);
}
private static URL buildURL(
ApiResource.RequestMethod method, String spec, Map<String, Object> params)
throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(spec);
URL specUrl = new URL(spec);
String specQueryString = specUrl.getQuery();
if ((method != ApiResource.RequestMethod.POST) && (params != null)) {
String queryString = FormEncoder.createQueryString(params);
if (queryString != null && !queryString.isEmpty()) {
if (specQueryString != null && !specQueryString.isEmpty()) {
sb.append("&");
} else {
sb.append("?");
}
sb.append(queryString);
}
}
return new URL(sb.toString());
}
private static HttpContent buildContent(
ApiResource.RequestMethod method, Map<String, Object> params, ApiMode apiMode)
throws IOException {
if (method != ApiResource.RequestMethod.POST) {
return null;
}
if (apiMode == ApiMode.V2) {
return JsonEncoder.createHttpContent(params);
}
return FormEncoder.createHttpContent(params);
}
private static HttpContent buildContentFromString(
ApiResource.RequestMethod method, String content, ApiMode apiMode)
throws ApiConnectionException {
if (method != ApiResource.RequestMethod.POST) {
return null;
}
if (apiMode == ApiMode.V2) {
return HttpContent.buildJsonContent(content);
}
HttpContent httpContent = null;
try {
httpContent = HttpContent.buildFormURLEncodedContent(content);
} catch (IOException e) {
handleIOException(e);
}
return httpContent;
}
private static void handleIOException(IOException e) throws ApiConnectionException {
throw ApiConnectionException.create(Stripe.getApiBase(), e);
}
private static HttpHeaders buildHeaders(
ApiResource.RequestMethod method,
RequestOptions options,
HttpContent content,
ApiMode apiMode) {
Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
// Accept
headerMap.put("Accept", Arrays.asList("application/json"));
// Accept-Charset
headerMap.put("Accept-Charset", Arrays.asList(ApiResource.CHARSET.name()));
// Stripe-Version
if (RequestOptions.unsafeGetStripeVersionOverride(options) != null) {
headerMap.put(
"Stripe-Version", Arrays.asList(RequestOptions.unsafeGetStripeVersionOverride(options)));
} else if (options.getStripeVersion() != null) {
headerMap.put("Stripe-Version", Arrays.asList(options.getStripeVersion()));
}
if (apiMode == ApiMode.V2) {
if (content != null) {
headerMap.put("Content-Type", Arrays.asList(content.contentType()));
}
}
// Stripe-Context
if (options.getStripeContext() != null) {
headerMap.put("Stripe-Context", Arrays.asList(options.getStripeContext()));
}
// Stripe-Request-Trigger
if (options.getStripeRequestTrigger() != null) {
headerMap.put("Stripe-Request-Trigger", Arrays.asList(options.getStripeRequestTrigger()));
}
// Stripe-Account
if (options.getStripeAccount() != null) {
headerMap.put("Stripe-Account", Arrays.asList(options.getStripeAccount()));
}
// Idempotency-Key
if (options.getIdempotencyKey() != null) {
headerMap.put("Idempotency-Key", Arrays.asList(options.getIdempotencyKey()));
} else if (method == ApiResource.RequestMethod.POST
|| (apiMode == ApiMode.V2 && method == ApiResource.RequestMethod.DELETE)) {
headerMap.put("Idempotency-Key", Arrays.asList(UUID.randomUUID().toString()));
}
return HttpHeaders.of(headerMap);
}
}