updates
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
from paypalhttp.serializers.form_encoded_serializer import FormEncoded
|
||||
from paypalhttp.serializers.json_serializer import Json
|
||||
from paypalhttp.serializers.text_serializer import Text
|
||||
from paypalhttp.serializers.multipart_serializer import Multipart
|
||||
from paypalhttp.serializers.form_part import FormPart
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,18 @@
|
||||
try:
|
||||
from urllib import quote
|
||||
except ImportError:
|
||||
from urllib.parse import quote
|
||||
|
||||
class FormEncoded:
|
||||
def encode(self, request):
|
||||
params = []
|
||||
for k, v in request.body.items():
|
||||
params.append("{0}={1}".format(k, quote(v)))
|
||||
|
||||
return '&'.join(params)
|
||||
|
||||
def decode(self, data):
|
||||
raise IOError("FormEncoded does not support deserialization")
|
||||
|
||||
def content_type(self):
|
||||
return "application/x-www-form-urlencoded"
|
||||
@@ -0,0 +1,8 @@
|
||||
class FormPart(object):
|
||||
|
||||
def __init__(self, value, headers):
|
||||
self.value = value
|
||||
self.headers = {}
|
||||
|
||||
for key in headers:
|
||||
self.headers['-'.join(map(lambda word: word[0].upper() + word[1:], key.lower().split('-')))] = headers[key]
|
||||
@@ -0,0 +1,13 @@
|
||||
import json
|
||||
|
||||
|
||||
class Json:
|
||||
|
||||
def encode(self, request):
|
||||
return json.dumps(request.body)
|
||||
|
||||
def decode(self, data):
|
||||
return json.loads(data)
|
||||
|
||||
def content_type(self):
|
||||
return "application/json"
|
||||
@@ -0,0 +1,85 @@
|
||||
import time
|
||||
import os
|
||||
|
||||
from paypalhttp import File
|
||||
from paypalhttp.encoder import Encoder
|
||||
from paypalhttp.serializers.form_part import FormPart
|
||||
|
||||
from paypalhttp.serializers import Json, Text, FormEncoded
|
||||
|
||||
CRLF = "\r\n"
|
||||
|
||||
class FormPartRequest:
|
||||
pass
|
||||
|
||||
class Multipart:
|
||||
|
||||
def encode(self, request):
|
||||
boundary = str(time.time()).replace(".", "")
|
||||
request.headers["content-type"] = "multipart/form-data; boundary=" + boundary
|
||||
params = []
|
||||
form_params = []
|
||||
file_params = []
|
||||
for k, v in request.body.items():
|
||||
if isinstance(v, File):
|
||||
file_params.append(self.add_file_part(k, v))
|
||||
elif isinstance(v, FormPart):
|
||||
form_params.append(self.add_form_part(k, v))
|
||||
else: # It's a regular form param
|
||||
form_params.append(self.add_form_field(k, v))
|
||||
|
||||
params = form_params + file_params
|
||||
data = "--" + boundary + CRLF + ("--" + boundary + CRLF).join(params) + CRLF + "--" + boundary + "--"
|
||||
|
||||
return data
|
||||
|
||||
def decode(self, data):
|
||||
raise IOError('Multipart does not support deserialization.')
|
||||
|
||||
def content_type(self):
|
||||
return "multipart/.*"
|
||||
|
||||
def add_form_field(self, key, value):
|
||||
return "Content-Disposition: form-data; name=\"{}\"{}{}{}{}".format(key, CRLF, CRLF, value, CRLF)
|
||||
|
||||
def add_form_part(self, key, formPart):
|
||||
retValue = "Content-Disposition: form-data; name=\"{}\"".format(key)
|
||||
formatted_headers = self.format_headers(formPart.headers)
|
||||
if formatted_headers["content-type"] == "application/json":
|
||||
retValue += "; filename=\"{}.json\"".format(key)
|
||||
retValue += CRLF
|
||||
|
||||
for key in formPart.headers:
|
||||
retValue += "{}: {}{}".format(key, formPart.headers[key], CRLF)
|
||||
|
||||
retValue += CRLF
|
||||
|
||||
req = FormPartRequest()
|
||||
req.headers = formatted_headers
|
||||
req.body = formPart.value
|
||||
retValue += Encoder([Json(), Text(), FormEncoded()]).serialize_request(req)
|
||||
|
||||
retValue += CRLF
|
||||
return retValue
|
||||
|
||||
def add_file_part(self, key, f):
|
||||
mime_type = self.mime_type_for_filename(os.path.basename(f.name))
|
||||
s = "Content-Disposition: form-data; name=\"{}\"; filename=\"{}\"{}".format(key, os.path.basename(f.name), CRLF)
|
||||
return s + "Content-Type: {}{}{}{}{}".format(mime_type, CRLF, CRLF, f.read(), CRLF)
|
||||
|
||||
def format_headers(self, headers):
|
||||
if headers:
|
||||
return dict((k.lower(), v) for k, v in headers.items())
|
||||
|
||||
def mime_type_for_filename(self, filename):
|
||||
_, extension = os.path.splitext(filename)
|
||||
if extension == ".jpeg" or extension == ".jpg":
|
||||
return "image/jpeg"
|
||||
elif extension == ".png":
|
||||
return "image/png"
|
||||
elif extension == ".gif":
|
||||
return "image/gif"
|
||||
elif extension == ".pdf":
|
||||
return "application/pdf"
|
||||
else:
|
||||
return "application/octet-stream"
|
||||
@@ -0,0 +1,10 @@
|
||||
class Text:
|
||||
|
||||
def encode(self, request):
|
||||
return str(request.body)
|
||||
|
||||
def decode(self, data):
|
||||
return str(data)
|
||||
|
||||
def content_type(self):
|
||||
return "text/.*"
|
||||
Reference in New Issue
Block a user