Skip to content
Open
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
3 changes: 2 additions & 1 deletion requirements/requirements-development.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
django>=1.7
djangorestframework>=3.1.0
requests>=1.1.0
requests>=1.1.0
requests-toolbelt>=0.6.2
63 changes: 0 additions & 63 deletions rest_framework_proxy/adapters.py

This file was deleted.

64 changes: 0 additions & 64 deletions rest_framework_proxy/utils.py

This file was deleted.

53 changes: 17 additions & 36 deletions rest_framework_proxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils import six
from django.utils.six import BytesIO as StringIO
from requests.exceptions import ConnectionError, SSLError, Timeout
from requests_toolbelt.multipart.encoder import MultipartEncoder
from requests import sessions
from django.http import HttpResponse
from rest_framework.response import Response
Expand All @@ -13,8 +14,6 @@
from rest_framework.exceptions import UnsupportedMediaType

from rest_framework_proxy.settings import api_proxy_settings
from rest_framework_proxy.adapters import StreamingHTTPAdapter
from rest_framework_proxy.utils import StreamingMultipart, generate_boundary


class BaseProxyView(APIView):
Expand Down Expand Up @@ -158,40 +157,22 @@ def proxy(self, request, *args, **kwargs):
cookies = self.get_cookies(request)

try:
if files:
"""
By default requests library uses chunked upload for files
but it is much more easier for servers to handle streamed
uploads.

This new implementation is also lightweight as files are not
read entirely into memory.
"""
boundary = generate_boundary()
headers['Content-Type'] = 'multipart/form-data; boundary=%s' % boundary

body = StreamingMultipart(data, files, boundary)

session = sessions.Session()
session.mount('http://', StreamingHTTPAdapter())
session.mount('https://', StreamingHTTPAdapter())

response = session.request(request.method, url,
params=params,
data=body,
headers=headers,
timeout=self.proxy_settings.TIMEOUT,
verify=verify_ssl,
cookies=cookies)
else:
response = requests.request(request.method, url,
params=params,
data=data,
files=files,
headers=headers,
timeout=self.proxy_settings.TIMEOUT,
verify=verify_ssl,
cookies=cookies)
if not type(data) is str and bool(data):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What happens if you have files but not data, doesn't this solution drop the files from the request?

# django rest framework request data attribute contains file and non file inputs
# need to process file differently to add their names
data={k: v for k, v in data.items() if k not in files.keys()}
for k, v in files.items():
data[k] = (v.name, v)
data = MultipartEncoder(fields=data)
headers['Content-Type'] = data.content_type

response = requests.request(request.method, url,
params=params,
data=data,
headers=headers,
timeout=self.proxy_settings.TIMEOUT,
verify=verify_ssl,
cookies=cookies)
except (ConnectionError, SSLError):
status = requests.status_codes.codes.bad_gateway
return self.create_error_response({
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
install_requires = [
'django>=1.7',
'djangorestframework>=3.1.0',
'requests>=1.1.0'
'requests>=1.1.0',
'requests-toolbelt>=0.6.2'
]
classifiers = [
'Environment :: Web Environment',
Expand Down