1
- import io
2
1
import json
3
- import os
4
2
import requests
5
- from .input_file import InputFile
3
+ from .payload import Payload
4
+ from .multipart import MultipartParser
6
5
from .exception import AppwriteException
7
6
from .encoders .value_class_encoder import ValueClassEncoder
8
7
@@ -13,11 +12,11 @@ def __init__(self):
13
12
self ._endpoint = 'https://cloud.appwrite.io/v1'
14
13
self ._global_headers = {
15
14
'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 (${os.uname().sysname}; ${os.uname().version}; ${os.uname().machine})' ,
17
16
'x-sdk-name' : 'Python' ,
18
17
'x-sdk-platform' : 'server' ,
19
18
'x-sdk-language' : 'python' ,
20
- 'x-sdk-version' : '6.1 .0' ,
19
+ 'x-sdk-version' : '7.0 .0' ,
21
20
'X-Appwrite-Response-Format' : '1.6.0' ,
22
21
}
23
22
@@ -91,11 +90,15 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
91
90
92
91
if headers ['content-type' ].startswith ('multipart/form-data' ):
93
92
del headers ['content-type' ]
93
+ headers ['accept' ] = 'multipart/form-data'
94
94
stringify = True
95
95
for key in data .copy ():
96
- if isinstance (data [key ], InputFile ):
97
- files [key ] = (data [key ].filename , data [key ].data )
98
- del data [key ]
96
+ if isinstance (data [key ], Payload ):
97
+ if data [key ].filename :
98
+ files [key ] = (data [key ].filename , data [key ].to_binary ())
99
+ del data [key ]
100
+ else :
101
+ data [key ] = data [key ].to_string ()
99
102
data = self .flatten (data , stringify = stringify )
100
103
101
104
response = None
@@ -126,6 +129,9 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
126
129
if content_type .startswith ('application/json' ):
127
130
return response .json ()
128
131
132
+ if content_type .startswith ('multipart/form-data' ):
133
+ return MultipartParser (response .content , content_type ).to_dict ()
134
+
129
135
return response ._content
130
136
except Exception as e :
131
137
if response != None :
@@ -146,20 +152,10 @@ def chunked_upload(
146
152
on_progress = None ,
147
153
upload_id = ''
148
154
):
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
161
157
162
- params [ param_name ] = input_file
158
+ if size < self . _chunk_size :
163
159
return self .call (
164
160
'post' ,
165
161
path ,
@@ -182,16 +178,10 @@ def chunked_upload(
182
178
input .seek (offset )
183
179
184
180
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
+ )
195
185
headers ["content-range" ] = f'bytes { offset } -{ min ((offset + self ._chunk_size ) - 1 , size - 1 )} /{ size } '
196
186
197
187
result = self .call (
0 commit comments