Class: SelfSDK::RestClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, app_id, app_key, env) ⇒ RestClient

RestClient initializer

Parameters:

  • url (string)

    self-messaging url

  • token (string)

    jwt token identifying the authenticated user



15
16
17
18
19
20
# 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

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/client.rb', line 9

def env
  @env
end

#jwtObject (readonly)

Returns the value of attribute jwt.



9
10
11
# File 'lib/client.rb', line 9

def jwt
  @jwt
end

#self_urlObject (readonly)

Returns the value of attribute self_url.



9
10
11
# File 'lib/client.rb', line 9

def self_url
  @self_url
end

Instance Method Details

#app(id) ⇒ Object

Get app details

Parameters:

  • id (string)

    app self_id.



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

def app(id)
  get_identity "/v1/apps/#{id}"
end

#device_public_key(id, did) ⇒ Object

Get the active public key for a device

Parameters:

  • id (string)

    identity id



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

def device_public_key(id, did)
  i = entity(id)
  sg = SelfSDK::SignatureGraph.new(i[:history])
  sg.key_by_device(did)
end

#devices(id) ⇒ Object

Lists all devices assigned to the given identity

Parameters:

  • id (string)

    identity id



51
52
53
54
55
56
57
58
59
# 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.debug "getting devices response : #{body[:message]}"
    raise "you need connection permissions"
  end
  body
end

#entity(id) ⇒ Object

Get app/identity details

Parameters:

  • id (string)

    app/identity self_id.



39
40
41
42
43
44
45
46
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/client.rb', line 81

def get(endpoint)
  safe_request do
    HTTParty.get("#{@self_url}#{endpoint}", 
                 headers: {
                    'Content-Type'  => 'application/json',
                    'Authorization' => "Bearer #{@jwt.auth_token}"
                  })
  end
end

#identity(id) ⇒ Object

Get identity details

Parameters:

  • id (string)

    identity self_id.



25
26
27
# File 'lib/client.rb', line 25

def identity(id)
  get_identity "/v1/identities/#{id}"
end

#post(endpoint, body) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/client.rb', line 70

def post(endpoint, body)
  safe_request do
    HTTParty.post("#{@self_url}#{endpoint}",
                  headers: {
                    'Content-Type' => 'application/json',
                    'Authorization' => "Bearer #{@jwt.auth_token}"
                  },
                  body: body)
  end
end

#public_key(id, kid) ⇒ Object

Lists all public keys stored on self for the given ID

Parameters:

  • id (string)

    identity id



113
114
115
116
117
# File 'lib/client.rb', line 113

def public_key(id, kid)
  i = entity(id)
  sg = SelfSDK::SignatureGraph.new(i[:history])
  sg.key_by_id(kid)
end

#public_keys(id) ⇒ Object

Lists all public keys stored on self for the given ID

DEPRECATED

Parameters:

  • id (string)

    identity id



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

def public_keys(id)
  i = entity(id)
  i[:public_keys]
end

#safe_request(&block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/client.rb', line 91

def safe_request(&block)
  res = nil
  loop do
    begin
      res = block.call

      break if res.code == 200
      next if res.code == 503
      next if body[:error_id] == 'token_expired'

      break
    rescue StandardError
      # retry if the server is down
    end
    sleep 2
  end
  res
end