Skip to content

Commit

Permalink
Include last reponse on Stripe API Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sedouard committed Nov 16, 2017
1 parent b70aba3 commit 3c488ce
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,7 @@ target
# Gradle files
.gradle/*
build/*

# compiler output
bin/

11 changes: 11 additions & 0 deletions src/main/java/com/stripe/model/StripeObject.java
@@ -1,5 +1,7 @@
package com.stripe.model;

import com.stripe.net.StripeResponse;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -26,10 +28,19 @@ public String toString() {
PRETTY_PRINT_GSON.toJson(this));
}

public StripeResponse getLastResponse() {
return lastResponse;
}
public void setLastResponse(StripeResponse response) {
this.lastResponse = response;
}

public String toJson() {
return PRETTY_PRINT_GSON.toJson(this);
}

private transient StripeResponse lastResponse;

private Object getIdString() {
try {
Field idField = this.getClass().getDeclaredField("id");
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
@@ -1,6 +1,7 @@
package com.stripe.net;

import com.stripe.Stripe;
import com.stripe.model.StripeObject;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.APIException;
import com.stripe.exception.AuthenticationException;
Expand All @@ -15,7 +16,6 @@
import com.stripe.exception.oauth.OAuthException;
import com.stripe.exception.oauth.UnsupportedGrantTypeException;
import com.stripe.exception.oauth.UnsupportedResponseTypeException;
import com.stripe.model.StripeCollectionInterface;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
Expand All @@ -35,7 +35,6 @@
import java.net.URLStreamHandler;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -482,16 +481,20 @@ private static <T> T _request(
APIConnectionException, CardException, APIException {
StripeResponse response = _rawRequest(method, url, params, type, options);

int rCode = response.getResponseCode();
String rBody = response.getResponseBody();
String requestId = response.getRequestId();
int rCode = response.code();
String rBody = response.body();
String requestId = response.requestId();

if (rCode < 200 || rCode >= 300) {
handleAPIError(rBody, rCode, requestId);
}

T resource = APIResource.GSON.fromJson(rBody, clazz);

if (resource instanceof StripeObject) {
StripeObject obj = (StripeObject)resource;
obj.setLastResponse(response);
}
return resource;
}

Expand All @@ -502,9 +505,9 @@ private static <T> T _oAuthRequest(
APIConnectionException, APIException, OAuthException {
StripeResponse response = _rawRequest(method, url, params, type, options);

int rCode = response.getResponseCode();
String rBody = response.getResponseBody();
String requestId = response.getRequestId();
int rCode = response.code();
String rBody = response.body();
String requestId = response.requestId();

if (rCode < 200 || rCode >= 300) {
handleOAuthError(rBody, rCode, requestId);
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/stripe/net/StripeHeaders.java
@@ -0,0 +1,32 @@
package com.stripe.net;

import java.util.List;
import java.util.Map;

public class StripeHeaders {

Map<String, List<String>> headers;

public StripeHeaders(Map<String, List<String>> headers) {
this.headers = headers;
}

/**
* Returns the first header value for a given key
* @param name The name of the header key
* @return the first value for the given key
*/
public String get(String name) {
List<String> valuesList = values(name);
String value = null;
if (valuesList != null && valuesList.size() > 0) {
value = valuesList.get(0);
}
return value;
}

public List<String> values(String name) {
return headers == null ? null : headers.get(name);
}

}
56 changes: 26 additions & 30 deletions src/main/java/com/stripe/net/StripeResponse.java
Expand Up @@ -5,49 +5,45 @@

