Skip to content

Commit

Permalink
Use partialmethod for shortcuts (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Story <mark@mark-story.com>
  • Loading branch information
beliaev-maksim and markstory authored Sep 16, 2022
1 parent a24d964 commit 3bb694b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Add support for the `loose` version of `json_params_matcher` via named argument `strict_match`. See #551
* Add lists support as JSON objects in `json_params_matcher`. See #559
* Added project links to pypi listing.
* `delete`, `get`, `head`, `options`, `patch`, `post`, `put` shortcuts are now implemented using `functools.partialmethod`.
* Fix `MaxRetryError` exception. Replace exception by `RetryError` according to `requests` implementation. See #572.
* Adjust error message when `Retry` is exhausted. See #580.

Expand Down
30 changes: 9 additions & 21 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json as json_module
import logging
from collections import namedtuple
from functools import partialmethod
from functools import wraps
from http import client
from itertools import groupby
Expand Down Expand Up @@ -760,6 +761,14 @@ def add(
response = Response(method=method, url=url, body=body, **kwargs)
return self._registry.add(response)

delete = partialmethod(add, DELETE)
get = partialmethod(add, GET)
head = partialmethod(add, HEAD)
options = partialmethod(add, OPTIONS)
patch = partialmethod(add, PATCH)
post = partialmethod(add, POST)
put = partialmethod(add, PUT)

def _add_from_file(self, file_path: "Union[str, bytes, os.PathLike[Any]]") -> None:
with open(file_path) as file:
data = _toml.load(file)
Expand All @@ -775,27 +784,6 @@ def _add_from_file(self, file_path: "Union[str, bytes, os.PathLike[Any]]") -> No
auto_calculate_content_length=rsp["auto_calculate_content_length"],
)

def delete(self, *args: Any, **kwargs: Any) -> BaseResponse:
return self.add(DELETE, *args, **kwargs)

def get(self, *args: Any, **kwargs: Any) -> BaseResponse:
return self.add(GET, *args, **kwargs)

def head(self, *args: Any, **kwargs: Any) -> BaseResponse:
return self.add(HEAD, *args, **kwargs)

def options(self, *args: Any, **kwargs: Any) -> BaseResponse:
return self.add(OPTIONS, *args, **kwargs)

def patch(self, *args: Any, **kwargs: Any) -> BaseResponse:
return self.add(PATCH, *args, **kwargs)

def post(self, *args: Any, **kwargs: Any) -> BaseResponse:
return self.add(POST, *args, **kwargs)

def put(self, *args: Any, **kwargs: Any) -> BaseResponse:
return self.add(PUT, *args, **kwargs)

def add_passthru(self, prefix: _URLPatternType) -> None:
"""
Register a URL prefix or regex to passthru any non-matching mock requests to.
Expand Down

0 comments on commit 3bb694b

Please sign in to comment.