Skip to content

Commit

Permalink
Merge branch 'feat-multipart' of https://github.com/appwrite/sdk-gene…
Browse files Browse the repository at this point in the history
…rator into feat-swift-multipart
  • Loading branch information
loks0n committed Sep 30, 2024
2 parents 0098b76 + b426bbd commit b4c1bb6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion templates/python/package/multipart.py.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from email.parser import BytesParser
from email.policy import default
from .payload import Payload
import json

class MultipartParser:
def __init__(self, multipart_bytes, content_type):
Expand Down Expand Up @@ -38,7 +39,7 @@ class MultipartParser:
result[name] = Payload.from_binary(part["contents"])
elif name == "responseHeaders":
headers_str = part["contents"].decode('utf-8', errors='replace')
result[name] = dict(line.split(": ", 1) for line in headers_str.split("\r\n") if line)
result[name] = json.loads(headers_str)
elif name == "responseStatusCode":
result[name] = int(part["contents"])
elif name == "duration":
Expand Down
12 changes: 6 additions & 6 deletions templates/python/package/payload.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ class Payload:
_data: Optional[bytes] = None

def __init__(self, path: Optional[str] = None, data: Optional[bytes] = None, filename: Optional[str] = None):
if not path and not data:
if path is None and data is None:
raise ValueError("One of path or data must be provided")

self._path = path
self._data = data

self.filename = filename
if not self._data:
if self._data is None:
self.size = os.path.getsize(self._path)
else:
self.size = len(self._data)

def to_binary(self, offset: Optional[int] = 0, length: Optional[int] = None) -> bytes:
if not length:
if length is None:
length = self.size

if not self._data:
if self._data is None:
with open(self._path, 'rb') as f:
f.seek(offset)
return f.read(length)
Expand Down Expand Up @@ -59,5 +59,5 @@ class Payload:
return cls(path=path, filename=filename)

@classmethod
def from_json(cls, json: Dict[str, Any]) -> 'Payload':
return cls(data=json.dumps(json))
def from_json(cls, data: Dict[str, Any]) -> 'Payload':
return cls(data=json.dumps(data))

0 comments on commit b4c1bb6

Please sign in to comment.