Skip to content

Commit

Permalink
Merge pull request #6 from jessica-gavaGN/master
Browse files Browse the repository at this point in the history
Added API Pix endpoints
  • Loading branch information
jessica-gavaGN authored May 10, 2021
2 parents 402e091 + a4e4680 commit 04b92cb
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 274 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 1.0.1

- Fix: Error message

# 1.0.0

- Added: API Pix endpoints

# 0.2.6

- Added: new endpoint (one step)
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ java 7.0 and 8.0
**Via gradle:**

```gradle
compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:0.2.6'
compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:1.0.1'
```

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

Expand Down Expand Up @@ -60,6 +60,7 @@ Instantiate the module passing using your client_id, client_secret and sandbox e
JSONObject options = new JSONObject();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
options.put("pix_cert", "./certs/developmentCertificate.p12");
options.put("sandbox", true);

Gerencianet gn = new Gerencianet($options);
Expand All @@ -70,6 +71,7 @@ Or
Map<String, Object> options = new HashMap<String, Object>();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
options.put("pix_cert", "./certs/developmentCertificate.p12");
options.put("sandbox", true);

Gerencianet gn = new Gerencianet($options);
Expand All @@ -81,6 +83,7 @@ To change the environment to production, just set the third sandbox to false:
JSONObject options = new JSONObject();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
options.put("pix_cert", "./certs/productionCertificate.p12");
options.put("sandbox", false);

Gerencianet gn = new Gerencianet($options);
Expand All @@ -91,6 +94,7 @@ Or
Map<String, Object> options = new HashMap<String, Object>();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
options.put("pix_cert", "./certs/productionCertificate.p12");
options.put("sandbox", false);

Gerencianet gn = new Gerencianet($options);
Expand Down
25 changes: 17 additions & 8 deletions 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.6</version>
<version>1.0.1</version>

<name>GN API SDK JAVA</name>
<description>Java SDK for integrating with Gerencianet API</description>
Expand Down Expand Up @@ -58,9 +58,9 @@
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -88,6 +88,7 @@
<pluginManagement>
<plugins>
<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5</version>
Expand All @@ -104,7 +105,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -115,10 +116,10 @@

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/test/**</exclude>
</excludes>
Expand All @@ -144,6 +145,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
Expand Down
48 changes: 27 additions & 21 deletions src/main/java/br/com/gerencianet/gnsdk/APIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,72 @@
import br.com.gerencianet.gnsdk.exceptions.GerencianetException;

/**
* This class instance a Auth Object, to authenticate client credentials in Gerencianet API. After client's credentials
* are validated a client Object send a given request body to a given endpoint throw a given route.
* This class instance a Auth Object, to authenticate client credentials in
* Gerencianet API. After client's credentials are validated a client Object
* send a given request body to a given endpoint throw a given route.
*
* @author Filipe Mata
*
*/
public class APIRequest {
private Request requester;
private Auth authenticator;
private String route;
private String route;
private JSONObject body;



public APIRequest(String method, String route, JSONObject body, Config config) throws Exception {
this.route = route;
String authenticateRoute = config.getEndpoints().getJSONObject("authorize").getString("route");
String authenticateMethod = config.getEndpoints().getJSONObject("authorize").getString("method");
this.authenticator = new Auth(config.getOptions(), authenticateMethod, authenticateRoute);

String url = config.getOptions().getString("baseUri") + route;
URL link = new URL(url);
HttpURLConnection client = (HttpURLConnection) link.openConnection();

this.requester = new Request(method, client);
if(config.getOptions().has("partnerToken")){

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


if (config.getOptions().has("headers")) {
this.requester.addHeader("x-skip-mtls-checking", config.getOptions().getString("headers"));
}

this.body = body;
}
public APIRequest(Auth auth, Request request, JSONObject body){

public APIRequest(Auth auth, Request request, JSONObject body) {
this.authenticator = auth;
this.requester = request;
this.body = body;
}
public JSONObject send() throws AuthorizationException, GerencianetException, IOException{

public JSONObject send() throws AuthorizationException, GerencianetException, IOException {
Date expiredDate = this.authenticator.getExpires();
if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) {
this.authenticator.authorize();
}


this.authenticator.authorize();
}

this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken());
try {
try {
return this.requester.send(this.body);
} catch (AuthorizationException e) {
this.authenticator.authorize();
return this.requester.send(body);
}
}

public Request getRequester() {
return requester;
}

public String getRoute() {
return route;
}

public JSONObject getBody() {
return body;
}
Expand Down
45 changes: 26 additions & 19 deletions src/main/java/br/com/gerencianet/gnsdk/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import br.com.gerencianet.gnsdk.exceptions.GerencianetException;

/**
* This class is used to create an authenticator Object,
* responsible to send needed Gerencianet credentials (Client_Id and Client_Secret) to it's API.
* This class is used to create an authenticator Object, responsible to send
* needed Gerencianet credentials (Client_Id and Client_Secret) to it's API.
*
* @author Filipe Mata
*
*/
Expand All @@ -23,58 +24,64 @@ public class Auth {
private Request request;
private JSONObject authBody;
private String authCredentials;

public Auth(JSONObject credentials, String method, String authorizeRoute) throws Exception {
if(!credentials.has("clientId") || !credentials.has("clientSecret")){

if (!credentials.has("clientId") || !credentials.has("clientSecret")) {
throw new Exception("Client_Id or Client_Secret not found");
}


if (credentials.has("certificadoPix")) {
System.setProperty("javax.net.ssl.keyStore", credentials.getString("certificadoPix"));
}

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")){

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

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

String auth = credentials.getString("clientId") + ":" + credentials.getString("clientSecret");
this.authCredentials = DatatypeConverter.printBase64Binary(auth.getBytes("UTF-8"));

}

public void setRequest(Request request) {
this.request = request;
}
public void authorize() throws IOException, AuthorizationException, GerencianetException{

public void authorize() throws IOException, AuthorizationException, GerencianetException {
this.request.addHeader("Authorization", "Basic " + this.authCredentials);
JSONObject response = this.request.send(authBody);
this.accessToken = response.getString("access_token");
this.expires = new Date(new Date().getTime() + response.getLong("expires_in"));
this.tokenType = response.getString("token_type");

}

public Date getExpires() {
return this.expires;
}

public String getAccessToken() {
return this.accessToken;
}

public String getTokenType() {
return this.tokenType;
}

public JSONObject getAuthBody() {
return authBody;
}

public String getAuthCredentials() {
return authCredentials;
}
Expand Down
Loading

0 comments on commit 04b92cb

Please sign in to comment.