Class: KnuVerse::Knufactor::APIClientBase
Overview
The API Client Base class
Instance Attribute Summary collapse
Instance Method Summary
collapse
#json_escape, #raw, #version
#validate_creds, #validate_opts, #validate_server
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
6
7
8
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 6
def account
@account
end
|
#apikey ⇒ Object
Returns the value of attribute apikey.
6
7
8
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 6
def apikey
@apikey
end
|
#base_uri ⇒ Object
Returns the value of attribute base_uri.
5
6
7
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 5
def base_uri
@base_uri
end
|
#secret ⇒ Object
Returns the value of attribute secret.
6
7
8
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 6
def secret
@secret
end
|
#server ⇒ Object
Returns the value of attribute server.
5
6
7
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 5
def server
@server
end
|
Instance Method Details
#about_service ⇒ Object
11
12
13
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 11
def about_service
get 'about'
end
|
#api_uri ⇒ Object
15
16
17
18
19
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 15
def api_uri
@api_uri ||= URI.parse(server)
@api_uri.path = base_uri
@api_uri
end
|
#authenticated? ⇒ Boolean
21
22
23
24
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 21
def authenticated?
raise Exceptions::APIClientNotConfigured unless configured?
@auth_token && @last_auth && ((Time.now.utc - @last_auth) < (60 * 14)) ? true : false
end
|
26
27
28
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 26
def configured?
@configured ? true : false
end
|
#get(uri, data = nil) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 50
def get(uri, data = nil)
refresh_auth unless authenticated?
client_action do |client|
if data
JSON.parse client[uri].get(.merge(params: data))
else
JSON.parse client[uri].get()
end
end
end
|
30
31
32
33
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 30
def
base = { content_type: :json, accept: :json }
authenticated? ? base.merge(authorization: ) : base
end
|
#patch(uri, data) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 74
def patch(uri, data)
refresh_auth unless authenticated?
client_action do |client|
response = client[uri].patch(json_escape(data.to_json), )
if response && !response.empty?
JSON.parse(response)
else
true
end
end
end
|
#post(uri, data = nil) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 62
def post(uri, data = nil)
refresh_auth unless authenticated? || uri == 'auth'
client_action do |client|
if data
JSON.parse client[uri].post(json_escape(data.to_json), )
else
JSON.parse client[uri].post(nil, )
end
end
end
|
#put(uri, data) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 87
def put(uri, data)
refresh_auth unless authenticated?
client_action do |client|
client[uri].put(json_escape(data.to_json), )
end
end
|
#refresh_auth ⇒ Object
44
45
46
47
48
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 44
def refresh_auth
@auth_token = refresh_auth_bearer
@last_auth = Time.now.utc
true
end
|
#refresh_auth_bearer ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/knuverse/knufactor/api_client_base.rb', line 35
def refresh_auth_bearer
auth_data = {
key_id: @apikey,
secret: @secret
}
resp = post('auth', auth_data)
resp['jwt']
end
|