Class: IOTServices::Evolens::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, client_id:, client_secret:, admin_credentials: nil, http_opts: {}, http: Adapters::HTTP.new(http_opts.merge(base_url: base_url)), token: nil, token_expires: nil) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/iot_services/evolens/client.rb', line 6

def initialize(
  base_url:,
  client_id:,
  client_secret:,
  admin_credentials: nil, # Only needed for admin role endpoints
  http_opts: {},
  http: Adapters::HTTP.new(http_opts.merge(base_url: base_url)),
  token: nil,
  token_expires: nil
)
  @base_url = base_url
  @client_id = client_id
  @client_secret = client_secret
  @admin_credentials = admin_credentials
  @http = http
  @token = token
  @token_expires = token_expires
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/iot_services/evolens/client.rb', line 4

def token
  @token
end

#token_expiresObject (readonly)

Returns the value of attribute token_expires.



4
5
6
# File 'lib/iot_services/evolens/client.rb', line 4

def token_expires
  @token_expires
end

Instance Method Details

#admin_panel_loginObject

To access admin role operations, like creating an ECP, the API requires an additional login step. Should not be needed to use this explicitly as it’s called from the main login method.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/iot_services/evolens/client.rb', line 47

def 
  headers = {'Authorization' => @token, 'Content-Type' => 'application/json'}
  response = @http.post('/ga/user/login', headers: headers, json: @admin_credentials)
  on_ok_response response do
    # I think it's safe to override the token as the admin panel token appears to have a shorter span.
    # This way with can reuse the on_private_endpoint functionality, otherwise we would have to replicate
    # the whole relogin behaviour for this different tokens.
    @token = response.body['data'].first['token']
    @token_expires = Time.parse(response.body['data'].first['tokenExpire'])
  end
  self
end

#authenticateObject



25
26
27
28
29
30
31
# File 'lib/iot_services/evolens/client.rb', line 25

def authenticate
  response = @http.get('/oauth/authorize', headers: {'X-Client-Id' => @client_id})
  on_ok_response response do
    @token = response.body['data']['authorization']
  end
  self
end

#authorizeObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/iot_services/evolens/client.rb', line 33

def authorize
  headers = {'X-Client-Id' => @client_id, 'X-Client-Secret' => @client_secret}
  response = @http.get("/oauth/authorize/#{@token}", headers: headers)
  on_ok_response response do
    # I think it's safe to override the token obtained during the authentication step with this new one,
    # as it's no longer needed on following requests.
    @token = response.body['data']['token']
    @token_expires = Time.parse(response.body['data']['expire'])
  end
  self
end

#create_ecp(store_id:, ecp_code:) ⇒ Object

Creates an ECP “placeholder” that later should be associated with its email. Requires Admin role credentials.



101
102
103
104
105
106
# File 'lib/iot_services/evolens/client.rb', line 101

def create_ecp store_id:, ecp_code:
  json = {'EcpCode' => ecp_code}
  on_private_endpoint do |headers|
    @http.post("/ga/ecps/#{store_id}", headers: headers, json: json)
  end.first
end

#deregister_ecp(ecp_id:, ecp_code:) ⇒ Object

Unlinks an ECP email, i.e. leaves it open to register another email Requires Admin role credentials.



124
125
126
127
128
# File 'lib/iot_services/evolens/client.rb', line 124

def deregister_ecp ecp_id:, ecp_code:
  on_private_endpoint do |headers|
    @http.put("/ga/ecps/unlink?id=#{ecp_id}&EcpCode=#{ecp_code}", headers: headers)
  end.first
end

#get_ecpsObject

Returns the list of created ECPs. Requires Admin role credentials.



132
133
134
135
136
# File 'lib/iot_services/evolens/client.rb', line 132

def get_ecps
  on_private_endpoint do |headers|
    @http.get('/ga/ecps', headers: headers)
  end
end

#get_providers_and_designsObject

Returns an [Array] of providers and its designs. Requires ECP role credentials.



93
94
95
96
97
# File 'lib/iot_services/evolens/client.rb', line 93

def get_providers_and_designs
  on_private_endpoint do |headers|
    @http.get('/ecp/user/GetProvidersAndDesign', headers: headers)
  end
end

#loginObject

Executes the authentication and authorization procedure only if we haven’t obtain the tokens yet, or if they’ve expired. Idempotent operation.



62
63
64
65
# File 'lib/iot_services/evolens/client.rb', line 62

def 
  relogin if authorization_expired?
  self
end

#new_questionary(data) ⇒ Object

Uploads a patients visual profile. Requires ECP role credentials.



85
86
87
88
89
# File 'lib/iot_services/evolens/client.rb', line 85

def new_questionary data
  on_private_endpoint do |headers|
    @http.post('/ecp/store/wearer/new/questionary', headers: headers, json: data)
  end
end

#new_wearer(data) ⇒ Object

To create a visual profile, we first need to create the patient. Make sure you’ve provided ECP role credentials in the constructor.



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

def new_wearer data
  on_private_endpoint do |headers|
    @http.post('/ecp/store/wearer/new', headers: headers, json: data)
  end
end

#register_ecp(store_code:, ecp_code:, email:, password:) ⇒ Object

Links an ECP “placeholder” with its email and password. Requires ECP role credentials.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/iot_services/evolens/client.rb', line 110

def register_ecp store_code:, ecp_code:, email:, password:
  json = {
    storeCode: store_code,
    ecpUserCode: ecp_code,
    email: email,
    password: password
  }
  on_private_endpoint do |headers|
    @http.post('/ecp/user/register', headers: headers, json: json)
  end
end

#reloginObject

Executes the authentication and authorization procedure to obtain the tokens needed for the subsequent requests.



68
69
70
71
72
73
# File 'lib/iot_services/evolens/client.rb', line 68

def relogin
  reset_auth_data
  authenticate
  authorize
   if @admin_credentials
end