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'=> '13.0.0',
        'x-appwrite-response-format' => '1.6.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)


123
124
125
126
127
# File 'lib/appwrite/client.rb', line 123

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)


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/appwrite/client.rb', line 138

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/appwrite/client.rb', line 150

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

    if payload.size < @chunk_size
        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 && params[id_param_name] != "unique()"
        # 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 < payload.size
        params[param_name.to_sym] = Payload.from_binary(
            payload.to_binary(offset, [@chunk_size, payload.size - offset].min),
            filename: payload.filename
        )

        headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, payload.size - 1].min}/#{payload.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, payload.size].min).to_f/payload.size.to_f * 100.0,
            size_uploaded: [offset, payload.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)


100
101
102
103
104
# File 'lib/appwrite/client.rb', line 100

def set_endpoint(endpoint)
    @endpoint = endpoint
    
    self
end

#set_forwarded_user_agent(value) ⇒ self

Set ForwardedUserAgent

The user agent string of the client that made the request

# @param [String] value The value to set for the ForwardedUserAgent header

Returns:

  • (self)


89
90
91
92
93
# File 'lib/appwrite/client.rb', line 89

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

    self
end

#set_jwt(value) ⇒ self

Set JWT

Your secret JSON Web Token

# @param [String] value The value to set for the JWT header

Returns:

  • (self)


54
55
56
57
58
# File 'lib/appwrite/client.rb', line 54

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

    self
end

#set_key(value) ⇒ self

Set Key

Your secret API key

# @param [String] value The value to set for the Key header

Returns:

  • (self)


42
43
44
45
46
# File 'lib/appwrite/client.rb', line 42

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)


65
66
67
68
69
# File 'lib/appwrite/client.rb', line 65

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

    self
end

#set_project(value) ⇒ self

Set Project

Your project ID

# @param [String] value The value to set for the Project header

Returns:

  • (self)


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

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)


111
112
113
114
115
# File 'lib/appwrite/client.rb', line 111

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

# @param [String] value The value to set for the Session header

Returns:

  • (self)


77
78
79
80
81
# File 'lib/appwrite/client.rb', line 77

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

    self
end