diff --git a/README.md b/README.md index daf088a..ae328d7 100644 --- a/README.md +++ b/README.md @@ -444,8 +444,9 @@ SerpApi client = new SerpApi(auth); // run search Map 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()); ``` @@ -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 auth = new HashMap<>(); auth.put("api_key", "your_api_key"); diff --git a/README.md.erb b/README.md.erb index 85e9d8f..945b5cb 100644 --- a/README.md.erb +++ b/README.md.erb @@ -1,6 +1,10 @@ <%- 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*\(/) @@ -8,7 +12,7 @@ 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\"") diff --git a/src/main/java/serpapi/SerpApi.java b/src/main/java/serpapi/SerpApi.java index 2a1cab4..5a1d8af 100644 --- a/src/main/java/serpapi/SerpApi.java +++ b/src/main/java/serpapi/SerpApi.java @@ -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 @@ -40,6 +43,7 @@ public SerpApi(Map parameter) { this.parameter = parameter; this.client = new SerpApiHttp("/search"); this.client.setHttpConnectionTimeout(this.timeout); + this.client.setHttpReadTimeout(this.timeout); } /*** @@ -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); } /*** diff --git a/src/test/java/serpapi/TimeoutTest.java b/src/test/java/serpapi/TimeoutTest.java new file mode 100644 index 0000000..90ef1cb --- /dev/null +++ b/src/test/java/serpapi/TimeoutTest.java @@ -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); + } +}