Skip to content

Commit ddc5736

Browse files
Add Gson version to Stripe header (#2124)
* Add Gson version to Stripe header * Fix formatting issues
1 parent 7a86510 commit ddc5736

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.stripe.net;
22

3+
import com.google.gson.Gson;
34
import com.stripe.Stripe;
45
import com.stripe.exception.ApiConnectionException;
56
import com.stripe.exception.StripeException;
7+
import java.io.InputStream;
68
import java.net.ConnectException;
79
import java.net.SocketTimeoutException;
810
import java.time.Duration;
911
import java.util.HashMap;
1012
import java.util.Map;
13+
import java.util.Optional;
14+
import java.util.Properties;
1115
import java.util.concurrent.ThreadLocalRandom;
1216

1317
/** Base abstract class for HTTP clients used to send requests to Stripe's API. */
@@ -176,10 +180,31 @@ protected static String buildXStripeClientUserAgentString() {
176180
if (Stripe.getAppInfo() != null) {
177181
propertyMap.put("application", ApiResource.INTERNAL_GSON.toJson(Stripe.getAppInfo()));
178182
}
183+
getGsonVersion().ifPresent(ver -> propertyMap.put("gson.version", ver));
179184

180185
return ApiResource.INTERNAL_GSON.toJson(propertyMap);
181186
}
182187

188+
/**
189+
* Gets the Gson version being used at runtime.
190+
*
191+
* @return the Gson version string, or Optional.empty() if it cannot be determined
192+
*/
193+
private static Optional<String> getGsonVersion() {
194+
try (InputStream in =
195+
Gson.class.getResourceAsStream(
196+
"/META-INF/maven/com.google.code.gson/gson/pom.properties")) {
197+
if (in != null) {
198+
Properties props = new Properties();
199+
props.load(in);
200+
return Optional.ofNullable(props.getProperty("version"));
201+
}
202+
} catch (Exception ignore) {
203+
// Silently ignore - we'll just not include the version
204+
}
205+
return Optional.empty();
206+
}
207+
183208
private static String formatAppInfo(Map<String, String> info) {
184209
String str = info.get("name");
185210

0 commit comments

Comments
 (0)