forked from SeunAdelekan/PaystackJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiConnection.java
More file actions
148 lines (133 loc) · 4.62 KB
/
ApiConnection.java
File metadata and controls
148 lines (133 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONObject;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
/**
* Created by Iyanu on 17/07/2016.
*/
public class ApiConnection {
public ApiConnection(String url) {
this.url = url;
keys = new Keys();
try {
keys.initKeys();
} catch (FileNotFoundException e) {
System.out.print("Keys.json could not be found");
e.printStackTrace();
}
this.apiKey = keys.KEY_IN_USE;
}
private Keys keys;
private String url;
private String apiKey;
/*
The following methods are used to send API POST REQUESTS
*/
public JSONObject connectAndQuery(ApiQuery query) {
try {
HttpResponse<JsonNode> queryForResponse = Unirest.post(url)
.header("Accept","application/json")
.header("Authorization","Bearer "+apiKey)
.fields(query.getParams())
.asJson();
return queryForResponse.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
public JSONObject connectAndQuery(HashMap<String,Object> query) {
try {
HttpResponse<JsonNode> queryForResponse = Unirest.post(url)
.header("Accept","application/json")
.header("Authorization","Bearer "+apiKey)
.fields(query)
.asJson();
return queryForResponse.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
/*
The following methods are used to send API GET REQUESTS
*/
public JSONObject connectAndQueryWithGet(){
try {
HttpResponse<JsonNode> queryForResponse = Unirest.get(url)
.header("Accept","application/json")
.header("Authorization","Bearer "+apiKey)
.asJson();
return queryForResponse.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
public JSONObject connectAndQueryWithGet(ApiQuery query){
try {
HttpResponse<JsonNode> queryForResponse = Unirest.get(url)
.header("Accept","application/json")
.header("Authorization","Bearer "+apiKey)
.queryString(query.getParams())
.asJson();
return queryForResponse.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
public JSONObject connectAndQueryWithGet(HashMap<String,Object> query) {
try {
HttpResponse<JsonNode> queryForResponse = Unirest.get(url)
.header("Accept","application/json")
.header("Authorization","Bearer "+apiKey)
.queryString(query)
.asJson();
return queryForResponse.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
/*
The following methods are used to send API PUT REQUESTS
*/
public JSONObject connectAndQueryWithPut(ApiQuery query) {
try {
HttpResponse<JsonNode> queryForResponse = Unirest.put(url)
.header("Accept","application/json")
.header("Authorization","Bearer "+apiKey)
.fields(query.getParams())
.asJson();
return queryForResponse.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
public JSONObject connectAndQueryWithPut(HashMap<String,Object> query) {
try {
HttpResponse<JsonNode> queryForResponse = Unirest.get(url)
.header("Accept","application/json")
.header("Authorization","Bearer "+apiKey)
.queryString(query)
.asJson();
return queryForResponse.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
public static void shutDown() {
try {
Unirest.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
}