Skip to content

Commit

Permalink
Context manager for mocking API interactions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mesozoic committed Sep 6, 2024
1 parent 4c87441 commit 7735082
Show file tree
Hide file tree
Showing 7 changed files with 689 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Changelog
- `PR #373 <https://github.com/gtalarico/pyairtable/pull/373>`_
* Added command line utility and ORM module generator. See :doc:`cli`.
- `PR #376 <https://github.com/gtalarico/pyairtable/pull/376>`_
* Added :class:`pyairtable.testing.MockAirtable` for easier testing.

2.3.3 (2024-03-22)
------------------------
Expand Down
8 changes: 6 additions & 2 deletions pyairtable/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple, TypeVar, Union

import requests
from requests import PreparedRequest
from requests.sessions import Session
from typing_extensions import TypeAlias

Expand Down Expand Up @@ -273,14 +274,17 @@ def request(
json=json,
)

response = self.session.send(prepared, timeout=self.timeout)
return self._process_response(response)
return self._perform_request(prepared)

get = partialmethod(request, "GET")
post = partialmethod(request, "POST")
patch = partialmethod(request, "PATCH")
delete = partialmethod(request, "DELETE")

def _perform_request(self, prepared: PreparedRequest) -> Any:
response = self.session.send(prepared, timeout=self.timeout)
return self._process_response(response)

def _process_response(self, response: requests.Response) -> Any:
try:
response.raise_for_status()
Expand Down
3 changes: 3 additions & 0 deletions pyairtable/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ class UpdateRecordDict(TypedDict):
fields: WritableFields


AnyRecordDict: TypeAlias = Union[RecordDict, CreateRecordDict, UpdateRecordDict]


class RecordDeletedDict(TypedDict):
"""
A ``dict`` representing the payload returned by the Airtable API to confirm a deletion.
Expand Down
Loading

0 comments on commit 7735082

Please sign in to comment.