class SelfSDK::RestClient

Attributes

env[R]
jwt[R]
self_url[R]

Public Class Methods

new(url, app_id, app_key, env) click to toggle source

RestClient initializer

@param url [string] self-messaging url @param token [string] jwt token identifying the authenticated user

# File lib/client.rb, line 15
def initialize(url, app_id, app_key, env)
  SelfSDK.logger.info "client setup with #{url}"
  @self_url = url
  @env = env
  @jwt = SelfSDK::JwtService.new(app_id, app_key)
end

Public Instance Methods

app(id) click to toggle source

Get app details

@param id [string] app self_id.

# File lib/client.rb, line 32
def app(id)
  get_identity "/v1/apps/#{id}"
end
device_public_key(id, did) click to toggle source

Get the active public key for a device

@param id [string] identity id

# File lib/client.rb, line 110
def device_public_key(id, did)
  i = entity(id)
  sg = SelfSDK::SignatureGraph.new(i[:history])
  sg.key_by_device(did)
end
devices(id) click to toggle source

Lists all devices assigned to the given identity

@param id [string] identity id

# File lib/client.rb, line 51
def devices(id)
  res = get "/v1/identities/#{id}/devices"
  body = JSON.parse(res.body, symbolize_names: true)
  if res.code != 200
    SelfSDK.logger.error "identity response : #{body[:message]}"
    raise "you need connection permissions"
  end
  body
end
entity(id) click to toggle source

Get app/identity details

@param id [string] app/identity self_id.

# File lib/client.rb, line 39
def entity(id)
  #TODO : Consider a better check for this conditional
  if id.length == 11
    return identity(id)
  else
    return app(id)
  end
end
get(endpoint) click to toggle source
# File lib/client.rb, line 85
def get(endpoint)
  res = nil
  loop do
    res = HTTParty.get("#{@self_url}#{endpoint}", headers: {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{@jwt.auth_token}"
    })
    break if res.code != 503
    sleep 2
  end
  return res
end
identity(id) click to toggle source

Get identity details

@param id [string] identity self_id.

# File lib/client.rb, line 25
def identity(id)
  get_identity "/v1/identities/#{id}"
end
post(endpoint, body) click to toggle source
# File lib/client.rb, line 70
def post(endpoint, body)
  res = nil
  loop do
    res = HTTParty.post("#{@self_url}#{endpoint}",
                  headers: {
                      'Content-Type' => 'application/json',
                      'Authorization' => "Bearer #{@jwt.auth_token}"
                  },
                  body: body)
    break if res.code != 503
    sleep 2
  end
  return res
end
public_key(id, kid) click to toggle source

Lists all public keys stored on self for the given ID

@param id [string] identity id

# File lib/client.rb, line 101
def public_key(id, kid)
  i = entity(id)
  sg = SelfSDK::SignatureGraph.new(i[:history])
  sg.key_by_id(kid)
end
public_keys(id) click to toggle source

Lists all public keys stored on self for the given ID

@param id [string] identity id DEPRECATED

# File lib/client.rb, line 65
def public_keys(id)
  i = entity(id)
  i[:public_keys]
end

Private Instance Methods

get_identity(endpoint) click to toggle source
# File lib/client.rb, line 118
def get_identity(endpoint)
  res = get endpoint
  body = JSON.parse(res.body, symbolize_names: true)
  if res.code != 200
    SelfSDK.logger.error "app response : #{body[:message]}"
    raise body[:message]
  end
  body
end