Skip to content

Commit cf9837d

Browse files
authored
Merge pull request #449 from stripe/ob-setappinfo
Add setAppInfo() for passing custom application information in headers
2 parents 739ddbd + 89a155b commit cf9837d

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ Please take care to set conservative read timeouts. Some API requests can take
101101
some time, and a short timeout increases the likelihood of a problem within our
102102
servers.
103103

104+
### Writing a plugin
105+
106+
If you're writing a plugin that uses the library, we'd appreciate it if you
107+
identified using `Stripe.setAppInfo()`:
108+
109+
Stripe.setAppInfo("MyAwesomePlugin", "1.2.34", "https://myawesomeplugin.info");
110+
111+
This information is passed along when the library makes calls to the Stripe
112+
API.
113+
104114
## Testing
105115

106116
You must have Gradle installed. To run the tests:

src/main/java/com/stripe/Stripe.java

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.net.PasswordAuthentication;
44
import java.net.Proxy;
55

6+
import java.util.HashMap;
7+
import java.util.Map;
8+
69
public abstract class Stripe {
710
private final static int DEFAULT_CONNECT_TIMEOUT = 30 * 1000;
811
private final static int DEFAULT_READ_TIMEOUT = 80 * 1000;
@@ -27,6 +30,8 @@ public abstract class Stripe {
2730
private static volatile Proxy connectionProxy = null;
2831
private static volatile PasswordAuthentication proxyCredential = null;
2932

33+
private static volatile Map<String, String> appInfo = null;
34+
3035

3136
/**
3237
* (FOR TESTING ONLY) If you'd like your API requests to hit your own
@@ -116,4 +121,26 @@ public static void setProxyCredential(final PasswordAuthentication auth) {
116121
public static PasswordAuthentication getProxyCredential() {
117122
return proxyCredential;
118123
}
124+
125+
public static void setAppInfo(String name) {
126+
setAppInfo(name, null, null);
127+
}
128+
129+
public static void setAppInfo(String name, String version) {
130+
setAppInfo(name, version, null);
131+
}
132+
133+
public static void setAppInfo(String name, String version, String url) {
134+
if (appInfo == null) {
135+
appInfo = new HashMap<String, String>();
136+
}
137+
138+
appInfo.put("name", name);
139+
appInfo.put("version", version);
140+
appInfo.put("url", url);
141+
}
142+
143+
public static Map<String, String> getAppInfo() {
144+
return appInfo;
145+
}
119146
}

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,29 @@ private static String urlEncodePair(String k, String v)
8989
return String.format("%s=%s", APIResource.urlEncode(k), APIResource.urlEncode(v));
9090
}
9191

92+
static String formatAppInfo(Map<String, String> info) {
93+
String str = info.get("name");
94+
if (info.get("version") != null) {
95+
str += String.format("/%s", info.get("version"));
96+
}
97+
if (info.get("url") != null) {
98+
str += String.format(" (%s)", info.get("url"));
99+
}
100+
return str;
101+
}
102+
92103
static Map<String, String> getHeaders(RequestOptions options) {
93104
Map<String, String> headers = new HashMap<String, String>();
105+
106+
String userAgent = String.format("Stripe/v1 JavaBindings/%s", Stripe.VERSION);
107+
if (Stripe.getAppInfo() != null) {
108+
userAgent += " " + formatAppInfo(Stripe.getAppInfo());
109+
}
110+
headers.put("User-Agent", userAgent);
111+
94112
String apiVersion = options.getStripeVersion();
95113
headers.put("Accept-Charset", APIResource.CHARSET);
96114
headers.put("Accept", "application/json");
97-
headers.put("User-Agent",
98-
String.format("Stripe/v1 JavaBindings/%s", Stripe.VERSION));
99115

100116
headers.put("Authorization", String.format("Bearer %s", options.getApiKey()));
101117

@@ -110,6 +126,9 @@ static Map<String, String> getHeaders(RequestOptions options) {
110126
propertyMap.put("bindings.version", Stripe.VERSION);
111127
propertyMap.put("lang", "Java");
112128
propertyMap.put("publisher", "Stripe");
129+
if (Stripe.getAppInfo() != null) {
130+
propertyMap.put("application", APIResource.GSON.toJson(Stripe.getAppInfo()));
131+
}
113132
headers.put("X-Stripe-Client-User-Agent", APIResource.GSON.toJson(propertyMap));
114133
if (apiVersion != null) {
115134
headers.put("Stripe-Version", apiVersion);

src/test/java/com/stripe/net/LiveStripeResponseGetterTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.stripe.net;
22

3+
import com.stripe.Stripe;
34
import com.stripe.exception.StripeException;
45
import com.stripe.net.LiveStripeResponseGetter;
6+
import com.stripe.net.RequestOptions;
7+
import com.stripe.net.RequestOptions.RequestOptionsBuilder;
8+
9+
import com.google.gson.Gson;
510

611
import java.io.UnsupportedEncodingException;
712
import java.util.HashMap;
@@ -14,6 +19,7 @@
1419
import org.junit.Test;
1520

1621
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNotNull;
1723

1824
public class LiveStripeResponseGetterTest {
1925
LiveStripeResponseGetter srg;
@@ -126,4 +132,28 @@ public void testCorrectAdditionalOwners() throws StripeException, UnsupportedEnc
126132

127133
assertEquals(encode("legal_entity[additional_owners][0][first_name]=Stripe"), LiveStripeResponseGetter.createQuery(params));
128134
}
135+
136+
@Test
137+
public void testAppInfo() {
138+
RequestOptions options = (new RequestOptionsBuilder()).setApiKey("sk_foobar").build();
139+
140+
Stripe.setAppInfo("MyAwesomePlugin", "1.2.34", "https://myawesomeplugin.info");
141+
142+
Map<String, String> headers = srg.getHeaders(options);
143+
144+
String expectedUserAgent = String.format(
145+
"Stripe/v1 JavaBindings/%s MyAwesomePlugin/1.2.34 (https://myawesomeplugin.info)",
146+
Stripe.VERSION);
147+
assertEquals(expectedUserAgent, headers.get("User-Agent"));
148+
149+
Gson gson = new Gson();
150+
151+
Map<String, String> uaMap = gson.fromJson(headers.get("X-Stripe-Client-User-Agent"), Map.class);
152+
assertNotNull(uaMap.get("application"));
153+
154+
Map<String, String> appMap = gson.fromJson(uaMap.get("application"), Map.class);
155+
assertEquals("MyAwesomePlugin", appMap.get("name"));
156+
assertEquals("1.2.34", appMap.get("version"));
157+
assertEquals("https://myawesomeplugin.info", appMap.get("url"));
158+
}
129159
}

0 commit comments

Comments
 (0)