|
5 | 5 | import java.net.URL;
|
6 | 6 | import java.util.Date;
|
7 | 7 |
|
| 8 | +import org.json.JSONArray; |
8 | 9 | import org.json.JSONObject;
|
9 | 10 |
|
10 | 11 | import br.com.efi.efisdk.exceptions.AuthorizationException;
|
11 | 12 | import br.com.efi.efisdk.exceptions.EfiPayException;
|
12 | 13 |
|
13 | 14 | /**
|
14 |
| - * This class instance a Auth Object, to authenticate client credentials in |
15 |
| - * efi API. After client's credentials |
16 |
| - * are validated a client Object send a given request body to a given endpoint |
17 |
| - * throw a given route. |
18 |
| - * |
| 15 | + * This class instance a Auth Object, to authenticate client credentials in efi |
| 16 | + * API. After client's credentials are validated a client Object send a given |
| 17 | + * request body to a given endpoint throw a given route. |
| 18 | + * |
19 | 19 | * @author Consultoria Técnica
|
20 | 20 | *
|
21 | 21 | */
|
22 | 22 | public class APIRequest {
|
23 |
| - private Request requester; |
24 |
| - private Auth authenticator; |
25 |
| - private String route; |
26 |
| - private JSONObject body; |
27 |
| - |
28 |
| - public APIRequest(String method, String route, JSONObject body, JSONObject auth, Config config) throws Exception { |
29 |
| - this.route = route; |
30 |
| - String authenticateRoute = auth.getString("route"); |
31 |
| - String authenticateMethod = auth.getString("method"); |
32 |
| - this.authenticator = new Auth(config.getOptions(), authenticateMethod, authenticateRoute); |
33 |
| - |
34 |
| - String url = config.getOptions().getString("baseUri") + route; |
35 |
| - URL link = new URL(url); |
36 |
| - HttpURLConnection client = (HttpURLConnection) link.openConnection(); |
37 |
| - |
38 |
| - this.requester = new Request(method, client); |
39 |
| - |
40 |
| - if (config.getOptions().has("partnerToken")) { |
41 |
| - this.requester.addHeader("partner-token", config.getOptions().getString("partnerToken")); |
42 |
| - } |
43 |
| - |
44 |
| - if (config.getOptions().has("headers")) { |
45 |
| - this.requester.addHeader("x-skip-mtls-checking", config.getOptions().getString("headers")); |
46 |
| - this.requester.addHeader("x-idempotency-key", config.getOptions().getString("headers")); |
47 |
| - } |
48 |
| - |
49 |
| - this.body = body; |
50 |
| - } |
51 |
| - |
52 |
| - public APIRequest(Auth auth, Request request, JSONObject body) { |
53 |
| - this.authenticator = auth; |
54 |
| - this.requester = request; |
55 |
| - this.body = body; |
56 |
| - } |
57 |
| - |
58 |
| - public JSONObject send() throws AuthorizationException, EfiPayException, IOException { |
59 |
| - Date expiredDate = this.authenticator.getExpires(); |
60 |
| - if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) { |
61 |
| - this.authenticator.authorize(); |
62 |
| - } |
63 |
| - this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken()); |
64 |
| - try { |
65 |
| - return this.requester.send(this.body); |
66 |
| - } catch (AuthorizationException e) { |
67 |
| - this.authenticator.authorize(); |
68 |
| - return this.requester.send(body); |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - public String sendString() throws AuthorizationException, EfiPayException, IOException { |
73 |
| - Date expiredDate = this.authenticator.getExpires(); |
74 |
| - if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) { |
75 |
| - this.authenticator.authorize(); |
76 |
| - } |
77 |
| - this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken()); |
78 |
| - try { |
79 |
| - return this.requester.sendString(this.body); |
80 |
| - } catch (AuthorizationException e) { |
81 |
| - this.authenticator.authorize(); |
82 |
| - return this.requester.sendString(body); |
83 |
| - } |
84 |
| - } |
85 |
| - |
86 |
| - public Request getRequester() { |
87 |
| - return requester; |
88 |
| - } |
89 |
| - |
90 |
| - public String getRoute() { |
91 |
| - return route; |
92 |
| - } |
93 |
| - |
94 |
| - public JSONObject getBody() { |
95 |
| - return body; |
96 |
| - } |
| 23 | + |
| 24 | + private Request requester; |
| 25 | + private Auth authenticator; |
| 26 | + private String route; |
| 27 | + private JSONObject body; |
| 28 | + private JSONArray bodyArray; |
| 29 | + |
| 30 | + public APIRequest(String method, String route, JSONObject body, JSONObject auth, Config config) throws Exception { |
| 31 | + this.route = route; |
| 32 | + String authenticateRoute = auth.getString("route"); |
| 33 | + String authenticateMethod = auth.getString("method"); |
| 34 | + this.authenticator = new Auth(config.getOptions(), authenticateMethod, authenticateRoute); |
| 35 | + |
| 36 | + String url = config.getOptions().getString("baseUri") + route; |
| 37 | + URL link = new URL(url); |
| 38 | + HttpURLConnection client = (HttpURLConnection) link.openConnection(); |
| 39 | + |
| 40 | + this.requester = new Request(method, client); |
| 41 | + |
| 42 | + if (config.getOptions().has("partnerToken")) { |
| 43 | + this.requester.addHeader("partner-token", config.getOptions().getString("partnerToken")); |
| 44 | + } |
| 45 | + |
| 46 | + if (config.getOptions().has("headers")) { |
| 47 | + this.requester.addHeader("x-skip-mtls-checking", config.getOptions().getString("headers")); |
| 48 | + this.requester.addHeader("x-idempotency-key", config.getOptions().getString("headers")); |
| 49 | + } |
| 50 | + |
| 51 | + this.body = body; |
| 52 | + } |
| 53 | + |
| 54 | + public APIRequest(String method, String route, JSONArray bodyArray, JSONObject auth, Config config) throws Exception { |
| 55 | + this.route = route; |
| 56 | + String authenticateRoute = auth.getString("route"); |
| 57 | + String authenticateMethod = auth.getString("method"); |
| 58 | + this.authenticator = new Auth(config.getOptions(), authenticateMethod, authenticateRoute); |
| 59 | + |
| 60 | + String url = config.getOptions().getString("baseUri") + route; |
| 61 | + URL link = new URL(url); |
| 62 | + HttpURLConnection client = (HttpURLConnection) link.openConnection(); |
| 63 | + |
| 64 | + this.requester = new Request(method, client); |
| 65 | + this.bodyArray = bodyArray; |
| 66 | + } |
| 67 | + |
| 68 | + public APIRequest(Auth auth, Request request, JSONObject body) { |
| 69 | + this.authenticator = auth; |
| 70 | + this.requester = request; |
| 71 | + this.body = body; |
| 72 | + } |
| 73 | + |
| 74 | + public JSONObject send() throws AuthorizationException, EfiPayException, IOException { |
| 75 | + Date expiredDate = this.authenticator.getExpires(); |
| 76 | + if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) { |
| 77 | + this.authenticator.authorize(); |
| 78 | + } |
| 79 | + this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken()); |
| 80 | + try { |
| 81 | + return this.requester.send(this.body); |
| 82 | + } catch (AuthorizationException e) { |
| 83 | + this.authenticator.authorize(); |
| 84 | + return this.requester.send(body); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public String sendString() throws AuthorizationException, EfiPayException, IOException { |
| 89 | + Date expiredDate = this.authenticator.getExpires(); |
| 90 | + if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) { |
| 91 | + this.authenticator.authorize(); |
| 92 | + } |
| 93 | + this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken()); |
| 94 | + try { |
| 95 | + return this.requester.sendString(this.body); |
| 96 | + } catch (AuthorizationException e) { |
| 97 | + this.authenticator.authorize(); |
| 98 | + return this.requester.sendString(body); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public JSONArray sendArray() throws AuthorizationException, EfiPayException, IOException { |
| 103 | + Date expiredDate = this.authenticator.getExpires(); |
| 104 | + if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) { |
| 105 | + this.authenticator.authorize(); |
| 106 | + } |
| 107 | + this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken()); |
| 108 | + |
| 109 | + return this.requester.sendArray(this.bodyArray); |
| 110 | + } |
| 111 | + |
| 112 | + public Request getRequester() { |
| 113 | + return requester; |
| 114 | + } |
| 115 | + |
| 116 | + public String getRoute() { |
| 117 | + return route; |
| 118 | + } |
| 119 | + |
| 120 | + public JSONObject getBody() { |
| 121 | + return body; |
| 122 | + } |
97 | 123 | }
|
0 commit comments