Skip to content
Open
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
40 changes: 20 additions & 20 deletions serpapi/serpapi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,60 +34,60 @@ public SerpApi(Hashtable parameter = null)
this.client = new HttpClient();

// set default timeout to 60s
this.setTimeoutSeconds(60);
this.SetTimeoutSeconds(60);
}

/***
* Set HTTP timeout in seconds
*/
public void setTimeoutSeconds(int seconds)
public void SetTimeoutSeconds(int seconds)
{
this.client.Timeout = TimeSpan.FromSeconds(seconds);
}

/***
* Get Json result
*/
public JObject search(Hashtable parameter)
public JObject Search(Hashtable parameter)
{
return json("/search", parameter);
return Json("/search", parameter);
}

/***
* Get search archive for JSON results
*/
public JObject searchArchive(string searchId)
public JObject SearchArchive(string searchId)
{
return json("/searches/" + searchId + ".json", new Hashtable());
return Json("/searches/" + searchId + ".json", new Hashtable());
}

/***
* Get search HTML results
*/
public string html(Hashtable parameter)
public string Html(Hashtable parameter)
{
return get("/search", parameter, false);
return Get("/search", parameter, false);
}

/***
* Get user account
*/
public JObject account(string apiKey = "")
public JObject Account(string apiKey = "")
{
Hashtable parameter = new Hashtable();
if(apiKey != "") {
parameter.Add("api_key", apiKey);
}
return json("/account", parameter);
return Json("/account", parameter);
}

/***
* Get location using location API
*/
public JArray location(Hashtable parameter)
public JArray Location(Hashtable parameter)
{
// get json result
string buffer = get("/locations.json", parameter, true);
string buffer = Get("/locations.json", parameter, true);
// parse json response (ignore http response status)
try {
JArray data = JArray.Parse(buffer);
Expand All @@ -105,22 +105,22 @@ public JArray location(Hashtable parameter)
}
}

public string get(string endpoint, Hashtable parameter, bool jsonEnabled)
public string Get(string endpoint, Hashtable parameter, bool jsonEnabled)
{
string url = createUrl(endpoint, parameter, jsonEnabled);
string url = CreateUrl(endpoint, parameter, jsonEnabled);
// run asynchonous http query (.net framework implementation)
Task<string> queryTask = createQuery(url, jsonEnabled);
Task<string> queryTask = CreateQuery(url, jsonEnabled);
// block until http query is completed
queryTask.ConfigureAwait(true);
// parse result into json
return queryTask.Result;
}


public JObject json(string uri, Hashtable parameter)
public JObject Json(string uri, Hashtable parameter)
{
// get json result
string buffer = get(uri, parameter, true);
string buffer = Get(uri, parameter, true);
// parse json response (ignore http response status)
JObject data = JObject.Parse(buffer);
// report error if something went wrong
Expand All @@ -137,7 +137,7 @@ public JObject json(string uri, Hashtable parameter)
// - C# URL encoding is pretty buggy and the API provides method which are not functional.
// - System.Web.HttpUtility.UrlEncode breaks if apply the full URL
///
public string createUrl(string endpoint, Hashtable parameter, bool jsonEnabled)
public string CreateUrl(string endpoint, Hashtable parameter, bool jsonEnabled)
{
// merge parameter
Hashtable table = new Hashtable();
Expand Down Expand Up @@ -182,7 +182,7 @@ public void Close()
this.client.Dispose();
}

private async Task<string> createQuery(string url, bool jsonEnabled)
private async Task<string> CreateQuery(string url, bool jsonEnabled)
{
// display url for debug:
//Console.WriteLine("url: " + url);
Expand Down Expand Up @@ -213,7 +213,7 @@ private async Task<string> createQuery(string url, bool jsonEnabled)
// handle HTTP issues
throw new ClientException(ex.ToString());
}
throw new ClientException("Oops something went very wrong");
//throw new ClientException("Oops something went very wrong");
}
}

