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