Skip to content

Commit fdc9a56

Browse files
author
Kenneth Reitz
committed
new function signatures, per @hartator's suggestion
1 parent d658e87 commit fdc9a56

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

serpapi/core.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class Client(HTTPClient):
2828
def __repr__(self):
2929
return "<SerpApi Client>"
3030

31-
def search(self, **params):
31+
def search(self, params=None, **kwargs):
3232
"""Fetch a page of results from SerpApi. Returns a :class:`SerpResults <serpapi.client.SerpResults>` object, or unicode text (*e.g.* if ``'output': 'html'`` was passed).
3333
34-
The following two calls are equivalent:
34+
The following three calls are equivalent:
3535
3636
.. code-block:: python
3737
@@ -42,6 +42,11 @@ def search(self, **params):
4242
>>> params = {"q": "Coffee", "location": "Austin, Texas, United States"}
4343
>>> s = serpapi.search(**params)
4444
45+
.. code-block:: python
46+
47+
>>> params = {"q": "Coffee", "location": "Austin, Texas, United States"}
48+
>>> s = serpapi.search(params)
49+
4550
4651
:param q: typically, this is the parameter for the search engine query.
4752
:param engine: the search engine to use. Defaults to ``google``.
@@ -52,12 +57,17 @@ def search(self, **params):
5257
5358
**Learn more**: https://serpapi.com/search-api
5459
"""
60+
if params is None:
61+
params = {}
62+
63+
if kwargs:
64+
params.update(kwargs)
5565

5666
r = self.request("GET", "/search", params=params)
5767

5868
return SerpResults.from_http_response(r, client=self)
5969

60-
def search_archive(self, **params):
70+
def search_archive(self, params=None, **kwargs):
6171
"""Get a result from the SerpApi Search Archive API.
6272
6373
:param search_id: the Search ID of the search to retrieve from the archive.
@@ -67,6 +77,12 @@ def search_archive(self, **params):
6777
6878
**Learn more**: https://serpapi.com/search-archive-api
6979
"""
80+
if params is None:
81+
params = {}
82+
83+
if kwargs:
84+
params.update(kwargs)
85+
7086

7187
try:
7288
search_id = params["search_id"]
@@ -78,7 +94,7 @@ def search_archive(self, **params):
7894
r = self.request("GET", f"/searches/{ search_id }", params=params)
7995
return SerpResults.from_http_response(r, client=self)
8096

81-
def locations(self, **params):
97+
def locations(self, params=None, **kwargs):
8298
"""Get a list of supported Google locations.
8399
84100
@@ -88,6 +104,11 @@ def locations(self, **params):
88104
89105
**Learn more**: https://serpapi.com/locations-api
90106
"""
107+
if params is None:
108+
params = {}
109+
110+
if kwargs:
111+
params.update(kwargs)
91112

92113
r = self.request(
93114
"GET",
@@ -99,7 +120,8 @@ def locations(self, **params):
99120

100121
def account(
101122
self,
102-
**params,
123+
params=None,
124+
**kwargs,
103125
):
104126
"""Get SerpApi account information.
105127
@@ -109,10 +131,16 @@ def account(
109131
**Learn more**: https://serpapi.com/account-api
110132
"""
111133

134+
if params is None:
135+
params = {}
136+
137+
if kwargs:
138+
params.update(kwargs)
139+
112140
r = self.request("GET", "/account.json", params=params, assert_200=True)
113141
return r.json()
114142

115-
143+
# An un-authenticated client instance, .
116144
_client = Client()
117145
search = _client.search
118146
search_archive = _client.search_archive

tests/test_integration.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,14 @@ def test_coffee_search_next_page(coffee_search):
6868

6969
assert isinstance(next_page, serpapi.SerpResults)
7070
assert coffee_search["search_metadata"]["id"] != next_page["search_metadata"]["id"]
71+
72+
73+
def test_search_function_signature(coffee_params, client):
74+
s = client.search(coffee_params)
75+
assert s["search_metadata"]["id"]
76+
77+
s = client.search(**coffee_params)
78+
assert s["search_metadata"]["id"]
79+
80+
s = client.search(q='coffee')
81+
assert s["search_metadata"]["id"]

0 commit comments

Comments
 (0)