Expand Down
3 changes: 1 addition & 2 deletions test/account_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class AccountTest
{
private SerpApi client;
private string apiKey;

public AccountTest()
{
apiKey = Environment.GetEnvironmentVariable("API_KEY");
Expand All @@ -28,7 +27,7 @@ public void TestGetAccount()
{
return;
}
JObject account = client.account();
JObject account = client.Account();
Dictionary<string, string> dict = account.ToObject<Dictionary<string, string>>();
Assert.IsNotNull(dict["account_id"]);
Assert.IsNotNull(dict["plan_id"]);
Expand Down
4 changes: 2 additions & 2 deletions test/archive_search_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void TestArchiveSearch()
searchParameter.Add("hl", "en");
searchParameter.Add("google_domain", "google.com");

JObject data = client.search(searchParameter);
JObject data = client.Search(searchParameter);
string id = (string)((JObject)data["search_metadata"])["id"];
JObject archivedSearch = client.searchArchive(id);
JObject archivedSearch = client.SearchArchive(id);
int expected = GetSize((JArray)data["organic_results"]);
int actual = GetSize((JArray)archivedSearch["organic_results"]);
Assert.IsTrue(expected == actual);
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_apple_app_store_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "apple_app_store");
query.Add("term", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_baidu_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "baidu");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_bing_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "bing");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_duckduckgo_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "duckduckgo");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_ebay_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "ebay");
query.Add("_nkw", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_autocomplete_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "google_autocomplete");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray suggestions = (JArray)results["suggestions"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_events_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "google_events");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray events_results = (JArray)results["events_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_images_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestSearch()
query.Add("tbm", "isch");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray images_results = (JArray)results["images_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_jobs_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "google_jobs");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray jobs_results = (JArray)results["jobs_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_local_services_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestSearch()
query.Add("q", "electrician");
query.Add("data_cid", "6745062158417646970");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray local_ads = (JArray)results["local_ads"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_maps_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void TestSearch()
query.Add("ll", "@40.7455096,-74.0083012,15.1z");
query.Add("type", "search");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray local_results = (JArray)results["local_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_play_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestSearch()
query.Add("q", "kite");
query.Add("store", "apps");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_product_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestSearch()
query.Add("q", "coffee");
query.Add("product_id", "4887235756540435899");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

//Console.WriteLine(results);
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_reverse_image_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "google_reverse_image");
query.Add("image_url", "https://i.imgur.com/5bGzZi7.jpg");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray image_sizes = (JArray)results["image_sizes"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_scholar_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "google_scholar");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_google_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "google");
query.Add("q", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_home_depot_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "home_depot");
query.Add("q", "table");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray products = (JArray)results["products"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_naver_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "naver");
query.Add("query", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray ads_results = (JArray)results["ads_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_walmart_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "walmart");
query.Add("query", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_yahoo_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "yahoo");
query.Add("p", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray organic_results = (JArray)results["organic_results"];
Expand Down
2 changes: 1 addition & 1 deletion test/example_search_youtube_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestSearch()
query.Add("engine", "youtube");
query.Add("search_query", "coffee");

JObject results = client.search(query);
JObject results = client.Search(query);
Assert.IsNotNull(results);

JArray video_results = (JArray)results["video_results"];
Expand Down
4 changes: 2 additions & 2 deletions test/google_search_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestSearch()
searchParameter.Add("hl", "en");
searchParameter.Add("google_domain", "google.com");

JObject data = client.search(searchParameter);
JObject data = client.Search(searchParameter);
JArray coffeeShops = (JArray)data["local_results"]["places"];
int counter = 0;
foreach (JObject coffeeShop in coffeeShops)
Expand Down Expand Up @@ -68,7 +68,7 @@ public void TestHtml()
searchParameter.Add("q", "Coffee");
searchParameter.Add("hl", "en");
searchParameter.Add("google_domain", "google.com");
string htmlContent = client.html(searchParameter);
string htmlContent = client.Html(searchParameter);
Assert.IsNotNull(htmlContent);
//Console.WriteLine(htmlContent);
Assert.IsTrue(htmlContent.Contains("</body>"));
Expand Down
2 changes: 1 addition & 1 deletion test/location_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void TestLocation()
Hashtable locationParameter = new Hashtable();
locationParameter.Add("q", "Austin,TX");
locationParameter.Add("limit", "5");
JArray locations = client.location(locationParameter);
JArray locations = client.Location(locationParameter);
int counter = 0;
foreach (JObject location in locations)
{
Expand Down
6 changes: 3 additions & 3 deletions test/serpapi_search_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void TestSpecialCharactersEncoding()
client.defaultParameter.Remove("google_domain");
client.defaultParameter.Add("q", q);

string encoded = client.createUrl("/search", client.defaultParameter, false);
string encoded = client.CreateUrl("/search", client.defaultParameter, false);
string qEncoded = "q=%c2%a1+%e2%84%a2%c2%a3%c2%a2%e2%88%9e%c2%a7%c2%b6%e2%80%a2%c2%aa%c2%ba%e2%80%93%e2%89%a0%c5%93%e2%88%91%c2%b4%c2%ae%e2%80%a0%c2%a5%c2%a8%cb%86%c3%b8%cf%80%e2%80%9c%e2%80%98%c2%ab%c3%a5%c3%9f%e2%88%82%c6%92%c2%a9%c3%a5%c3%9f%e2%88%82%c6%92%c2%a9%cb%9a%c2%ac%e2%80%a6%c3%a6%e2%89%88%c3%a7%e2%88%9a%e2%88%ab%cb%9c%c2%b5%e2%89%a4%e2%89%a5%c3%b7%60%e2%81%84%e2%82%ac%e2%80%b9%e2%80%ba%ef%ac%81%ef%ac%82%e2%80%a1%c2%b0%c2%b7%e2%80%9a%e2%80%94%c2%b1%c5%92%e2%80%9e%c2%b4%e2%80%b0%cb%87%c3%81%c2%a8%c3%98%e2%88%8f%e2%80%9d%e2%80%99%2f*+%c3%8d%c3%8e%cb%9d%c3%93%c3%94%ef%a3%bf%c3%92%c3%9a%c3%86%c2%b8%cb%9b%c3%87%e2%97%8a%c4%b1%cb%9c%c3%82%c2%af%cb%98%c2%bf%5cn*%2f%c4%80%c4%81%c4%92%c4%93%c4%aa%c4%ab%c5%8c%c5%8d%c5%aa%c5%ab";
Assert.IsTrue(encoded.Contains(qEncoded));
}
Expand All @@ -70,7 +70,7 @@ public void TestSearchModifiedSpecialCharacters()
ht.Add("engine", "google");
ht.Add("api_key", apiKey);
SerpApi client = new SerpApi(ht);
JObject data = client.search(ht);
JObject data = client.Search(ht);
dynamic first = ((JArray)data["organic_results"])[0];
Assert.AreEqual<string>((string)first["position"], "1");

Expand All @@ -79,7 +79,7 @@ public void TestSearchModifiedSpecialCharacters()
ht.Add("q", "blak ˈkôfē !");

// run client 2
JObject data_ = client.search(ht);
JObject data_ = client.Search(ht);
dynamic first_ = ((JArray)data_["organic_results"])[0];
Assert.AreEqual<string>((string)first_["position"], "1");

Expand Down