Skip to content

Commit c2f16d2

Browse files
refactor(auth): use Authorization header instead of query params for API key
Migrate API key authentication from query parameters to Authorization header following security best practices and Geocodio API documentation recommendations. This reduces code duplication by centralizing auth header injection in the _request method. - Add Authorization header with Bearer token in _request method - Remove redundant api_key parameter initialization from all API methods - Make params parameter optional in _request method (defaults to empty dict) - Remove TODO comment about repeated API key handling
1 parent 57c4ae5 commit c2f16d2

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/geocodio/client.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def geocode(
7777
limit: Optional[int] = None,
7878
country: Optional[str] = None,
7979
) -> GeocodingResponse:
80-
params: Dict[str, Union[str, int]] = {"api_key": self.api_key}
80+
params: Dict[str, Union[str, int]] = {}
8181
if fields:
8282
params["fields"] = ",".join(fields)
8383
if limit:
@@ -131,7 +131,7 @@ def reverse(
131131
fields: Optional[List[str]] = None,
132132
limit: Optional[int] = None,
133133
) -> GeocodingResponse:
134-
params: Dict[str, Union[str, int]] = {"api_key": self.api_key}
134+
params: Dict[str, Union[str, int]] = {}
135135
if fields:
136136
params["fields"] = ",".join(fields)
137137
if limit:
@@ -170,7 +170,7 @@ def _request(
170170
self,
171171
method: str,
172172
endpoint: str,
173-
params: dict,
173+
params: Optional[dict] = None,
174174
json: Optional[dict] = None,
175175
files: Optional[dict] = None,
176176
timeout: Optional[float] = None,
@@ -183,8 +183,11 @@ def _request(
183183
if timeout is None:
184184
timeout = self.single_timeout
185185

186+
# Set up authorization header
187+
headers = {"Authorization": f"Bearer {self.api_key}"}
188+
186189
logger.debug(f"Using timeout: {timeout}s")
187-
resp = self._http.request(method, endpoint, params=params, json=json, files=files, timeout=timeout)
190+
resp = self._http.request(method, endpoint, params=params, json=json, files=files, headers=headers, timeout=timeout)
188191

189192
logger.debug(f"Response status code: {resp.status_code}")
190193
logger.debug(f"Response headers: {resp.headers}")
@@ -289,9 +292,7 @@ def create_list(
289292
AuthenticationError: If the API key is invalid.
290293
GeocodioServerError: If the server encounters an error.
291294
"""
292-
# @TODO we repeat building the params here; prob should move the API key
293-
# to the self._request() method.
294-
params: Dict[str, Union[str, int]] = {"api_key": self.api_key}
295+
params: Dict[str, Union[str, int]] = {}
295296
endpoint = f"{self.BASE_PATH}/lists"
296297

297298
if not file:
@@ -321,7 +322,7 @@ def get_lists(self) -> PaginatedResponse:
321322
Returns:
322323
A ListResponse object containing all lists.
323324
"""
324-
params: Dict[str, Union[str, int]] = {"api_key": self.api_key}
325+
params: Dict[str, Union[str, int]] = {}
325326
endpoint = f"{self.BASE_PATH}/lists"
326327

327328
response = self._request("GET", endpoint, params, timeout=self.list_timeout)
@@ -356,7 +357,7 @@ def get_list(self, list_id: str) -> ListResponse:
356357
Returns:
357358
A ListResponse object containing the retrieved list.
358359
"""
359-
params: Dict[str, Union[str, int]] = {"api_key": self.api_key}
360+
params: Dict[str, Union[str, int]] = {}
360361
endpoint = f"{self.BASE_PATH}/lists/{list_id}"
361362

362363
response = self._request("GET", endpoint, params, timeout=self.list_timeout)
@@ -369,7 +370,7 @@ def delete_list(self, list_id: str) -> None:
369370
Args:
370371
list_id: The ID of the list to delete.
371372
"""
372-
params: Dict[str, Union[str, int]] = {"api_key": self.api_key}
373+
params: Dict[str, Union[str, int]] = {}
373374
endpoint = f"{self.BASE_PATH}/lists/{list_id}"
374375

375376
self._request("DELETE", endpoint, params, timeout=self.list_timeout)
@@ -538,7 +539,7 @@ def download(self, list_id: str, filename: Optional[str] = None) -> str | bytes:
538539
Raises:
539540
GeocodioServerError if the list is still processing or another error occurs.
540541
"""
541-
params = {"api_key": self.api_key}
542+
params = {}
542543
endpoint = f"{self.BASE_PATH}/lists/{list_id}/download"
543544

544545
response: httpx.Response = self._request("GET", endpoint, params, timeout=self.list_timeout)

0 commit comments

Comments
 (0)