Skip to content

Commit 81bd7cb

Browse files
Preserve caller parameters and per-client API keys
1 parent 827ebf4 commit 81bd7cb

3 files changed

Lines changed: 86 additions & 8 deletions

File tree

‎serpapi/core.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ def search(self, params: dict = None, **kwargs):
3939
4040
**Learn more**: https://serpapi.com/search-api
4141
"""
42-
if params is None:
43-
params = {}
42+
params = dict(params) if params is not None else {}
4443

4544
# These are arguments that should be passed to the underlying requests.request call.
4645
request_kwargs = {}
@@ -63,8 +62,7 @@ def search_archive(self, params: dict = None, **kwargs):
6362
6463
**Learn more**: https://serpapi.com/search-archive-api
6564
"""
66-
if params is None:
67-
params = {}
65+
params = dict(params) if params is not None else {}
6866

6967
# These are arguments that should be passed to the underlying requests.request call.
7068
request_kwargs = {}
@@ -141,8 +139,7 @@ def locations(self, params: dict = None, **kwargs):
141139
142140
**Learn more**: https://serpapi.com/locations-api
143141
"""
144-
if params is None:
145-
params = {}
142+
params = dict(params) if params is not None else {}
146143

147144
# These are arguments that should be passed to the underlying requests.request call.
148145
request_kwargs = {}
@@ -171,8 +168,7 @@ def account(self, params: dict = None, **kwargs):
171168
**Learn more**: https://serpapi.com/account-api
172169
"""
173170

174-
if params is None:
175-
params = {}
171+
params = dict(params) if params is not None else {}
176172

177173
# These are arguments that should be passed to the underlying requests.request call.
178174
request_kwargs = {}

‎serpapi/http.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def __init__(self, *, api_key=None, timeout=None):
2222
self.session = requests.Session()
2323

2424
def request(self, method, path, params, *, assert_200=True, **kwargs):
25+
# Keep API key injection local to this request.
26+
params = dict(params)
27+
2528
# Inject the API Key into the params.
2629
request_data = kwargs.get("data")
2730
api_key_in_data = isinstance(request_data, dict) and "api_key" in request_data

‎tests/test_parameter_reuse.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import copy
2+
from urllib.parse import parse_qs, urlsplit
3+
4+
import pytest
5+
import requests
6+
7+
from serpapi import Client
8+
9+
10+
PARAMS = {
11+
"search": {"q": "coffee"},
12+
"search_archive": {"search_id": "example-search"},
13+
"locations": {"q": "Austin"},
14+
"account": {},
15+
}
16+
17+
18+
def capture_requests(monkeypatch, client):
19+
sent = []
20+
21+
def send(request, **kwargs):
22+
sent.append(request)
23+
response = requests.Response()
24+
response.status_code = 200
25+
response.headers["Content-Type"] = "application/json"
26+
response._content = b'{}'
27+
response.request = request
28+
return response
29+
30+
monkeypatch.setattr(client.session, "send", send)
31+
return sent
32+
33+
34+
@pytest.mark.parametrize("method", list(PARAMS))
35+
@pytest.mark.parametrize("explicit_key", [False, True])
36+
def test_reused_params_keep_each_clients_key(monkeypatch, method, explicit_key):
37+
first = Client(api_key="client-one")
38+
second = Client(api_key="client-two")
39+
first_sent = capture_requests(monkeypatch, first)
40+
second_sent = capture_requests(monkeypatch, second)
41+
params = dict(PARAMS[method])
42+
if explicit_key:
43+
params["api_key"] = "caller-key"
44+
original = copy.deepcopy(params)
45+
getattr(first, method)(params)
46+
getattr(second, method)(params)
47+
expected = ["caller-key", "caller-key"] if explicit_key else ["client-one", "client-two"]
48+
actual = [parse_qs(urlsplit(sent[0].url).query)["api_key"][0] for sent in (first_sent, second_sent)]
49+
assert actual == expected
50+
assert params == original
51+
52+
53+
@pytest.mark.parametrize("method,override", [
54+
("search", {"q": "tea"}),
55+
("search_archive", {"output": "json"}),
56+
("locations", {"limit": 2}),
57+
("account", {"api_key": "caller-key"}),
58+
])
59+
def test_keyword_overrides_do_not_mutate_params(monkeypatch, method, override):
60+
client = Client(api_key="client-key")
61+
sent = capture_requests(monkeypatch, client)
62+
params = dict(PARAMS[method])
63+
original = copy.deepcopy(params)
64+
getattr(client, method)(params, **override)
65+
query = parse_qs(urlsplit(sent[0].url).query)
66+
for key, value in override.items():
67+
assert query[key] == [str(value)]
68+
assert params == original
69+
70+
71+
@pytest.mark.parametrize("explicit_key", [False, True])
72+
def test_direct_request_does_not_mutate_params(monkeypatch, explicit_key):
73+
client = Client(api_key="client-key")
74+
sent = capture_requests(monkeypatch, client)
75+
params = {"api_key": "caller-key"} if explicit_key else {}
76+
original = dict(params)
77+
client.request("GET", "/search", params)
78+
assert parse_qs(urlsplit(sent[0].url).query)["api_key"] == ["caller-key" if explicit_key else "client-key"]
79+
assert params == original

0 commit comments

Comments
 (0)