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

added omit_headers #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions drf_batch_requests/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BatchRequest(HttpRequest):
def __init__(self, request, request_data):
super(BatchRequest, self).__init__()
self.name = request_data.get('name')
self.omit_headers = request_data.get('omit_headers', False)
self.omit_response_on_success = request_data.get('omit_response_on_success', False)

self._stream = BytesIO(request_data['_body'].encode('utf-8'))
Expand Down
8 changes: 6 additions & 2 deletions drf_batch_requests/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ class BatchResponse:
_return_body: bool = True

def __init__(self, name: str, status_code: int, body: str, headers: Iterable[ResponseHeader] = None,
omit_response_on_success: bool = False, status_text: str = None):
omit_headers: bool = False, omit_response_on_success: bool = False, status_text: str = None):
self.name = name
self.status_code = status_code
self.status_text = status_text
self.body = body
self.headers = headers or []
self.omit_response_on_success = omit_response_on_success

if omit_headers:
self.headers = []
else:
self.headers = headers or []

if is_success(self.status_code):
try:
self._data = json.loads(self.body)
Expand Down
2 changes: 2 additions & 0 deletions drf_batch_requests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SingleRequestSerializer(serializers.Serializer):
data = serializers.SerializerMethodField()
files = serializers.SerializerMethodField()

omit_headers = serializers.BooleanField(required=False)

def validate_headers(self, value):
if isinstance(value, dict):
return value
Expand Down
1 change: 1 addition & 0 deletions drf_batch_requests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def post(self, request, *args, **kwargs):
ResponseHeader(key, value)
for key, value in header_items
],
omit_headers=current_request.omit_headers,
omit_response_on_success=current_request.omit_response_on_success,
status_text=response.reason_phrase
)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ def test_json_batch(self):
','.join([str(o['id']) for o in responses_data[0]['data']])
)

def test_omit_headers(self):
batch = [
{
"method": "GET",
"relative_url": "/tests/test/",
"omit_headers": True,
},
{
"method": "GET",
"relative_url": "/tests/test/",
"omit_headers": False,
},
{
"method": "GET",
"relative_url": "/tests/test/",
},
]

responses = self.forced_auth_req('post', '/batch/', data={'batch': batch})

self.assertFalse(responses.data[0]['headers'])
self.assertTrue(responses.data[1]['headers'])
self.assertTrue(responses.data[2]['headers'])

def test_multipart_simple_request(self):
batch = [
{
Expand Down