Class: Appwrite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/appwrite/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/appwrite/client.rb', line 11

def initialize
    @chunk_size = 5*1024*1024
    @headers = {
        'user-agent' => RUBY_PLATFORM + ':ruby-' + RUBY_VERSION,
        'x-sdk-name'=> 'Ruby',
        'x-sdk-platform'=> 'server',
        'x-sdk-language'=> 'ruby',
        'x-sdk-version'=> '22.0.0',
        'X-Appwrite-Response-Format' => '1.9.0'
    }
    @endpoint = 'https://cloud.appwrite.io/v1'
end

Instance Method Details

#add_header(key, value) ⇒ self

Add Header

Parameters:

  • key (String)

    The key for the header to add

  • value (String)

    The value for the header to add

Returns:

  • (self)


171
172
173
174
175
# File 'lib/appwrite/client.rb', line 171

def add_header(key, value)
    @headers[key.downcase] = value

    self
end

#call(method:, path: '', headers: {}, params: {}, response_type: nil) ⇒ self

Send the HTTP request.

Parameters:

  • method (String)

    The HTTP method for the request

  • path (String) (defaults to: '')

    The path for the request

  • headers (Hash) (defaults to: {})

    The headers to send with the request

  • params (Hash) (defaults to: {})

    The parameters to send with the request

  • response_type (Class) (defaults to: nil)

    The type of response to return

Returns:

  • (self)


186
187
188
189
190
191
192
193
194
195
196
# File 'lib/appwrite/client.rb', line 186

def call(
    method:,
    path: '',
    headers: {},
    params: {},
    response_type: nil
)
    uri = URI.parse(@endpoint + path + ((method == "GET" && params.length) ? '?' + encode(params) : ''))

    fetch(method, uri, headers, params, response_type)
end

#chunked_upload(path:, headers:, params:, param_name: '', id_param_name: nil, on_progress: nil, response_type: nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/appwrite/client.rb', line 198

def chunked_upload(
    path:,
    headers:,
    params:,
    param_name: '',
    id_param_name: nil,
    on_progress: nil,
    response_type: nil
)
    input_file = params[param_name.to_sym]

    case input_file.source_type
    when 'path'
        size = ::File.size(input_file.path)
    when 'string'
        size = input_file.data.bytesize
    end

    if size < @chunk_size
        if input_file.source_type == 'path'
            input_file.data = IO.read(input_file.path)
        end
        params[param_name.to_sym] = input_file
        return call(
            method: 'POST',
            path: path,
            headers: headers,
            params: params,
            response_type: response_type,
        )
    end

    offset = 0
    id_param_name = id_param_name.to_sym if id_param_name
    if id_param_name&.empty? == false
        # Make a request to check if a file already exists
        current = call(
            method: "GET",
            path: "#{path}/#{params[id_param_name]}",
            headers: headers,
            params: {}
        )
        chunks_uploaded = current['chunksUploaded'].to_i
        offset = chunks_uploaded * @chunk_size
    end

    while offset < size
        case input_file.source_type
        when 'path'
            string = IO.read(input_file.path, @chunk_size, offset)
        when 'string'
            string = input_file.data.byteslice(offset, [@chunk_size, size - offset].min)
        end

        params[param_name.to_sym] = InputFile::from_string(
            string,
            filename: input_file.filename,
            mime_type: input_file.mime_type
        )

        headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size - 1].min}/#{size}"

        result = call(
            method: 'POST',
            path: path,
            headers: headers,
            params: params,
        )

        offset += @chunk_size

        if defined? result['$id']
            headers['x-appwrite-id'] = result['$id']
        end

        on_progress.call({
            id: result['$id'],
            progress: ([offset, size].min).to_f/size.to_f * 100.0,
            size_uploaded: [offset, size].min,
            chunks_total: result['chunksTotal'],
            chunks_uploaded: result['chunksUploaded']
        }) unless on_progress.nil?
    end

    return result unless response_type.respond_to?("from")

    response_type.from(map: result)
end

#set_endpoint(endpoint) ⇒ self

Set endpoint.

