-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigParserAPIDemo.java
150 lines (117 loc) · 5.84 KB
/
BigParserAPIDemo.java
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
149
150
/*
* This is a simple java example for BigParser API which shows:
* 1. How to login and obtain an authId
* 2. Fetching the grid headers & data from the Grid using gridId provided :
* 2a. Fetching the grid headers
* 2b. Fetching the grid data
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
public class BigParserAPIDemo {
public static void main(String[] args) {
//1. How to login and obtain an authId <START>
//User login. Please Replace with your respective emailid and password below
String loginRequest = "{\"emailId\": \"[email protected]\"," +
" \"password\": \"apidemo_pwd\"}";
String loginUri = "https://www.bigparser.com/APIServices/api/common/login";
HashMap<String, String> loginHeaders = new HashMap<String, String>();
loginHeaders.put("Content-Type", "application/json");
String loginResponse = makePOSTCall(loginUri, loginHeaders, loginRequest);
System.out.println("LOGIN RESPONSE : " + loginResponse + "\n");
int startOfAuthID = loginResponse.indexOf("authId");
//fetch authId for future calls
String authId = loginResponse.substring(startOfAuthID + 9, startOfAuthID + 45);
System.out.println("authId : " + authId + "\n");
//1. How to login and obtain an authId <END>
//2.Fetching the grid headers & data from the Grid using gridId provided <START>
//Replace with the gridId you want to query
String gridId = "57a34b2de4b05fc193e80f32";
//2a. Get the grid headers
//setting the url for getting grid header
String queryTableHeaderURL = "https://www.bigparser.com/APIServices/api/grid/headers?gridId=" + gridId;
//setting the API request headers
HashMap<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Content-Type", "application/json");
requestHeaders.put("authId", authId);
//Making API call to get the headers of grid
String queryTableHeaderResponse = makeGETCall(queryTableHeaderURL, requestHeaders);
System.out.println("queryTable Header RESPONSE : " + queryTableHeaderResponse + "\n");
//2b. Getting the rows of the grid
//Replace rowcount with the no. of rows you need. You can also customize the queryTableRequest to add additional parameters
String queryTableRequest = "{\"gridId\":\"" + gridId + "\",\"rowCount\":50}";
/*startIndex and endIndex are *Optional*. You can use them in case you need pagination.
In case you are not using the start & end indexes, the queryTableURL would be like :
String queryTableURL = "https://www.bigparser.com/APIServices/api/query/table */
String queryTableURL = "https://www.bigparser.com/APIServices/api/query/table?startIndex=0&endIndex=50";
//Making API call to get the data rows of grid
String queryTableResponse = makePOSTCall(queryTableURL, requestHeaders, queryTableRequest);
System.out.println("queryTable Rows RESPONSE : " + queryTableResponse + "\n");
//2.Fetching the grid headers & data from the Grid using gridId provided <END>
}
//This is a generic java method which can be used for any REST GET API call
private static String makeGETCall(String uri, HashMap<String, String> headers) {
StringBuffer response = new StringBuffer();
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
for (String key : headers.keySet()) {
conn.setRequestProperty(key, headers.get(key));
}
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
response.append(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
//This is a generic java method which can be used for any REST POST API call
private static String makePOSTCall(String uri, HashMap<String, String> headers, String body) {
StringBuffer response = new StringBuffer();
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
for (String key : headers.keySet()) {
conn.setRequestProperty(key, headers.get(key));
}
OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
response.append(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
}