class KintoBox::KintoClient
Public Class Methods
Initializes a new Kinto client.
@param [String] server Url of the server without the version @param [Hash] options Optional parameter. If the hash contains :username
and :password, it will be used to authenticate. `options` parameter Can be used to pass in credentials. If no credentials are passed, it looks for KINTO_API_TOKEN environment variable.
@return [KintoBox::KintoClient] A kinto client object
# File lib/kinto_box.rb, line 37 def initialize(server, username: nil, password: nil) self.class.base_uri(URI.join(server, '/v1/').to_s) auth = if username && password Base64.encode64("#{username}:#{password}") else ENV['KINTO_API_TOKEN'] end self.class.headers('Authorization' => "Basic #{auth}") @server = KintoServer.new(client: self) end
Public Instance Methods
Make batch requests
results = client.batch do req req.add_request(...) end
# File lib/kinto_box.rb, line 164 def batch req = create_batch_request if block_given? yield req req.execute else req end end
Get reference to a bucket
@param [String] bucket_id The id of the bucket @return [KintoBox::KintoBucket] A kinto bucket object
# File lib/kinto_box.rb, line 55 def bucket(bucket_id) @server.bucket(bucket_id) end
Make batch requests @return [KintoBatchRequest] New back request object
# File lib/kinto_box.rb, line 156 def create_batch_request KintoBatchRequest.new self end
Create a bucket
@param [String] bucket_id The id of the bucket @return [KintoBox::KintoBucket] A kinto bucket object
# File lib/kinto_box.rb, line 84 def create_bucket(bucket_id) @server.create_bucket(bucket_id) end
Get a request object @param [String] method @param [String] path @param [Hash] body @return [KintoRequest] Request object
# File lib/kinto_box.rb, line 150 def create_request(method, path, body = {}) KintoRequest.new self, method, path, body end
Get current user id
@return [String] current user id
# File lib/kinto_box.rb, line 69 def current_user_id @server.current_user_id end
Calls http DELETE on path
@params [String]path Url path @return [Hash] response body
# File lib/kinto_box.rb, line 125 def delete(path) request 'DELETE', path end
Delete all buckets @return [Hash] API response
# File lib/kinto_box.rb, line 90 def delete_buckets @server.delete_buckets end
Calls http GET on path
@params [String]path Url path @return [Hash] response body
# File lib/kinto_box.rb, line 133 def get(path) request 'GET', path end
Calls http HEAD on path
@params [String]path Url path @return [Hash] response body
# File lib/kinto_box.rb, line 141 def head(path) request 'HEAD', path end
List of buckets
@return [Hash] with list of buckets
# File lib/kinto_box.rb, line 76 def list_buckets @server.list_buckets end
Calls http PATCH on path
@params [String]path Url path @params [Hash] data to be sent in the body @return [Hash] response body
# File lib/kinto_box.rb, line 117 def patch(path, data) request 'PATCH', path, body: data.to_json end
Calls http POST on path
@params [String]path Url path @params [Hash] data to be sent in the body @return [Hash] response body
# File lib/kinto_box.rb, line 108 def post(path, data = {}) request 'POST', path, body: data.to_json end
Calls http PUT on path
@params [String]path Url path @params [Hash] data to be sent in the body @return [Hash] response body
# File lib/kinto_box.rb, line 99 def put(path, data = {}) request 'PUT', path, body: data.to_json end
Send a prepared request @param [KintoRequest] request @return [Hash] response
# File lib/kinto_box.rb, line 177 def send_request(request_obj) request(request_obj.method, request_obj.path, body: request_obj.body.to_json) end
Get server information
@return [Hash] Server info as a hash
# File lib/kinto_box.rb, line 62 def server_info @server.info end
Private Instance Methods
Parse and process HTTP response. Check for codes, and parse return body. @param [Object] Response object @param [Boolean] <head_instead> Return headers instead of body @return [Hash] Response payload
# File lib/kinto_box.rb, line 204 def handle_response(resp, head_instead: false) case resp.code when 200, 201 head_instead ? resp.headers : JSON.parse(resp.body) when 202, 204 true when 400 raise BadRequest, resp when 401 raise NotAllowed, resp when 403 raise NotAuthorized, resp when 404 raise NotFound, resp else if resp.code >= 500 raise ServerError, resp else raise Error, resp end end end
Handle all the kinds of requests
@param [String] HTTP method @param [String] Path to query @return [Hash] Return data
# File lib/kinto_box.rb, line 188 def request(method, path, **kwargs) verbs = %w(put get post delete options head move copy patch) method = method.to_s.downcase raise HTTPBadRequest("Unsupported HTTP method #{method}") unless verbs.include?(method) resp = self.class.send(method.to_sym, path, **kwargs) if method == 'head' handle_response resp, head_instead: true else handle_response resp end end