Skip to content

Commit

Permalink
Updating and adding new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jessica-gavaGN committed Oct 11, 2022
1 parent bacf7c6 commit 7463a0d
Show file tree
Hide file tree
Showing 15 changed files with 620 additions and 389 deletions.
20 changes: 19 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand All @@ -27,5 +28,22 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1665500716441</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 2.0.0
- Added: API Open Finance
- Added: API Pagamentos
- Added: new endpoints of API Pix
- Updated: PixSend example
- Updated: rename "pix_cert" to "certificate" in credentials.json

# 1.0.2

- Fix: Error message
Expand Down
12 changes: 6 additions & 6 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:1.0.2'
compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:2.0.0'
```

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

Expand Down Expand Up @@ -60,7 +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("certificate", "./certs/developmentCertificate.p12");
options.put("sandbox", true);

Gerencianet gn = new Gerencianet($options);
Expand All @@ -71,7 +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("certificate", "./certs/developmentCertificate.p12");
options.put("sandbox", true);

Gerencianet gn = new Gerencianet($options);
Expand All @@ -83,7 +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("certificate", "./certs/productionCertificate.p12");
options.put("sandbox", false);

Gerencianet gn = new Gerencianet($options);
Expand All @@ -94,7 +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("certificate", "./certs/productionCertificate.p12");
options.put("sandbox", false);

Gerencianet gn = new Gerencianet($options);
Expand Down
8 changes: 4 additions & 4 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>1.0.2</version>
<version>2.0.0</version>

<name>GN API SDK JAVA</name>
<description>Java SDK for integrating with Gerencianet API</description>
Expand Down Expand Up @@ -148,7 +148,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<version>2.22.0</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
Expand All @@ -157,7 +157,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<version>0.7.8</version>
<configuration>
<excludes>
<exclude>**/exceptions/*</exclude>
Expand Down Expand Up @@ -210,7 +210,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/br/com/gerencianet/gnsdk/APIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

/**
* 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.
* 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
*
Expand All @@ -24,10 +25,10 @@ public class APIRequest {
private String route;
private JSONObject body;

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

String url = config.getOptions().getString("baseUri") + route;
Expand All @@ -42,6 +43,7 @@ public APIRequest(String method, String route, JSONObject body, Config config) t

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

this.body = body;
Expand All @@ -56,10 +58,8 @@ public APIRequest(Auth auth, Request request, JSONObject body) {
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.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken());
try {
return this.requester.send(this.body);
Expand All @@ -69,6 +69,20 @@ public JSONObject send() throws AuthorizationException, GerencianetException, IO
}
}

public String sendString() throws AuthorizationException, GerencianetException, IOException {
Date expiredDate = this.authenticator.getExpires();
if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) {
this.authenticator.authorize();
}
this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken());
try {
return this.requester.sendString(this.body);
} catch (AuthorizationException e) {
this.authenticator.authorize();
return this.requester.sendString(body);
}
}

public Request getRequester() {
return requester;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/br/com/gerencianet/gnsdk/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public Auth(JSONObject credentials, String method, String authorizeRoute) throws
throw new Exception("Client_Id or Client_Secret not found");
}

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

String url = credentials.getString("baseUri") + authorizeRoute;
Expand Down
52 changes: 15 additions & 37 deletions src/main/java/br/com/gerencianet/gnsdk/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,19 @@
*
*/
public class Config {
private final static String version = "1.0.2";
private final static String version = "2.0.0";
private JSONObject conf = new JSONObject();
private JSONObject endpoints = new JSONObject();
private JSONObject urls = new JSONObject();
private JSONObject options = new JSONObject();

public Config(JSONObject options, JSONObject config) throws Exception {

if (config.has("ENDPOINTS")) {
this.endpoints = (JSONObject) config.get("ENDPOINTS");
if (setPix(options)) {
if (this.endpoints.has("PIX")) {
this.endpoints = (JSONObject) this.endpoints.get("PIX");
}
} else {
if (this.endpoints.has("DEFAULT")) {
this.endpoints = (JSONObject) this.endpoints.get("DEFAULT");
}
}
} else
throw new Exception("Problems to get ENDPOINTS in file config.json");

if (config.has("URL")) {
this.urls = (JSONObject) config.get("URL");
if (setPix(options)) {
if (this.urls.has("PIX")) {
this.urls = (JSONObject) this.urls.get("PIX");
}
} else if (this.urls.has("DEFAULT")) {
this.urls = (JSONObject) this.urls.get("DEFAULT");
}
} else
throw new Exception("Problems to get URLs in file config.json");

this.setConf(options);
this.endpoints = config;
this.options = options;
}

public JSONObject getEndpoints() {
return endpoints;
return (JSONObject)endpoints.get("APIs");
}

public JSONObject getUrls() {
Expand All @@ -71,15 +46,13 @@ 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("pix_cert"))
this.conf.put("certificadoPix", options.getString("pix_cert"));
if (options.has("certificate"))
this.conf.put("certificate", options.getString("certificate"));
if (options.has("partner_token"))
this.conf.put("partnerToken", options.getString("partner_token"));
if (options.has("url")) {
this.conf.put("baseUri", options.getString("url"));
}

else {
} else {
String baseUri = this.urls.getString("production");
if (this.conf.getBoolean("sandbox") == true)
baseUri = this.urls.getString("sandbox");
Expand All @@ -90,6 +63,10 @@ public void setConf(JSONObject options) {
if (options.has("x-skip-mtls-checking")) {
this.conf.put("headers", options.getString("x-skip-mtls-checking"));
}

if (options.has("x-idempotency-key")) {
this.conf.put("headers", options.getString("x-idempotency-key"));
}
}

public JSONObject getOptions() {
Expand All @@ -100,8 +77,9 @@ public static String getVersion() {
return Config.version;
}

public boolean setPix(JSONObject options) {
return options.has("pix_cert");
public void setURLs(JSONObject parURLs) {
this.urls = parURLs;
this.setConf(this.options);
}

}
Loading

0 comments on commit 7463a0d

Please sign in to comment.