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

fix: revert multipart #95

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
48 changes: 29 additions & 19 deletions appwrite/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import io
import json
import os
import requests
from .payload import Payload
from .multipart import MultipartParser
from .input_file import InputFile
from .exception import AppwriteException
from .encoders.value_class_encoder import ValueClassEncoder

Expand Down Expand Up @@ -90,15 +91,11 @@ def call(self, method, path='', headers=None, params=None, response_type='json')

if headers['content-type'].startswith('multipart/form-data'):
del headers['content-type']
headers['accept'] = 'multipart/form-data'
stringify = True
for key in data.copy():
if isinstance(data[key], Payload):
if data[key].filename:
files[key] = (data[key].filename, data[key].to_binary())
del data[key]
else:
data[key] = data[key].to_string()
if isinstance(data[key], InputFile):
files[key] = (data[key].filename, data[key].data)
del data[key]
data = self.flatten(data, stringify=stringify)

response = None
Expand Down Expand Up @@ -129,9 +126,6 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
if content_type.startswith('application/json'):
return response.json()

if content_type.startswith('multipart/form-data'):
return MultipartParser(response.content, content_type).to_dict()

return response._content
except Exception as e:
if response != None:
Expand All @@ -152,10 +146,20 @@ def chunked_upload(
on_progress = None,
upload_id = ''
):
payload = params[param_name]
size = params[param_name].size
input_file = params[param_name]

if input_file.source_type == 'path':
size = os.stat(input_file.path).st_size
input = open(input_file.path, 'rb')
elif input_file.source_type == 'bytes':
size = len(input_file.data)
input = input_file.data

if size < self._chunk_size:
if input_file.source_type == 'path':
input_file.data = input.read()

if size < self._chunk_size:
params[param_name] = input_file
return self.call(
'post',
path,
Expand All @@ -178,10 +182,16 @@ def chunked_upload(
input.seek(offset)

while offset < size:
params[param_name] = Payload.from_binary(
payload.to_binary(offset, min(self._chunk_size, size - offset)),
payload.filename
)
if input_file.source_type == 'path':
input_file.data = input.read(self._chunk_size) or input.read(size - offset)
elif input_file.source_type == 'bytes':
if offset + self._chunk_size < size:
end = offset + self._chunk_size
else:
end = size - offset
input_file.data = input[offset:end]

params[param_name] = input_file
headers["content-range"] = f'bytes {offset}-{min((offset + self._chunk_size) - 1, size - 1)}/{size}'

result = self.call(
Expand Down
11 changes: 10 additions & 1 deletion appwrite/enums/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Runtime(Enum):
NODE_19_0 = "node-19.0"
NODE_20_0 = "node-20.0"
NODE_21_0 = "node-21.0"
NODE_22 = "node-22"
PHP_8_0 = "php-8.0"
PHP_8_1 = "php-8.1"
PHP_8_2 = "php-8.2"
Expand All @@ -25,28 +26,36 @@ class Runtime(Enum):
DENO_1_24 = "deno-1.24"
DENO_1_35 = "deno-1.35"
DENO_1_40 = "deno-1.40"
DENO_1_46 = "deno-1.46"
DENO_2_0 = "deno-2.0"
DART_2_15 = "dart-2.15"
DART_2_16 = "dart-2.16"
DART_2_17 = "dart-2.17"
DART_2_18 = "dart-2.18"
DART_3_0 = "dart-3.0"
DART_3_1 = "dart-3.1"
DART_3_3 = "dart-3.3"
DOTNET_3_1 = "dotnet-3.1"
DART_3_5 = "dart-3.5"
DOTNET_6_0 = "dotnet-6.0"
DOTNET_7_0 = "dotnet-7.0"
DOTNET_8_0 = "dotnet-8.0"
JAVA_8_0 = "java-8.0"
JAVA_11_0 = "java-11.0"
JAVA_17_0 = "java-17.0"
JAVA_18_0 = "java-18.0"
JAVA_21_0 = "java-21.0"
JAVA_22 = "java-22"
SWIFT_5_5 = "swift-5.5"
SWIFT_5_8 = "swift-5.8"
SWIFT_5_9 = "swift-5.9"
SWIFT_5_10 = "swift-5.10"
KOTLIN_1_6 = "kotlin-1.6"
KOTLIN_1_8 = "kotlin-1.8"
KOTLIN_1_9 = "kotlin-1.9"
KOTLIN_2_0 = "kotlin-2.0"
CPP_17 = "cpp-17"
CPP_20 = "cpp-20"
BUN_1_0 = "bun-1.0"
BUN_1_1 = "bun-1.1"
GO_1_23 = "go-1.23"
STATIC_1 = "static-1"
21 changes: 21 additions & 0 deletions appwrite/input_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import mimetypes

class InputFile:
@classmethod
def from_path(cls, path):
instance = cls()
instance.path = path
instance.filename = os.path.basename(path)
instance.mime_type = mimetypes.guess_type(path)
instance.source_type = 'path'
return instance

@classmethod
def from_bytes(cls, bytes, filename, mime_type = None):
instance = cls()
instance.data = bytes
instance.filename = filename
instance.mime_type = mime_type
instance.source_type = 'bytes'
return instance
49 changes: 0 additions & 49 deletions appwrite/multipart.py

This file was deleted.

72 changes: 0 additions & 72 deletions appwrite/payload.py

This file was deleted.

Loading