1- import io
21import json
3- import os
42import requests
5- from .input_file import InputFile
3+ from .payload import Payload
4+ from .multipart import MultipartParser
65from .exception import AppwriteException
76from .encoders .value_class_encoder import ValueClassEncoder
87
@@ -13,11 +12,11 @@ def __init__(self):
1312 self ._endpoint = 'https://cloud.appwrite.io/v1'
1413 self ._global_headers = {
1514 'content-type' : '' ,
16- 'user-agent' : 'AppwritePythonSDK/6.1.0 (${os.uname().sysname}; ${os.uname().version}; ${os.uname().machine})' ,
15+ 'user-agent' : 'AppwritePythonSDK/7.0.0-rc1 (${os.uname().sysname}; ${os.uname().version}; ${os.uname().machine})' ,
1716 'x-sdk-name' : 'Python' ,
1817 'x-sdk-platform' : 'server' ,
1918 'x-sdk-language' : 'python' ,
20- 'x-sdk-version' : '6.1.0 ' ,
19+ 'x-sdk-version' : '7.0.0-rc1 ' ,
2120 'X-Appwrite-Response-Format' : '1.6.0' ,
2221 }
2322
@@ -91,10 +90,14 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
9190
9291 if headers ['content-type' ].startswith ('multipart/form-data' ):
9392 del headers ['content-type' ]
93+ headers ['accept' ] = 'multipart/form-data'
9494 stringify = True
9595 for key in data .copy ():
96- if isinstance (data [key ], InputFile ):
97- files [key ] = (data [key ].filename , data [key ].data )
96+ if isinstance (data [key ], Payload ):
97+ if data [key ].filename :
98+ files [key ] = (data [key ].filename , data [key ].to_binary ())
99+ else :
100+ data [key ] = data [key ].to_binary ()
98101 del data [key ]
99102 data = self .flatten (data , stringify = stringify )
100103
@@ -126,6 +129,9 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
126129 if content_type .startswith ('application/json' ):
127130 return response .json ()
128131
132+ if content_type .startswith ('multipart/form-data' ):
133+ return MultipartParser (response .content , content_type ).to_dict ()
134+
129135 return response ._content
130136 except Exception as e :
131137 if response != None :
@@ -146,20 +152,10 @@ def chunked_upload(
146152 on_progress = None ,
147153 upload_id = ''
148154 ):
149- input_file = params [param_name ]
150-
151- if input_file .source_type == 'path' :
152- size = os .stat (input_file .path ).st_size
153- input = open (input_file .path , 'rb' )
154- elif input_file .source_type == 'bytes' :
155- size = len (input_file .data )
156- input = input_file .data
157-
158- if size < self ._chunk_size :
159- if input_file .source_type == 'path' :
160- input_file .data = input .read ()
155+ payload = params [param_name ]
156+ size = params [param_name ].size
161157
162- params [ param_name ] = input_file
158+ if size < self . _chunk_size :
163159 return self .call (
164160 'post' ,
165161 path ,
@@ -182,16 +178,10 @@ def chunked_upload(
182178 input .seek (offset )
183179
184180 while offset < size :
185- if input_file .source_type == 'path' :
186- input_file .data = input .read (self ._chunk_size ) or input .read (size - offset )
187- elif input_file .source_type == 'bytes' :
188- if offset + self ._chunk_size < size :
189- end = offset + self ._chunk_size
190- else :
191- end = size - offset
192- input_file .data = input [offset :end ]
193-
194- params [param_name ] = input_file
181+ params [param_name ] = Payload .from_binary (
182+ payload .to_binary (offset , min (self ._chunk_size , size - offset )),
183+ payload .filename
184+ )
195185 headers ["content-range" ] = f'bytes { offset } -{ min ((offset + self ._chunk_size ) - 1 , size - 1 )} /{ size } '
196186
197187 result = self .call (
0 commit comments