Skip to content

Commit

Permalink
Compatibility fixes for Django 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszn committed Apr 12, 2021
1 parent f818155 commit f782bf7
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ matrix:
include:
- { python: "2.7", env: DJANGO=1.11 }
- { python: "3.4", env: DJANGO=2.0 }
- { python: "nightly", env: DJANGO=2.2 }
- { python: "3.5", env: DJANGO=2.2 }
- { python: "3.6", env: DJANGO=3.0 }
- { python: "nightly", env: DJANGO=3.1 }

install:
- pip install -r requirements.txt
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Django Batch Requests
=========================

![](https://img.shields.io/badge/python-2.7%20|%203.4%20|%20nightly-blue.svg)
![](https://img.shields.io/badge/django%20versions-1.11%20|%202.0%20|%202.2-blue.svg)
![](https://img.shields.io/badge/python-2.7%20--%203.10-blue.svg)
![](https://img.shields.io/badge/django%20versions-1.11%20|%202.0%20|%202.2%20|%203.0%20|%203.1-blue.svg)
[![build-status-image]][travis]
[![coverage]][coverage-repo]

Expand Down
2 changes: 1 addition & 1 deletion batch_requests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

__title__ = 'django-batch-requests'
__version__ = '1.0'
__version__ = '2.0'
__author__ = 'Rahul Tanwani, Tomasz Nowak'
__license__ = 'MIT'

Expand Down
4 changes: 2 additions & 2 deletions batch_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def pre_process_method_headers(method, headers):
header = header.replace("-", "_")
header = "http_{header}".format(
header=header) if header.lower() not in _wsgi_headers else header
_transformed_headers.update({header.upper(): value})
_transformed_headers[header.upper()] = value

return method, _transformed_headers

Expand All @@ -87,7 +87,7 @@ def get_wsgi_request_object(curr_request, method, url, headers, body):

# Add default content type.
if "CONTENT_TYPE" not in t_headers:
t_headers.update({"CONTENT_TYPE": _settings.DEFAULT_CONTENT_TYPE})
t_headers["CONTENT_TYPE"] = _settings.DEFAULT_CONTENT_TYPE

# Override existing batch requests headers with the new headers passed for this request.
x_headers.update(t_headers)
Expand Down
4 changes: 2 additions & 2 deletions batch_requests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_response(wsgi_request):
except Exception as exc:
resp = HttpResponseServerError(content=exc.args[0])

headers = dict(resp._headers.values())
headers = dict(resp.items())
# Convert HTTP response into simple dict type.
d_resp = {"status_code": resp.status_code, "reason_phrase": resp.reason_phrase,
"headers": headers}
Expand All @@ -48,7 +48,7 @@ def get_response(wsgi_request):

# Check if we need to send across the duration header.
if _settings.ADD_DURATION_HEADER:
d_resp['headers'].update({_settings.DURATION_HEADER_NAME: (datetime.now() - service_start_time).seconds})
d_resp['headers'][_settings.DURATION_HEADER_NAME] = (datetime.now() - service_start_time).seconds

return d_resp

Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def get_package_data(package):
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Topic :: Internet :: WWW/HTTP'
]
)
2 changes: 1 addition & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def headers_dict(self, headers):
'''
Converts the headers from the response in to a dict.
'''
return dict(headers.values())
return dict(headers)

def prepare_response(self, status_code, body, headers):
'''
Expand Down
16 changes: 8 additions & 8 deletions tests/test_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_compatibility_of_get_request(self):
'''
# Get the response for an individual request.
inv_req = self.client.get("/views/")
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Get the response for a batch request.
batch_request = self.make_a_batch_request("GET", "/views/", "")
Expand All @@ -41,7 +41,7 @@ def test_compatibility_of_post_request(self):

# Get the response for an individual request.
inv_req = self.client.post("/views/", data, content_type="text/plain")
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Get the response for a batch request.
batch_request = self.make_a_batch_request("POST", "/views/", data, {"content_type": "text/plain"})
Expand All @@ -60,7 +60,7 @@ def test_compatibility_of_put_request(self):

# Get the response for an individual request.
inv_req = self.client.patch("/views/", data, content_type="text/plain")
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Get the response for a batch request.
batch_request = self.make_a_batch_request("patch", "/views/", data, {"content_type": "text/plain"})
Expand All @@ -79,7 +79,7 @@ def test_compatibility_of_patch_request(self):

# Get the response for an individual request.
inv_req = self.client.post("/views/", data, content_type="text/plain")
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Get the response for a batch request.
batch_request = self.make_a_batch_request("POST", "/views/", data, {"CONTENT_TYPE": "text/plain"})
Expand All @@ -96,7 +96,7 @@ def test_compatibility_of_delete_request(self):
'''
# Get the response for an individual request.
inv_req = self.client.delete("/views/")
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_resp = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Get the response for a batch request.
batch_request = self.make_a_batch_request("delete", "/views/", "")
Expand All @@ -117,15 +117,15 @@ def test_compatibility_of_multiple_requests(self):

# Get the response for an individual GET request.
inv_req = self.client.get("/views/")
inv_get = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_get = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Get the response for an individual POST request.
inv_req = self.client.post("/views/", data, content_type="text/plain")
inv_post = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_post = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Get the response for an individual PUT request.
inv_req = self.client.patch("/views/", data, content_type="text/plain")
inv_put = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req._headers)
inv_put = self.prepare_response(inv_req.status_code, inv_req.content.decode('utf-8'), inv_req.items())

# Consolidate all the responses.
indv_responses = [inv_get, inv_post, inv_put]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_concurrency_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def compare_seq_concurrent_duration(self):

# Get the response for a batch request.
batch_requests = self.make_multiple_batch_request([sleep_2_seconds, sleep_1_second, sleep_2_seconds])
seq_duration = int(batch_requests._headers.get(br_settings.DURATION_HEADER_NAME)[1])
seq_duration = int(batch_requests[br_settings.DURATION_HEADER_NAME])

# Update the executor settings.
br_settings.executor = self.get_executor()
concurrent_batch_requests = self.make_multiple_batch_request([sleep_2_seconds, sleep_1_second, sleep_2_seconds])
concurrency_duration = int(concurrent_batch_requests._headers.get(br_settings.DURATION_HEADER_NAME)[1])
concurrency_duration = int(concurrent_batch_requests[br_settings.DURATION_HEADER_NAME])

self.assertLess(concurrency_duration, seq_duration, "Concurrent requests are slower than running them in sequence.")
2 changes: 1 addition & 1 deletion tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_duration_header(self):
batch_requests = self._make_multiple_batch_request([get_req])

# Make sure we have the header present in enclosing batch response and all individual responses.
self.assertIn(br_settings.DURATION_HEADER_NAME, batch_requests._headers,
self.assertIn(br_settings.DURATION_HEADER_NAME, batch_requests,
"Enclosing batch request does not contain duration header.")

self.assertIn(br_settings.DURATION_HEADER_NAME, json.loads(batch_requests.content.decode('utf-8'))[0]['headers'],
Expand Down

0 comments on commit f782bf7

Please # to comment.