@@ -15,16 +15,17 @@ def initialize
15
15
'x-sdk-name' => 'Ruby' ,
16
16
'x-sdk-platform' => 'server' ,
17
17
'x-sdk-language' => 'ruby' ,
18
- 'x-sdk-version' => '13.0.0' ,
19
- 'x-appwrite-response-format ' => '1.6.0'
18
+ 'x-sdk-version' => '13.0.0' ,
19
+ 'X-Appwrite-Response-Format ' => '1.6.0'
20
20
}
21
21
@endpoint = 'https://cloud.appwrite.io/v1'
22
22
end
23
23
24
24
# Set Project
25
25
#
26
26
# Your project ID
27
- # # @param [String] value The value to set for the Project header
27
+ #
28
+ # @param [String] value The value to set for the Project header
28
29
#
29
30
# @return [self]
30
31
def set_project ( value )
@@ -36,7 +37,8 @@ def set_project(value)
36
37
# Set Key
37
38
#
38
39
# Your secret API key
39
- # # @param [String] value The value to set for the Key header
40
+ #
41
+ # @param [String] value The value to set for the Key header
40
42
#
41
43
# @return [self]
42
44
def set_key ( value )
@@ -48,7 +50,8 @@ def set_key(value)
48
50
# Set JWT
49
51
#
50
52
# Your secret JSON Web Token
51
- # # @param [String] value The value to set for the JWT header
53
+ #
54
+ # @param [String] value The value to set for the JWT header
52
55
#
53
56
# @return [self]
54
57
def set_jwt ( value )
@@ -71,7 +74,8 @@ def set_locale(value)
71
74
# Set Session
72
75
#
73
76
# The user session to authenticate with
74
- # # @param [String] value The value to set for the Session header
77
+ #
78
+ # @param [String] value The value to set for the Session header
75
79
#
76
80
# @return [self]
77
81
def set_session ( value )
@@ -83,7 +87,8 @@ def set_session(value)
83
87
# Set ForwardedUserAgent
84
88
#
85
89
# The user agent string of the client that made the request
86
- # # @param [String] value The value to set for the ForwardedUserAgent header
90
+ #
91
+ # @param [String] value The value to set for the ForwardedUserAgent header
87
92
#
88
93
# @return [self]
89
94
def set_forwarded_user_agent ( value )
@@ -114,6 +119,7 @@ def set_self_signed(self_signed = true)
114
119
self
115
120
end
116
121
122
+
117
123
# Add Header
118
124
#
119
125
# @param [String] key The key for the header to add
@@ -156,9 +162,20 @@ def chunked_upload(
156
162
on_progress : nil ,
157
163
response_type : nil
158
164
)
159
- 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
160
173
161
- 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
162
179
return call (
163
180
method : 'POST' ,
164
181
path : path ,
@@ -182,13 +199,21 @@ def chunked_upload(
182
199
offset = chunks_uploaded * @chunk_size
183
200
end
184
201
185
- while offset < payload . size
186
- params [ param_name . to_sym ] = Payload . from_binary (
187
- payload . to_binary ( offset , [ @chunk_size , payload . size - offset ] . min ) ,
188
- 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
189
214
)
190
215
191
- 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 } "
192
217
193
218
result = call (
194
219
method : 'POST' ,
@@ -205,8 +230,8 @@ def chunked_upload(
205
230
206
231
on_progress . call ( {
207
232
id : result [ '$id' ] ,
208
- progress : ( [ offset , payload . size ] . min ) . to_f /payload . size . to_f * 100.0 ,
209
- size_uploaded : [ offset , payload . size ] . min ,
233
+ progress : ( [ offset , size ] . min ) . to_f /size . to_f * 100.0 ,
234
+ size_uploaded : [ offset , size ] . min ,
210
235
chunks_total : result [ 'chunksTotal' ] ,
211
236
chunks_uploaded : result [ 'chunksUploaded' ]
212
237
} ) unless on_progress . nil?
@@ -233,27 +258,18 @@ def fetch(
233
258
@http . use_ssl = !@self_signed
234
259
payload = ''
235
260
236
- headers = @headers . merge ( headers . transform_keys ( & :to_s ) )
261
+ headers = @headers . merge ( headers )
237
262
238
263
params . compact!
239
264
265
+ @boundary = "----A30#3ad1"
240
266
if method != "GET"
241
- case headers [ 'content-type' ]
267
+ case headers [ : 'content-type']
242
268
when 'application/json'
243
269
payload = params . to_json
244
270
when 'multipart/form-data'
245
- multipart = MultipartBuilder . new ( )
246
-
247
- params . each do |name , value |
248
- if value . is_a? ( Payload )
249
- multipart . add ( name , value . to_s , filename : value . filename )
250
- else
251
- multipart . add ( name , value )
252
- end
253
- end
254
-
255
- headers [ 'content-type' ] = multipart . content_type
256
- payload = multipart . body
271
+ payload = encode_form_data ( params ) + "--#{ @boundary } --\r \n "
272
+ headers [ :'content-type' ] = "multipart/form-data; boundary=#{ @boundary } "
257
273
else
258
274
payload = encode ( params )
259
275
end
@@ -283,7 +299,7 @@ def fetch(
283
299
return fetch ( method , uri , headers , { } , response_type , limit - 1 )
284
300
end
285
301
286
- if response . content_type . start_with? ( 'application/json' )
302
+ if response . content_type == 'application/json'
287
303
begin
288
304
result = JSON . parse ( response . body )
289
305
rescue JSON ::ParserError => e
@@ -294,34 +310,49 @@ def fetch(
294
310
raise Appwrite ::Exception . new ( result [ 'message' ] , result [ 'status' ] , result [ 'type' ] , result )
295
311
end
296
312
297
- if response_type . respond_to? ( "from" )
298
- return response_type . from ( map : result )
313
+ unless response_type . respond_to? ( "from" )
314
+ return result
299
315
end
300
316
301
- return result
317
+ return response_type . from ( map : result )
302
318
end
303
319
304
320
if response . code . to_i >= 400
305
321
raise Appwrite ::Exception . new ( response . body , response . code , response )
306
322
end
307
323
308
- if response . content_type . start_with? ( 'multipart/form-data' )
309
- multipart = MultipartParser . new ( response . body , response [ 'content-type' ] )
310
- result = multipart . to_hash
311
-
312
- if response_type . respond_to? ( "from" )
313
- return response_type . from ( map : result )
314
- end
315
-
316
- return result
317
- end
318
-
319
- if response . class . body_permitted?
320
- return response . body
324
+ if response . respond_to? ( "body_permitted?" )
325
+ return response . body if response . body_permitted?
321
326
end
322
327
323
328
return response
324
329
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
325
356
326
357
def encode ( value , key = nil )
327
358
case value
0 commit comments