@@ -15,14 +15,17 @@ def initialize
1515 'x-sdk-name' => 'Ruby' ,
1616 'x-sdk-platform' => 'server' ,
1717 'x-sdk-language' => 'ruby' ,
18- 'x-sdk-version' => '13.0.0' , 'X-Appwrite-Response-Format' => '1.6.0' }
18+ 'x-sdk-version' => '12.1.1' ,
19+ 'X-Appwrite-Response-Format' => '1.6.0'
20+ }
1921 @endpoint = 'https://cloud.appwrite.io/v1'
2022 end
2123
2224 # Set Project
2325 #
2426 # Your project ID
25- # # @param [String] value The value to set for the Project header
27+ #
28+ # @param [String] value The value to set for the Project header
2629 #
2730 # @return [self]
2831 def set_project ( value )
@@ -34,7 +37,8 @@ def set_project(value)
3437 # Set Key
3538 #
3639 # Your secret API key
37- # # @param [String] value The value to set for the Key header
40+ #
41+ # @param [String] value The value to set for the Key header
3842 #
3943 # @return [self]
4044 def set_key ( value )
@@ -46,7 +50,8 @@ def set_key(value)
4650 # Set JWT
4751 #
4852 # Your secret JSON Web Token
49- # # @param [String] value The value to set for the JWT header
53+ #
54+ # @param [String] value The value to set for the JWT header
5055 #
5156 # @return [self]
5257 def set_jwt ( value )
@@ -69,7 +74,8 @@ def set_locale(value)
6974 # Set Session
7075 #
7176 # The user session to authenticate with
72- # # @param [String] value The value to set for the Session header
77+ #
78+ # @param [String] value The value to set for the Session header
7379 #
7480 # @return [self]
7581 def set_session ( value )
@@ -81,7 +87,8 @@ def set_session(value)
8187 # Set ForwardedUserAgent
8288 #
8389 # The user agent string of the client that made the request
84- # # @param [String] value The value to set for the ForwardedUserAgent header
90+ #
91+ # @param [String] value The value to set for the ForwardedUserAgent header
8592 #
8693 # @return [self]
8794 def set_forwarded_user_agent ( value )
@@ -112,6 +119,7 @@ def set_self_signed(self_signed = true)
112119 self
113120 end
114121
122+
115123 # Add Header
116124 #
117125 # @param [String] key The key for the header to add
@@ -154,9 +162,20 @@ def chunked_upload(
154162 on_progress : nil ,
155163 response_type : nil
156164 )
157- payload = params [ param_name . to_sym ]
165+ input_file = params [ param_name . to_sym ]
166+
167+ case input_file . source_type
168+ when 'path'
169+ size = ::File . size ( input_file . path )
170+ when 'string'
171+ size = input_file . data . bytesize
172+ end
158173
159- if payload . size < @chunk_size
174+ if size < @chunk_size
175+ if input_file . source_type == 'path'
176+ input_file . data = IO . read ( input_file . path )
177+ end
178+ params [ param_name . to_sym ] = input_file
160179 return call (
161180 method : 'POST' ,
162181 path : path ,
@@ -180,13 +199,21 @@ def chunked_upload(
180199 offset = chunks_uploaded * @chunk_size
181200 end
182201
183- while offset < payload . size
184- params [ param_name . to_sym ] = Payload . from_binary (
185- payload . to_binary ( offset , [ @chunk_size , payload . size - offset ] . min ) ,
186- filename : payload . filename
202+ while offset < size
203+ case input_file . source_type
204+ when 'path'
205+ string = IO . read ( input_file . path , @chunk_size , offset )
206+ when 'string'
207+ string = input_file . data . byteslice ( offset , [ @chunk_size , size - offset ] . min )
208+ end
209+
210+ params [ param_name . to_sym ] = InputFile ::from_string (
211+ string ,
212+ filename : input_file . filename ,
213+ mime_type : input_file . mime_type
187214 )
188215
189- headers [ 'content-range' ] = "bytes #{ offset } -#{ [ offset + @chunk_size - 1 , payload . size - 1 ] . min } /#{ payload . size } "
216+ headers [ 'content-range' ] = "bytes #{ offset } -#{ [ offset + @chunk_size - 1 , size - 1 ] . min } /#{ size } "
190217
191218 result = call (
192219 method : 'POST' ,
@@ -203,8 +230,8 @@ def chunked_upload(
203230
204231 on_progress . call ( {
205232 id : result [ '$id' ] ,
206- progress : ( [ offset , payload . size ] . min ) . to_f /payload . size . to_f * 100.0 ,
207- size_uploaded : [ offset , payload . size ] . min ,
233+ progress : ( [ offset , size ] . min ) . to_f /size . to_f * 100.0 ,
234+ size_uploaded : [ offset , size ] . min ,
208235 chunks_total : result [ 'chunksTotal' ] ,
209236 chunks_uploaded : result [ 'chunksUploaded' ]
210237 } ) unless on_progress . nil?
@@ -231,27 +258,18 @@ def fetch(
231258 @http . use_ssl = !@self_signed
232259 payload = ''
233260
234- headers = @headers . merge ( headers . transform_keys ( & :to_s ) )
261+ headers = @headers . merge ( headers )
235262
236263 params . compact!
237264
265+ @boundary = "----A30#3ad1"
238266 if method != "GET"
239- case headers [ 'content-type' ]
267+ case headers [ : 'content-type']
240268 when 'application/json'
241269 payload = params . to_json
242270 when 'multipart/form-data'
243- multipart = MultipartBuilder . new ( )
244-
245- params . each do |name , value |
246- if value . is_a? ( Payload )
247- multipart . add ( name , value . to_s , filename : value . filename )
248- else
249- multipart . add ( name , value )
250- end
251- end
252-
253- headers [ 'content-type' ] = multipart . content_type
254- payload = multipart . body
271+ payload = encode_form_data ( params ) + "--#{ @boundary } --\r \n "
272+ headers [ :'content-type' ] = "multipart/form-data; boundary=#{ @boundary } "
255273 else
256274 payload = encode ( params )
257275 end
@@ -281,7 +299,7 @@ def fetch(
281299 return fetch ( method , uri , headers , { } , response_type , limit - 1 )
282300 end
283301
284- if response . content_type . start_with? ( 'application/json' )
302+ if response . content_type == 'application/json'
285303 begin
286304 result = JSON . parse ( response . body )
287305 rescue JSON ::ParserError => e
@@ -292,34 +310,49 @@ def fetch(
292310 raise Appwrite ::Exception . new ( result [ 'message' ] , result [ 'status' ] , result [ 'type' ] , result )
293311 end
294312
295- if response_type . respond_to? ( "from" )
296- return response_type . from ( map : result )
313+ unless response_type . respond_to? ( "from" )
314+ return result
297315 end
298316
299- return result
317+ return response_type . from ( map : result )
300318 end
301319
302320 if response . code . to_i >= 400
303321 raise Appwrite ::Exception . new ( response . body , response . code , response )
304322 end
305323
306- if response . content_type . start_with? ( 'multipart/form-data' )
307- multipart = MultipartParser . new ( response . body , response [ 'content-type' ] )
308- result = multipart . to_hash
309-
310- if response_type . respond_to? ( "from" )
311- return response_type . from ( map : result )
312- end
313-
314- return result
315- end
316-
317- if response . class . body_permitted?
318- return response . body
324+ if response . respond_to? ( "body_permitted?" )
325+ return response . body if response . body_permitted?
319326 end
320327
321328 return response
322329 end
330+
331+ def encode_form_data ( value , key = nil )
332+ case value
333+ when Hash
334+ value . map { |k , v | encode_form_data ( v , k ) } . join
335+ when Array
336+ value . map { |v | encode_form_data ( v , "#{ key } []" ) } . join
337+ when nil
338+ ''
339+ else
340+ post_body = [ ]
341+ if value . instance_of? InputFile
342+ post_body << "--#{ @boundary } "
343+ post_body << "Content-Disposition: form-data; name=\" #{ key } \" ; filename=\" #{ value . filename } \" "
344+ post_body << "Content-Type: #{ value . mime_type } "
345+ post_body << ""
346+ post_body << value . data
347+ else
348+ post_body << "--#{ @boundary } "
349+ post_body << "Content-Disposition: form-data; name=\" #{ key } \" "
350+ post_body << ""
351+ post_body << value . to_s
352+ end
353+ post_body . join ( "\r \n " ) + "\r \n "
354+ end
355+ end
323356
324357 def encode ( value , key = nil )
325358 case value
0 commit comments