Skip to content

Commit 3d9fc65

Browse files
committed
Fix deploy
1 parent d4daa9d commit 3d9fc65

24 files changed

+763
-906
lines changed

.github/workflows/publish.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ jobs:
1919

2020
- name: Set up RubyGems
2121
run: |
22-
cat "---\n:rubygems_api_key: ${{ secrets.RUBYGEMS_TOKEN }}\n" > ~/.gem/credentials
22+
echo -e "---\n:rubygems_api_key: $RUBYGEMS_TOKEN\n" > ~/.gem/credentials
2323
chmod 0600 ~/.gem/credentials
24+
env:
25+
RUBYGEMS_TOKEN: $
2426

2527
- name: Build gem
2628
run: gem build appwrite.gemspec

Gemfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
source 'https://rubygems.org'
22

3-
gem 'mime-types', '~> 3.4.1'
4-
5-
gemspec
6-
3+
gemspec

appwrite.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |spec|
22

33
spec.name = 'appwrite'
4-
spec.version = '13.0.0'
4+
spec.version = '12.1.1'
55
spec.license = 'BSD-3-Clause'
66
spec.summary = 'Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API'
77
spec.author = 'Appwrite Team'

docs/examples/functions/create-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ functions = Functions.new(client)
1111

1212
result = functions.create_deployment(
1313
function_id: '<FUNCTION_ID>',
14-
code: Payload.from_file('/path/to/file.png'),
14+
code: InputFile.from_path('dir/file.png'),
1515
activate: false,
1616
entrypoint: '<ENTRYPOINT>', # optional
1717
commands: '<COMMANDS>' # optional

docs/examples/functions/create-execution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ functions = Functions.new(client)
1111

1212
result = functions.create_execution(
1313
function_id: '<FUNCTION_ID>',
14-
body: Payload.from_json({ "x": "y" }), # optional
14+
body: , # optional
1515
async: false, # optional
1616
path: '<PATH>', # optional
1717
method: ExecutionMethod::GET, # optional

docs/examples/storage/create-file.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ storage = Storage.new(client)
1212
result = storage.create_file(
1313
bucket_id: '<BUCKET_ID>',
1414
file_id: '<FILE_ID>',
15-
file: Payload.from_file('/path/to/file.png'),
15+
file: InputFile.from_path('dir/file.png'),
1616
permissions: ["read("any")"] # optional
1717
)

lib/appwrite.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
require_relative 'appwrite/client'
77
require_relative 'appwrite/service'
88
require_relative 'appwrite/exception'
9-
require_relative 'appwrite/payload'
10-
require_relative 'appwrite/multipart'
9+
require_relative 'appwrite/input_file'
1110
require_relative 'appwrite/query'
1211
require_relative 'appwrite/permission'
1312
require_relative 'appwrite/role'

lib/appwrite/client.rb

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/appwrite/input_file.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'mime/types'
2+
3+
module Appwrite
4+
class InputFile
5+
attr_accessor :path
6+
attr_accessor :filename
7+
attr_accessor :mime_type
8+
attr_accessor :source_type
9+
attr_accessor :data
10+
11+
def self.from_path(path)
12+
instance = InputFile.new
13+
instance.path = path
14+
instance.filename = ::File.basename(path)
15+
instance.mime_type = MIME::Types.type_for(path).first.content_type
16+
instance.source_type = 'path'
17+
instance
18+
end
19+
20+
def self.from_string(string, filename: nil, mime_type: nil)
21+
instance = InputFile.new
22+
instance.data = string
23+
instance.filename = filename
24+
instance.mime_type = mime_type
25+
instance.source_type = 'string'
26+
instance
27+
end
28+
29+
def self.from_bytes(bytes, filename: nil, mime_type: nil)
30+
self.from_string(bytes.pack('C*'), filename: filename, mime_type: mime_type)
31+
end
32+
end
33+
end

lib/appwrite/models/document.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ class Document
1010
attr_reader :updated_at
1111
attr_reader :permissions
1212
attr_reader :data
13+
1314
def initialize(
1415
id:,
1516
collection_id:,
1617
database_id:,
1718
created_at:,
1819
updated_at:,
1920
permissions:,
20-
data: )
21+
data:
22+
)
2123
@id = id
2224
@collection_id = collection_id
2325
@database_id = database_id
2426
@created_at = created_at
2527
@updated_at = updated_at
2628
@permissions = permissions
27-
@data = data end
29+
@data = data
30+
end
2831

2932
def self.from(map:)
3033
Document.new(
@@ -34,7 +37,8 @@ def self.from(map:)
3437
created_at: map["$createdAt"],
3538
updated_at: map["$updatedAt"],
3639
permissions: map["$permissions"],
37-
data: map )
40+
data: map
41+
)
3842
end
3943

4044
def to_map
@@ -45,7 +49,8 @@ def to_map
4549
"$createdAt": @created_at,
4650
"$updatedAt": @updated_at,
4751
"$permissions": @permissions,
48-
"data": @data }
52+
"data": @data
53+
}
4954
end
5055

5156
def convert_to(from_json)

0 commit comments

Comments
 (0)