1
+ import io
1
2
import json
3
+ import os
2
4
import requests
3
- from .payload import Payload
4
- from .multipart import MultipartParser
5
+ from .input_file import InputFile
5
6
from .exception import AppwriteException
6
7
from .encoders .value_class_encoder import ValueClassEncoder
7
8
@@ -90,15 +91,11 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
90
91
91
92
if headers ['content-type' ].startswith ('multipart/form-data' ):
92
93
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 ], 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 ()
96
+ if isinstance (data [key ], InputFile ):
97
+ files [key ] = (data [key ].filename , data [key ].data )
98
+ del data [key ]
102
99
data = self .flatten (data , stringify = stringify )
103
100
104
101
response = None
@@ -129,9 +126,6 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
129
126
if content_type .startswith ('application/json' ):
130
127
return response .json ()
131
128
132
- if content_type .startswith ('multipart/form-data' ):
133
- return MultipartParser (response .content , content_type ).to_dict ()
134
-
135
129
return response ._content
136
130
except Exception as e :
137
131
if response != None :
@@ -152,10 +146,20 @@ def chunked_upload(
152
146
on_progress = None ,
153
147
upload_id = ''
154
148
):
155
- payload = params [param_name ]
156
- size = params [param_name ].size
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 ()
157
161
158
- if size < self . _chunk_size :
162
+ params [ param_name ] = input_file
159
163
return self .call (
160
164
'post' ,
161
165
path ,
@@ -178,10 +182,16 @@ def chunked_upload(
178
182
input .seek (offset )
179
183
180
184
while offset < size :
181
- params [param_name ] = Payload .from_binary (
182
- payload .to_binary (offset , min (self ._chunk_size , size - offset )),
183
- payload .filename
184
- )
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
185
195
headers ["content-range" ] = f'bytes { offset } -{ min ((offset + self ._chunk_size ) - 1 , size - 1 )} /{ size } '
186
196
187
197
result = self .call (
0 commit comments