class KintoBox::KintoClient

Public Class Methods

new(server, username: nil, password: nil) click to toggle source

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

batch() { |req| ... } click to toggle source

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
bucket(bucket_id) click to toggle source

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
create_batch_request() click to toggle source

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_bucket(bucket_id) click to toggle source

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
create_request(method, path, body = {}) click to toggle source

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
current_user_id() click to toggle source

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
delete(path) click to toggle source

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_buckets() click to toggle source

Delete all buckets @return [Hash] API response

# File lib/kinto_box.rb, line 90
def delete_buckets
  @server.delete_buckets
end
get(path) click to toggle source

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
head(path) click to toggle source

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_buckets() click to toggle source

List of buckets

@return [Hash] with list of buckets

# File lib/kinto_box.rb, line 76
def list_buckets
  @server.list_buckets
end
patch(path, data) click to toggle source

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
post(path, data = {}) click to toggle source

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
put(path, data = {}) click to toggle source

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_request(request_obj) click to toggle source

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
server_info() click to toggle source

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

handle_response(resp, head_instead: false) click to toggle source

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
request(method, path, **kwargs) click to toggle source

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