Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mbeliaev/recorder #545

Merged
merged 19 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.22.0
------

* [BETA] Added possibility to record responses to YAML files via `@_recorder.record(file_path="out.yml")` decorator.
* Add `passthrough` argument to `BaseResponse` object. See #557

0.21.0
Expand Down
75 changes: 75 additions & 0 deletions responses/_recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from functools import wraps
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any
from typing import Callable
from typing import Type
from typing import Union
import os
from responses import FirstMatchRegistry
from responses import HTTPAdapter
from responses import PreparedRequest
from responses import models
from responses import _F

from responses import RequestsMock
from responses import Response
from responses import _real_send
from responses.registries import OrderedRegistry


class Recorder(RequestsMock):
def __init__(
self,
target: str = "requests.adapters.HTTPAdapter.send",
registry: "Type[FirstMatchRegistry]" = OrderedRegistry,
) -> None:
super().__init__(target=target, registry=registry)

def reset(self) -> None:
self._registry = OrderedRegistry()

def record(
self, *, file_path: "Union[str, bytes, os.PathLike[Any]]" = "response.toml"
) -> "Union[Callable[[_F], _F], _F]":
def deco_record(function: "_F") -> "Callable[..., Any]":
@wraps(function)
def wrapper(*args: "Any", **kwargs: "Any") -> "Any": # type: ignore[misc]
with self:
ret = function(*args, **kwargs)
with open(file_path, "w") as file:
self.get_registry()._dump(file)

return ret

return wrapper

return deco_record

def _on_request(
self,
adapter: "HTTPAdapter",
request: "PreparedRequest",
**kwargs: "Any",
) -> "models.Response":
# add attributes params and req_kwargs to 'request' object for further match comparison
# original request object does not have these attributes
request.params = self._parse_request_params(request.path_url) # type: ignore[attr-defined]
request.req_kwargs = kwargs # type: ignore[attr-defined]
requests_response = _real_send(adapter, request, **kwargs)
responses_response = Response(
method=str(request.method),
url=str(requests_response.request.url),
status=requests_response.status_code,
body=requests_response.text,
)
self._registry.add(responses_response)
return requests_response

def stop(self, allow_assert: bool = True) -> None:
super().stop(allow_assert=False)


recorder = Recorder()
record = recorder.record
31 changes: 31 additions & 0 deletions responses/registries.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import copy
from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple

import toml as _toml

if TYPE_CHECKING: # pragma: no cover
# import only for linter run
import io

from requests import PreparedRequest

from responses import BaseResponse
Expand Down Expand Up @@ -73,6 +79,31 @@ def replace(self, response: "BaseResponse") -> "BaseResponse":
self.registered[index] = response
return response

def _dump(self, destination: "io.IOBase") -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should dumping to a file be a separate thing? That would let userland code or us in the future change the serialization format that is being used to save response fixtures.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to create it in the registry because this will allow to dump existing tests to the file
Eg
If user ran responses.add() multiple times, then user can run responses.get_registry()._dump(*args)

That will allow safe (compared to brand new recording) transfer to the new method if required

In the future we can provide serializer argument, but at the moment I am a bit reluctant to support other serializers than one we decide

Also, user can also overwrite this method after inheriting

Or what are the other options to introduce this method?
As a separate function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't dumping be on the Recorder()? I'm concerned that recording feels like it is half in the Recorder and half in the registry right now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markstory
done!

this could be done even as a separate function. Then it will be possible to easily dump existing registries into the file

data: Dict[str, Any] = {"responses": []}
for rsp in self.registered:
try:
content_length = rsp.auto_calculate_content_length # type: ignore[attr-defined]
data["responses"].append(
{
"response": {
"method": rsp.method,
"url": rsp.url,
"body": rsp.body, # type: ignore[attr-defined]
"status": rsp.status, # type: ignore[attr-defined]
"headers": rsp.headers,
"content_type": rsp.content_type,
"auto_calculate_content_length": content_length,
}
}
)
except AttributeError as exc:
raise AttributeError(
"Cannot dump response object."
"Probably you use custom Response object that misses required aatributes"
beliaev-maksim marked this conversation as resolved.
Show resolved Hide resolved
) from exc
_toml.dump(data, destination)


class OrderedRegistry(FirstMatchRegistry):
def find(
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
install_requires = [
"requests>=2.0,<3.0",
"urllib3>=1.25.10",
"toml",
"types-toml",
"typing_extensions; python_version < '3.8'",
]

Expand Down