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

Django response binary format support #668

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions openapi_core/contrib/django/responses.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
"""OpenAPI core contrib django responses module"""
from itertools import tee

from django.http.response import HttpResponse
from django.http.response import StreamingHttpResponse
from werkzeug.datastructures import Headers


class DjangoOpenAPIResponse:
def __init__(self, response: HttpResponse):
if not isinstance(response, HttpResponse):
if not isinstance(response, (HttpResponse, StreamingHttpResponse)):
raise TypeError(
f"'response' argument is not type of {HttpResponse}"
f"'response' argument is not type of {HttpResponse} or {StreamingHttpResponse}"
)
self.response = response

@property
def data(self) -> str:
if isinstance(self.response, StreamingHttpResponse):
resp_iter1, resp_iter2 = tee(self.response._iterator)
self.response.streaming_content = resp_iter1
content = b"".join(map(self.response.make_bytes, resp_iter2))
return content
assert isinstance(self.response.content, bytes)
return self.response.content.decode("utf-8")

Expand Down
Loading