Parameters:

  • endpoint (String)

    The endpoint to set

Returns:

  • (self)


144
145
146
147
148
149
150
151
152
# File 'lib/appwrite/client.rb', line 144

def set_endpoint(endpoint)
    if not endpoint.start_with?('http://') and not endpoint.start_with?('https://')
        raise Appwrite::Exception.new('Invalid endpoint URL: ' + endpoint)
    end

    @endpoint = endpoint

    self
end

#set_forwarded_user_agent(value) ⇒ self

Set ForwardedUserAgent

The user agent string of the client that made the request

Parameters:

  • value (String)

    The value to set for the ForwardedUserAgent header

Returns:

  • (self)


94
95
96
97
98
# File 'lib/appwrite/client.rb', line 94

def set_forwarded_user_agent(value)
    add_header('x-forwarded-user-agent', value)

    self
end

#set_impersonate_user_email(value) ⇒ self

Set ImpersonateUserEmail

Impersonate a user by email on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.

Parameters:

  • value (String)

    The value to set for the ImpersonateUserEmail header

Returns:

  • (self)


120
121
122
123
124
# File 'lib/appwrite/client.rb', line 120

def set_impersonate_user_email(value)
    add_header('x-appwrite-impersonate-user-email', value)

    self
end

#set_impersonate_user_id(value) ⇒ self

Set ImpersonateUserId

Impersonate a user by ID on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.

Parameters:

  • value (String)

    The value to set for the ImpersonateUserId header

Returns:

  • (self)


107
108
109
110
111
# File 'lib/appwrite/client.rb', line 107

def set_impersonate_user_id(value)
    add_header('x-appwrite-impersonate-user-id', value)

    self
end

#set_impersonate_user_phone(value) ⇒ self

Set ImpersonateUserPhone

Impersonate a user by phone on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.

Parameters:

  • value (String)

    The value to set for the ImpersonateUserPhone header

Returns:

  • (self)


133
134
135
136
137
# File 'lib/appwrite/client.rb', line 133

def set_impersonate_user_phone(value)
    add_header('x-appwrite-impersonate-user-phone', value)

    self
end

#set_jwt(value) ⇒ self

Set JWT

Your secret JSON Web Token

Parameters:

  • value (String)

    The value to set for the JWT header

Returns:

  • (self)


57
58
59
60
61
# File 'lib/appwrite/client.rb', line 57

def set_jwt(value)
    add_header('x-appwrite-jwt', value)

    self
end

#set_key(value) ⇒ self

Set Key

Your secret API key

Parameters:

  • value (String)

    The value to set for the Key header

Returns:

  • (self)


44
45
46
47
48
# File 'lib/appwrite/client.rb', line 44

def set_key(value)
    add_header('x-appwrite-key', value)

    self
end

#set_locale(value) ⇒ self

Set Locale

Parameters:

  • value (String)

    The value to set for the Locale header

Returns:

  • (self)


68
69
70
71
72
# File 'lib/appwrite/client.rb', line 68

def set_locale(value)
    add_header('x-appwrite-locale', value)

    self
end

#set_project(value) ⇒ self

Set Project

Your project ID

Parameters:

  • value (String)

    The value to set for the Project header

Returns:

  • (self)


31
32
33
34
35
# File 'lib/appwrite/client.rb', line 31

def set_project(value)
    add_header('x-appwrite-project', value)

    self
end

#set_self_signed(self_signed = true) ⇒ self

Set self signed.

Parameters:

  • self_signed (String) (defaults to: true)

    Whether to allow self signed certificates.

Returns:

  • (self)


159
160
161
162
163
# File 'lib/appwrite/client.rb', line 159

def set_self_signed(self_signed = true)
    @self_signed = self_signed

    self
end

#set_session(value) ⇒ self

Set Session

The user session to authenticate with

Parameters:

  • value (String)

    The value to set for the Session header

Returns:

  • (self)


81
82
83
84
85
# File 'lib/appwrite/client.rb', line 81

def set_session(value)
    add_header('x-appwrite-session', value)

    self
end