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 9, 2024
1 parent 5c9e79e commit 454740b
Show file tree
Hide file tree
Showing 8 changed files with 695 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 @@ -43,6 +43,7 @@ Changelog
* Changed the return type of :meth:`Model.save <pyairtable.orm.Model.save>`
from ``bool`` to :class:`~pyairtable.orm.SaveResult`.
- `PR #387 <https://github.com/gtalarico/pyairtable/pull/387>`_
* 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 454740b

Please sign in to comment.