Skip to content

Commit 441f547

Browse files
committed
Move all request properties in StripeRequest (#898)
1 parent 91c06b8 commit 441f547

8 files changed

+426
-367
lines changed

src/main/java/com/stripe/net/HttpClient.java

+86
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.stripe.net;
22

3+
import com.stripe.Stripe;
34
import com.stripe.exception.StripeException;
5+
import com.stripe.util.Stopwatch;
6+
import java.util.HashMap;
7+
import java.util.Map;
48

59
/** Base abstract class for HTTP clients used to send requests to Stripe's API. */
610
public abstract class HttpClient {
11+
private static final RequestTelemetry requestTelemetry = new RequestTelemetry();
12+
713
/**
814
* Sends the given request to Stripe's API.
915
*
@@ -12,4 +18,84 @@ public abstract class HttpClient {
1218
* @throws StripeException If the request fails for any reason
1319
*/
1420
public abstract StripeResponse request(StripeRequest request) throws StripeException;
21+
22+
/**
23+
* Sends the given request to Stripe's API, handling telemetry if not disabled.
24+
*
25+
* @param request the request
26+
* @return the response
27+
* @throws StripeException If the request fails for any reason
28+
*/
29+
public StripeResponse requestWithTelemetry(StripeRequest request) throws StripeException {
30+
requestTelemetry.maybeAddTelemetryHeader(request.headers());
31+
32+
Stopwatch stopwatch = Stopwatch.startNew();
33+
34+
StripeResponse response = this.request(request);
35+
36+
stopwatch.stop();
37+
38+
requestTelemetry.maybeEnqueueMetrics(response, stopwatch.getElapsed());
39+
40+
return response;
41+
}
42+
43+
/**
44+
* Builds the value of the {@code User-Agent} header.
45+
*
46+
* @return a string containing the value of the {@code User-Agent} header
47+
*/
48+
protected static String buildUserAgentString() {
49+
String userAgent = String.format("Stripe/v1 JavaBindings/%s", Stripe.VERSION);
50+
51+
if (Stripe.getAppInfo() != null) {
52+
userAgent += " " + formatAppInfo(Stripe.getAppInfo());
53+
}
54+
55+
return userAgent;
56+
}
57+
58+
/**
59+
* Builds the value of the {@code X-Stripe-Client-User-Agent} header.
60+
*
61+
* @return a string containing the value of the {@code X-Stripe-Client-User-Agent} header
62+
*/
63+
protected static String buildXStripeClientUserAgentString() {
64+
String[] propertyNames = {
65+
"os.name",
66+
"os.version",
67+
"os.arch",
68+
"java.version",
69+
"java.vendor",
70+
"java.vm.version",
71+
"java.vm.vendor"
72+
};
73+
74+
Map<String, String> propertyMap = new HashMap<>();
75+
for (String propertyName : propertyNames) {
76+
propertyMap.put(propertyName, System.getProperty(propertyName));
77+
}
78+
propertyMap.put("bindings.version", Stripe.VERSION);
79+
propertyMap.put("lang", "Java");
80+
propertyMap.put("publisher", "Stripe");
81+
if (Stripe.getAppInfo() != null) {
82+
propertyMap.put("application", ApiResource.GSON.toJson(Stripe.getAppInfo()));
83+
}
84+
85+
return ApiResource.GSON.toJson(propertyMap);
86+
}
87+
88+
private static String formatAppInfo(Map<String, String> info) {
89+
String str = info.get("name");
90+
91+
if (info.get("version") != null) {
92+
str += String.format("/%s", info.get("version"));
93+
}
94+
95+
if (info.get("url") != null) {
96+
str += String.format(" (%s)", info.get("url"));
97+
}
98+
99+
return str;
100+
}
15101
}

0 commit comments

Comments
 (0)