Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ SerpApi client = new SerpApi(auth);
// run search
Map<String, String> parameter = new HashMap<>();
parameter.put("engine", "google_events");
parameter.put("q", "coffee");
parameter.put("q", "Events in Austin, TX");
JsonObject results = client.search(parameter);
JsonArray events = results.getAsJsonArray("events_results");
System.out.println(results.toString());
```

Expand Down Expand Up @@ -492,7 +493,6 @@ see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)

### Search google play
```java

// setup serpapi client
Map<String, String> auth = new HashMap<>();
auth.put("api_key", "your_api_key");
Expand Down
6 changes: 5 additions & 1 deletion README.md.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<%-
def snippet(format, path)
lines = File.new(path).readlines
start = lines.find_index { |line| line.include?('// setup serpapi client') }
if start.nil?
raise "No '// setup serpapi client' marker found in #{path}"
end
stop = lines.find_index do |line|
next false if line.lstrip.start_with?('//')
line.match?(/\bassert(True|NotNull)\s*\(/)
end
if stop.nil?
raise "No assertTrue/assertNotNull found in #{path}"
end
slice = lines[23..stop-1]
slice = lines[start..stop-1]
slice << "System.out.println(results.toString());"
buf = slice.map { |l| l.gsub(/(^\s\s\s\s)/, '')}.join
buf.gsub!("System.getenv(\"SERPAPI_KEY\")", "\"your_api_key\"")
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/serpapi/SerpApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ public class SerpApi {
*/
public SerpApiHttp client;

/**
* default HTTP client timeout
/**
* default HTTP client timeout
*
* Applied to both connecting and reading. Some engines, home_depot in
* particular, regularly take longer than a minute to respond.
*/
public Integer timeout = 60000;
public Integer timeout = 120000;

/***
* Constructor
Expand All @@ -40,6 +43,7 @@ public SerpApi(Map<String, String> parameter) {
this.parameter = parameter;
this.client = new SerpApiHttp("/search");
this.client.setHttpConnectionTimeout(this.timeout);
this.client.setHttpReadTimeout(this.timeout);
}

/***
Expand All @@ -49,6 +53,7 @@ public SerpApi() {
this.parameter = new HashMap<>();
this.client = new SerpApiHttp("/search");
this.client.setHttpConnectionTimeout(this.timeout);
this.client.setHttpReadTimeout(this.timeout);
}

/***
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/serpapi/TimeoutTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package serpapi;

import org.junit.Test;

import java.util.HashMap;

import static org.junit.Assert.*;

/**
* Test that the client timeout is applied to reading as well as connecting.
*
* Offline: only client configuration is inspected, so no network call and no
* SERPAPI_KEY are needed.
*/
public class TimeoutTest {

@Test
public void appliesTimeoutToBothConnectAndRead() {
SerpApi serpapi = new SerpApi(new HashMap<>());

assertEquals(serpapi.timeout.intValue(), serpapi.getClient().getHttpConnectionTimeout());
assertEquals(serpapi.timeout.intValue(), serpapi.getClient().getHttpReadTimeout());
}

@Test
public void defaultTimeoutSurvivesTheSlowestEngines() {
// home_depot has repeatedly taken longer than a minute to respond.
assertTrue(new SerpApi().timeout > 60000);
}
}
Loading