Skip to content

Commit 8503411

Browse files
committed
passing tests after major code update.
1 parent b859129 commit 8503411

13 files changed

+62
-463
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99

1010
// Package default
1111
archivesBaseName = 'serpapi'
12-
version = '2.0.3'
12+
version = '1.0.0'
1313
group = 'com.github.serpapi'
1414

1515
// java version

gradle/wrapper/gradle-wrapper.jar

58.1 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

src/main/java/serpapi/SerpApi.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,23 @@ public SerpApi(Map<String, String> parameter) {
3838
this.parameter = parameter;
3939
}
4040

41+
/***
42+
* Constructor
43+
*
44+
* @param parameter default search parameter should include {"api_key": "secret_api_key", "engine": "google" }
45+
*/
46+
public SerpApi() {
47+
this.parameter = new HashMap();
48+
}
49+
4150
/***
4251
* Returns HTML search results as a raw HTML String
4352
*
4453
* @param parameter html search parameter
4554
* @return raw HTML response from the client engine for custom parsing
4655
* @throws SerpApiException wraps backend error message
4756
*/
48-
public String html(HashMap<String, String> parameter) throws SerpApiException {
57+
public String html(Map<String, String> parameter) throws SerpApiException {
4958
return get("/client", "html", parameter);
5059
}
5160

@@ -56,8 +65,8 @@ public String html(HashMap<String, String> parameter) throws SerpApiException {
5665
* @return JsonObject search results parent node
5766
* @throws SerpApiException wraps backend error message
5867
*/
59-
public JsonObject search(HashMap<String, String> parameter) throws SerpApiException {
60-
return json("/client", parameter);
68+
public JsonObject search(Map<String, String> parameter) throws SerpApiException {
69+
return json("/search", parameter);
6170
}
6271

6372
/***
@@ -67,7 +76,7 @@ public JsonObject search(HashMap<String, String> parameter) throws SerpApiExcept
6776
* @return JsonObject location using Location API
6877
* @throws SerpApiException wraps backend error message
6978
*/
70-
public JsonObject location(HashMap<String, String> parameter) throws SerpApiException {
79+
public JsonObject location(Map<String, String> parameter) throws SerpApiException {
7180
return json("/locations.json", parameter);
7281
}
7382

@@ -88,21 +97,33 @@ public JsonObject searchArchive(String clientID) throws SerpApiException {
8897
/***
8998
* Get account information using Account API
9099
*
91-
* @param parameter HashMap including the api_key if not set in the default client parameter
100+
* @param parameter Map including the api_key if not set in the default client parameter
92101
* @return JsonObject account information
93102
* @throws SerpApiException wraps backend error message
94103
*/
95-
public JsonObject account(HashMap<String, String> parameter) throws SerpApiException {
104+
public JsonObject account(Map<String, String> parameter) throws SerpApiException {
96105
return json("/account.json", parameter);
97106
}
98107

108+
109+
/***
110+
* Get account information using Account API
111+
*
112+
* @param parameter Map including the api_key if not set in the default client parameter
113+
* @return JsonObject account information
114+
* @throws SerpApiException wraps backend error message
115+
*/
116+
public JsonObject account() throws SerpApiException {
117+
return json("/account.json", this.parameter);
118+
}
119+
99120
/***
100121
* Convert HTTP content to JsonValue
101122
*
102123
* @param content raw JSON HTTP response
103124
* @return JsonObject created by gson parser
104125
*/
105-
private JsonObject json(String endpoint, HashMap<String, String> parameter) throws SerpApiException {
126+
private JsonObject json(String endpoint, Map<String, String> parameter) throws SerpApiException {
106127
String content = get(endpoint, "json", parameter);
107128
JsonElement element = gson.fromJson(content, JsonElement.class);
108129
return element.getAsJsonObject();
@@ -124,7 +145,7 @@ public SerpApiHttp getClient() {
124145
* @return format parameter hash map
125146
* @throws SerpApiException wraps backend error message
126147
*/
127-
public String get(String path, String output, HashMap<String, String> parameter) throws SerpApiException {
148+
public String get(String path, String output, Map<String, String> parameter) throws SerpApiException {
128149
// Initialize client if not done
129150
if (this.client == null) {
130151
this.client = new SerpApiHttp(path);
@@ -133,7 +154,7 @@ public String get(String path, String output, HashMap<String, String> parameter)
133154
this.client.path = path;
134155
}
135156

136-
HashMap<String, String> query = new HashMap();
157+
Map<String, String> query = new HashMap();
137158
// Merge default parameter
138159
query.putAll(this.parameter);
139160

src/test/java/serpapi/AccountApiTest.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,25 @@
66
import org.mockito.ArgumentMatchers;
77

88
import java.nio.file.Paths;
9+
import java.util.HashMap;
10+
import java.util.Map;
911

1012
import static org.junit.Assert.*;
1113
import static org.mockito.Mockito.*;
1214

1315
public class AccountApiTest {
1416

15-
@Before
16-
public void setUp() throws Exception {
17-
if (System.getenv("API_KEY") != null) {
18-
GoogleSearch.api_key_default = System.getenv("API_KEY");
19-
}
20-
}
21-
2217
@Test
23-
public void getAccount() throws Exception {
24-
String expected_api_key = GoogleSearch.api_key_default;
25-
26-
// mock response if run on github
27-
GoogleSearch client = new GoogleSearch();
18+
public void account() throws Exception {
2819
if (System.getenv("API_KEY") == null) {
29-
SerpApiHttp stub = mock(SerpApiHttp.class);
30-
String data = ReadJsonFile.readAsString(Paths.get("src/test/java/serpapi/data/account.json"));
31-
when(stub.getResults(ArgumentMatchers.<String, String>anyMap())).thenReturn(data);
32-
client.client = stub;
33-
34-
// fallback to default
35-
expected_api_key = "demo";
20+
return;
3621
}
3722

38-
JsonObject info = client.getAccount();
23+
Map<String, String> parameter = new HashMap<String, String>();
24+
parameter.put("api_key", System.getenv("API_KEY"));
25+
SerpApi client = new SerpApi(parameter);
26+
JsonObject info = client.account();
3927
System.out.println(info.toString());
40-
assertEquals(expected_api_key, info.getAsJsonObject().get("api_key").getAsString());
28+
assertEquals("Active", info.getAsJsonObject().get("account_status").getAsString());
4129
}
4230
}

src/test/java/serpapi/GoogleSearchClientTest.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/test/java/serpapi/GoogleSearchFullTest.java

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/test/java/serpapi/GoogleSearchImplementationTest.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)