Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/com/stripe/net/HttpClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.stripe.net;

import com.google.gson.Gson;
import com.stripe.Stripe;
import com.stripe.exception.ApiConnectionException;
import com.stripe.exception.StripeException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;

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

return ApiResource.INTERNAL_GSON.toJson(propertyMap);
}

/**
* Gets the Gson version being used at runtime.
*
* @return the Gson version string, or Optional.empty() if it cannot be determined
*/
private static Optional<String> getGsonVersion() {
try (InputStream in =
Gson.class.getResourceAsStream(
"/META-INF/maven/com.google.code.gson/gson/pom.properties")) {
if (in != null) {
Properties props = new Properties();
props.load(in);
return Optional.ofNullable(props.getProperty("version"));
}
} catch (Exception ignore) {
// Silently ignore - we'll just not include the version
}
return Optional.empty();
}

private static String formatAppInfo(Map<String, String> info) {
String str = info.get("name");

Expand Down