Skip to content

Commit

Permalink
Master: Forward partner-token header
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipeMata committed Nov 22, 2016
1 parent 949ab61 commit 9031f14
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 0.2.1

- Fix: SDK version sent at request body.
- Added: forward `partner-token` header

# 0.2.1

- Fix: SDK version sent at request body

# 0.2.0

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ For more informations about parameters and values, please refer to [Gerencianet]
## Requirements
* Java >= 7.0

## Tested with
```
java 7.0 and 8.0
```
## Installation

**Via gradle:**

```gradle
compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:0.2.1'
compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:0.2.2'
```

**Via maven:**
Expand All @@ -28,7 +32,7 @@ compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:0.2.1'
<dependency>
    <groupId>br.com.gerencianet.gnsdk</groupId>
    <artifactId>gn-api-sdk-java</artifactId>
    <version>0.2.1</version>
    <version>0.2.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.gerencianet.gnsdk</groupId>
<artifactId>gn-api-sdk-java</artifactId>
<version>0.2.2-SNAPSHOT</version>
<version>0.2.2</version>

<name>GN API SDK JAVA</name>
<description>Java SDK for integrating with Gerencianet API</description>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/br/com/gerencianet/gnsdk/APIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public APIRequest(String method, String route, JSONObject body, Config config) t
HttpURLConnection client = (HttpURLConnection) link.openConnection();

this.requester = new Request(method, client);

if(config.getOptions().has("partnerToken")){
this.requester.addHeader("partner-token", config.getOptions().getString("partnerToken"));
}

this.body = body;
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/br/com/gerencianet/gnsdk/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ public Auth(JSONObject credentials, String method, String authorizeRoute) throws
if(!credentials.has("clientId") || !credentials.has("clientSecret")){
throw new Exception("Client_Id or Client_Secret not found");
}

String url = credentials.getString("baseUri") + authorizeRoute;
URL link = new URL(url);
HttpURLConnection client = (HttpURLConnection) link.openConnection();

this.request = new Request(method, client);

if(credentials.has("partnerToken")){
this.request.addHeader("partner-token", credentials.getString("partnerToken"));
}

authBody = new JSONObject();
authBody.put("grant_type", "client_credentials");

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/br/com/gerencianet/gnsdk/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
*/
public class Config {
private final static String version = "0.2.1";
private final static String version = "0.2.2";
private JSONObject conf = new JSONObject();
private JSONObject endpoints = new JSONObject();
private JSONObject urls = new JSONObject();
Expand Down Expand Up @@ -49,6 +49,10 @@ public void setConf(JSONObject options) {
this.conf.put("clientId", options.getString("client_id"));
if(options.has("client_secret"))
this.conf.put("clientSecret", options.getString("client_secret"));

if(options.has("partner_token"))
this.conf.put("partnerToken", options.getString("partner_token"));

if(options.has("url")){
this.conf.put("baseUri", options.getString("url"));
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public void shouldSetPropertiesCorrectly() throws Exception{
JSONObject credentials = mock(JSONObject.class);
when(credentials.has("clientId")).thenReturn(true);
when(credentials.has("clientSecret")).thenReturn(true);
when(credentials.has("partnerToken")).thenReturn(true);
when(credentials.getString("partnerToken")).thenReturn("teste");
when(credentials.getString("baseUri")).thenReturn("https://sandbox.gerencianet.com.br");
when(config.getEndpoints()).thenReturn(endpoints);
when(config.getOptions()).thenReturn(credentials);
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/br/com/gerencianet/gnsdk/AuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public void shouldSetPropertiesCorrectly() throws Exception
{
when(credentials.has("clientId")).thenReturn(true);
when(credentials.has("clientSecret")).thenReturn(true);
when(credentials.has("partnerToken")).thenReturn(true);
when(credentials.getString("partnerToken")).thenReturn("teste");
when(credentials.getString("baseUri")).thenReturn("https://sandbox.gerencianet.com.br");
authenticator = new Auth(credentials, "post", "v1/authorize");
}
Expand All @@ -84,7 +86,7 @@ public void shouldAuthorizeSuccessfully(){
when(requester.send(authenticator.getAuthBody())).thenReturn(success);
authenticator.setRequest(requester);
authenticator.authorize();
verify(requester, times(1)).addHeader("Authorization", "Basic " + authenticator.getAuthCredentials());;
verify(requester, times(1)).addHeader("Authorization", "Basic " + authenticator.getAuthCredentials());
verify(requester, times(1)).send(authenticator.getAuthBody());
Assert.assertTrue(authenticator.getAccessToken().equals("token"));
Assert.assertTrue(authenticator.getExpires().compareTo(expectedBefore) > 0);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/br/com/gerencianet/gnsdk/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void shouldBuildOptionsSuccessfully() throws Exception
JSONObject options = new JSONObject();
options.put("client_id", "123");
options.put("client_secret", "456");
options.put("partner_token", "ptteste");
options.put("debug", true);
options.put("url", "http://filipegnapi.gerencianet.com.br:4400");

Expand All @@ -80,12 +81,14 @@ public void shouldBuildOptionsSuccessfully() throws Exception
assertTrue(config.has("clientId"));
assertTrue(config.has("clientSecret"));
assertTrue(config.has("baseUri"));
assertTrue(config.has("partnerToken"));

assertTrue(config.get("sandbox").equals(false));
assertTrue(config.get("debug").equals(true));
assertTrue(config.get("clientId").equals("123"));
assertTrue(config.get("clientSecret").equals("456"));
assertTrue(config.get("baseUri").equals("http://filipegnapi.gerencianet.com.br:4400"));
assertTrue(config.get("partnerToken").equals("ptteste"));
} catch (IOException e) {
fail("The file config.json doesn't exist or is not in the right fouder.");
}
Expand Down

0 comments on commit 9031f14

Please sign in to comment.