public class StripeResponse {

int responseCode;
String responseBody;
Map<String, List<String>> responseHeaders;

public StripeResponse(int responseCode, String responseBody) {
this.responseCode = responseCode;
this.responseBody = responseBody;
this.responseHeaders = null;
}
int code;
String body;
StripeHeaders headers;

public StripeResponse(int responseCode, String responseBody, Map<String, List<String>> responseHeaders) {
this.responseCode = responseCode;
this.responseBody = responseBody;
this.responseHeaders = responseHeaders;
public StripeResponse(int code, String body) {
this.code = code;
this.body = body;
this.headers = null;
}

public int getResponseCode() {
return responseCode;
public StripeResponse(int code, String body, Map<String, List<String>> headers) {
this.code = code;
this.body = body;
this.headers = new StripeHeaders(headers);
}

public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
public int code() {
return this.code;
}

public String getResponseBody() {
return responseBody;
public String body() {
return this.body;
}

public void setResponseBody(String responseBody) {
this.responseBody = responseBody;
public StripeHeaders headers() {
return headers;
}

public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
public String idempotencyKey() {
if (headers == null) {
return null;
}
return headers.get("Idempotency-Key");
}

public String getRequestId() {
String requestId = null;
Map<String, List<String>> headers = getResponseHeaders();
List<String> requestIdList = headers == null ? null : headers.get("Request-Id");
if (requestIdList != null && requestIdList.size() > 0) {
requestId = requestIdList.get(0);
public String requestId() {
if (headers == null) {
return null;
}
return requestId;
return headers.get("Request-Id");
}
}
62 changes: 62 additions & 0 deletions src/test/java/com/stripe/functional/StripeResponseTest.java
@@ -0,0 +1,62 @@
package com.stripe.functional;

import static org.hamcrest.CoreMatchers.instanceOf;

import com.stripe.Stripe;
import com.stripe.model.Customer;
import com.stripe.model.CustomerCollection;

import com.stripe.BaseStripeFunctionalTest;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.APIException;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.net.StripeResponse;
import com.stripe.net.RequestOptions;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;

import java.util.HashMap;

public class StripeResponseTest extends BaseStripeFunctionalTest {
@Test
public void testResponseIncluded() throws
AuthenticationException,
InvalidRequestException,
APIException,
APIConnectionException,
CardException {
String idempotencyKey = Long.toString(System.currentTimeMillis());
RequestOptions requestOptions = RequestOptions.builder()
.setStripeVersion(Stripe.apiVersion)
.setIdempotencyKey(idempotencyKey)
.build();
Customer cus = Customer.create(defaultCustomerParams, requestOptions);
cus = Customer.retrieve(cus.getId(), requestOptions);
StripeResponse resp = cus.getLastResponse();
assertThat(resp, instanceOf(StripeResponse.class));
assertEquals(200, resp.code());
assertEquals(idempotencyKey, resp.idempotencyKey());
assertTrue(resp.requestId().startsWith("req_"));
assertTrue(resp.body().length() > 0);
}
@Test
public void testResponseIncludedList() throws
AuthenticationException,
InvalidRequestException,
APIException,
APIConnectionException,
CardException {
CustomerCollection cusCollection = Customer.list(new HashMap<String, Object>());
StripeResponse resp = cusCollection.getLastResponse();
assertThat(resp, instanceOf(StripeResponse.class));
assertEquals(200, resp.code());
assertTrue(resp.requestId().startsWith("req_"));
assertTrue(resp.body().length() > 0);
}
}
@@ -1,12 +1,9 @@
package com.stripe.net;

import com.stripe.exception.StripeException;
import com.stripe.model.Account;
import com.stripe.net.LiveStripeResponseGetter;
import com.stripe.net.RequestOptions;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand All @@ -17,7 +14,6 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

public class LiveStripeResponseGetterTest {
LiveStripeResponseGetter srg;
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/com/stripe/net/StripeHeadersTest.java
@@ -0,0 +1,50 @@
package com.stripe.net;

import com.stripe.BaseStripeTest;
import com.stripe.net.StripeHeaders;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class StripeHeadersTest extends BaseStripeTest {

private Map<String, List<String>> generateHeaderMap () {
Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
List<String> multiValueHeader = new ArrayList<String>();
multiValueHeader.add("FirstValue");
multiValueHeader.add("SecondValue");
List<String> requestIdHeader = new ArrayList<String>();
requestIdHeader.add("req_12345");
headerMap.put("Request-Id", requestIdHeader);
headerMap.put("Multi-Val", multiValueHeader);
headerMap.put("Empty-Header", new ArrayList<String>());
return headerMap;
}

@Test
public void testGet() {
StripeHeaders headers = new StripeHeaders(generateHeaderMap());
assertEquals("req_12345", headers.get("Request-Id"));
assertEquals("FirstValue", headers.get("Multi-Val"));
}

@Test
public void testValues() {
StripeHeaders headers = new StripeHeaders(generateHeaderMap());
assertEquals(2, headers.values("Multi-Val").size());
assertEquals("FirstValue", headers.values("Multi-Val").get(0));
assertEquals("SecondValue", headers.values("Multi-Val").get(1));
}

@Test
public void testGetEmpty() {
StripeHeaders headers = new StripeHeaders(generateHeaderMap());
assertEquals(null, headers.get("Empty-Header"));
}
}

0 comments on commit 3c488ce

Please sign in to comment.