|
1
|
|
|
|
|
2
|
|
|
package com.github.theresasogunle; |
|
3
|
|
|
|
|
4
|
|
|
import com.mashape.unirest.http.HttpResponse; |
|
5
|
|
|
import com.mashape.unirest.http.JsonNode; |
|
6
|
|
|
import com.mashape.unirest.http.Unirest; |
|
7
|
|
|
import com.mashape.unirest.http.exceptions.UnirestException; |
|
8
|
|
|
import org.json.JSONObject; |
|
9
|
|
|
import java.security.KeyManagementException; |
|
10
|
|
|
import java.security.NoSuchAlgorithmException; |
|
11
|
|
|
import java.util.HashMap; |
|
12
|
|
|
import java.util.logging.Level; |
|
13
|
|
|
import java.util.logging.Logger; |
|
14
|
|
|
import javax.net.ssl.SSLContext; |
|
15
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|
16
|
|
|
import static org.apache.http.conn.ssl.SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER; |
|
17
|
|
|
import org.apache.http.conn.ssl.SSLContexts; |
|
18
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
19
|
|
|
import org.apache.http.impl.client.HttpClients; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Theresa |
|
25
|
|
|
*/ |
|
26
|
|
|
public class ApiConnection { |
|
27
|
|
|
|
|
28
|
|
|
final private String url; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param url - Flutterwave API url |
|
32
|
|
|
*/ |
|
33
|
|
|
public ApiConnection(String url) { |
|
34
|
|
|
this.url = url; |
|
35
|
|
|
this.enforceTlsV1point2(); |
|
36
|
|
|
} |
|
37
|
|
|
private void enforceTlsV1point2() { |
|
38
|
|
|
try { |
|
39
|
|
|
SSLContext sslContext = SSLContexts.custom() |
|
40
|
|
|
.useTLS() |
|
41
|
|
|
.build(); |
|
42
|
|
|
SSLConnectionSocketFactory f = new SSLConnectionSocketFactory( |
|
43
|
|
|
sslContext, |
|
44
|
|
|
new String[]{"TLSv1.2"}, |
|
45
|
|
|
null, |
|
46
|
|
|
BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); |
|
47
|
|
|
CloseableHttpClient httpClient = HttpClients.custom() |
|
48
|
|
|
.setSSLSocketFactory(f) |
|
49
|
|
|
.build(); |
|
50
|
|
|
Unirest.setHttpClient(httpClient); |
|
51
|
|
|
|
|
52
|
|
|
} catch (NoSuchAlgorithmException | KeyManagementException ex) { |
|
53
|
|
|
Logger.getLogger(ApiConnection.class.getName()).log(Level.SEVERE, null, ex); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Connects to and queries Flutterwave API with POST |
|
60
|
|
|
* |
|
61
|
|
|
* @param query - APIQuery containing parameters to send |
|
62
|
|
|
* @return - JSONObject containing API response |
|
63
|
|
|
*/ |
|
64
|
|
|
public JSONObject connectAndQuery(ApiQuery query) { |
|
65
|
|
|
try { |
|
66
|
|
|
HttpResponse<JsonNode> queryForResponse = Unirest.post(url) |
|
67
|
|
|
.header("Accept", "application/json") |
|
68
|
|
|
.fields(query.getParams()) |
|
69
|
|
|
.asJson(); |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
try{ |
|
74
|
|
|
return queryForResponse.getBody().getObject(); |
|
75
|
|
|
}catch(Exception ex){} |
|
|
|
|
|
|
76
|
|
|
} catch (UnirestException e) { |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
/** |
|
81
|
|
|
* Connects to and queries API with POST |
|
82
|
|
|
* |
|
83
|
|
|
* @param query - HashMap containing parameters to send |
|
84
|
|
|
* @return - JSONObject containing API response |
|
85
|
|
|
*/ |
|
86
|
|
|
public JSONObject connectAndQuery(HashMap<String, Object> query) { |
|
|
|
|
|
|
87
|
|
|
try { |
|
88
|
|
|
HttpResponse<JsonNode> queryForResponse = Unirest.post(url) |
|
89
|
|
|
.header("Accept", "application/json") |
|
90
|
|
|
|
|
91
|
|
|
.fields(query) |
|
92
|
|
|
.asJson(); |
|
93
|
|
|
return queryForResponse.getBody().getObject(); |
|
94
|
|
|
} catch (UnirestException e) { |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Used to send a GET request to the Flutterwave API |
|
101
|
|
|
* |
|
102
|
|
|
* @return - JSONObject containing the API response |
|
103
|
|
|
*/ |
|
104
|
|
|
public JsonNode connectAndQueryWithGet() { |
|
105
|
|
|
try { |
|
106
|
|
|
HttpResponse<JsonNode> queryForResponse = Unirest.get(url) |
|
107
|
|
|
.header("content-type", "application/json") |
|
108
|
|
|
|
|
109
|
|
|
.asJson(); |
|
110
|
|
|
|
|
111
|
|
|
return queryForResponse.getBody(); |
|
112
|
|
|
|
|
113
|
|
|
} catch (UnirestException e) { |
|
|
|
|
|
|
114
|
|
|
System.out.println("Cant query at this time!"); |
|
115
|
|
|
} |
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
The Java specification lists the customary order.