-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerpApiHttp.java
More file actions
153 lines (132 loc) · 3.73 KB
/
Copy pathSerpApiHttp.java
File metadata and controls
153 lines (132 loc) · 3.73 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
149
150
151
152
153
package serpapi;
import java.io.*;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
/**
* HTTPS client for Serp API
*/
public class SerpApiHttp {
// http request configuration
private int httpConnectionTimeout = 60000;
private int httpReadTimeout = 60000;
/**
* current API version
*/
public static String VERSION = "1.2.0";
/**
* backend service
*/
public static String BACKEND = "https://serpapi.com";
/**
* initialize gson
*/
private static Gson gson = new Gson();
/**
* current backend HTTP path
*/
public String path;
/**
* HTTP client
*/
private HttpClient httpClient;
/***
* Constructor
* @param path HTTP url path
*/
public SerpApiHttp(String path) {
this.path = path;
this.httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofMillis(httpConnectionTimeout))
.build();
}
/***
* Returns HTTP response content as a raw String
*
* @param parameter user client parameters
* @return http response body
* @throws SerpApiException wraps error message
*/
public String get(Map<String, String> parameter) throws SerpApiException {
String query;
try {
query = ParameterStringBuilder.getParamsString(parameter);
} catch (UnsupportedEncodingException e) {
throw new SerpApiException(e);
}
URI uri = URI.create(BACKEND + path + "?" + query);
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.uri(uri)
.timeout(Duration.ofMillis(httpReadTimeout))
.GET();
String outputFormat = parameter.get("output");
if (outputFormat != null && outputFormat.startsWith("json")) {
requestBuilder.header("Content-Type", "application/json");
}
try {
HttpResponse<String> response = httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
triggerSerpApiException(response.body());
}
return response.body();
} catch (IOException | InterruptedException e) {
throw new SerpApiException(e);
}
}
/**
* trigger a exception on error
* @param content raw JSON response from serpapi.com
* @throws SerpApiException wraps error message
*/
protected void triggerSerpApiException(String content) throws SerpApiException {
String errorMessage;
try {
JsonObject element = gson.fromJson(content, JsonObject.class);
errorMessage = element.get("error").getAsString();
} catch (Exception e) {
throw new SerpApiException("invalid response format: " + content);
}
throw new SerpApiException(errorMessage);
}
/**
* Get the HTTP connection timeout
*
* @return current HTTP connection timeout
*/
public int getHttpConnectionTimeout() {
return httpConnectionTimeout;
}
/**
* Set the HTTP connection timeout
*
* @param httpConnectionTimeout set HTTP connection timeout
*/
public void setHttpConnectionTimeout(int httpConnectionTimeout) {
this.httpConnectionTimeout = httpConnectionTimeout;
// Recreate client with new timeout
this.httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofMillis(httpConnectionTimeout))
.build();
}
/**
* Get the HTTP read timeout
*
* @return current HTTP read timeout
*/
public int getHttpReadTimeout() {
return httpReadTimeout;
}
/**
* Set the HTTP read timeout
*
* @param httpReadTimeout set HTTP read timeout
*/
public void setHttpReadTimeout(int httpReadTimeout) {
this.httpReadTimeout = httpReadTimeout;
}
}