Skip to content

Commit a4ac34e

Browse files
author
Kenneth Reitz
authored
Merge pull request #5 from serpapi/kr-params-simplify
General Updates
2 parents d658e87 + c9e1371 commit a4ac34e

5 files changed

Lines changed: 50 additions & 17 deletions

File tree

docs/index.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ This part of the documentation covers all the interfaces of :class:`serpapi` Pyt
7878
.. module:: serpapi
7979
:platform: Unix, Windows
8080
:synopsis: SerpApi Python Library
81-
:members:
82-
:undoc-members:
8381

8482
.. autofunction:: serpapi.search
8583
.. autofunction:: serpapi.search_archive
@@ -162,7 +160,7 @@ This class also alleviates the need to pass an ``api_key``` along with every se
162160
Exceptions
163161
----------
164162

165-
.. autoexception:: serpapi.SerpAPIError
163+
.. autoexception:: serpapi.SerpApiError
166164
:members:
167165

168166
.. autoexception:: serpapi.SearchIDNotProvided

serpapi/core.py

Lines changed: 32 additions & 8 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: dict = 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: dict = 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,11 @@ 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)
7085

7186
try:
7287
search_id = params["search_id"]
@@ -78,7 +93,7 @@ def search_archive(self, **params):
7893
r = self.request("GET", f"/searches/{ search_id }", params=params)
7994
return SerpResults.from_http_response(r, client=self)
8095

81-
def locations(self, **params):
96+
def locations(self, params: dict = None, **kwargs):
8297
"""Get a list of supported Google locations.
8398
8499
@@ -88,6 +103,11 @@ def locations(self, **params):
88103
89104
**Learn more**: https://serpapi.com/locations-api
90105
"""
106+
if params is None:
107+
params = {}
108+
109+
if kwargs:
110+
params.update(kwargs)
91111

92112
r = self.request(
93113
"GET",
@@ -97,10 +117,7 @@ def locations(self, **params):
97117
)
98118
return r.json()
99119

100-
def account(
101-
self,
102-
**params,
103-
):
120+
def account(self, params: dict = None, **kwargs):
104121
"""Get SerpApi account information.
105122
106123
:param api_key: the API Key to use for SerpApi.com.
@@ -109,10 +126,17 @@ def account(
109126
**Learn more**: https://serpapi.com/account-api
110127
"""
111128

129+
if params is None:
130+
params = {}
131+
132+
if kwargs:
133+
params.update(kwargs)
134+
112135
r = self.request("GET", "/account.json", params=params, assert_200=True)
113136
return r.json()
114137

115138

139+
# An un-authenticated client instance.
116140
_client = Client()
117141
search = _client.search
118142
search_archive = _client.search_archive

serpapi/exceptions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import requests
22

33

4-
class SerpAPIError(Exception):
4+
class SerpApiError(Exception):
55
"""Base class for exceptions in this module."""
66

77
pass
88

99

10-
class APIKeyNotProvided(ValueError, SerpAPIError):
10+
class APIKeyNotProvided(ValueError, SerpApiError):
1111
"""API key is not provided."""
1212

1313
pass
1414

1515

16-
class SearchIDNotProvided(ValueError, SerpAPIError):
16+
class SearchIDNotProvided(ValueError, SerpApiError):
1717
"""Search ID is not provided."""
1818

1919
pass
2020

2121

22-
class HTTPError(requests.exceptions.HTTPError, SerpAPIError):
22+
class HTTPError(requests.exceptions.HTTPError, SerpApiError):
2323
"""HTTP Error."""
2424

2525
pass
2626

2727

28-
class HTTPConnectionError(HTTPError, requests.exceptions.ConnectionError, SerpAPIError):
28+
class HTTPConnectionError(HTTPError, requests.exceptions.ConnectionError, SerpApiError):
2929
"""Connection Error."""
3030

3131
pass

serpapi/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
class SerpResults(UserDict):
13-
"""A dictionary-like object that represents the results of a SerpAPI request.
13+
"""A dictionary-like object that represents the results of a SerpApi request.
1414
1515
.. code-block:: python
1616

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)