Class: KnuVerse::Knufactor::APIClientBase

Inherits:
Object
  • Object
show all
Includes:
Helpers::APIClient, Validations::APIClient
Defined in:
lib/knuverse/knufactor/api_client_base.rb

Overview

The API Client Base class

Direct Known Subclasses

APIClient, SimpleAPIClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::APIClient

#json_escape, #raw, #version

Methods included from Validations::APIClient

#validate_creds, #validate_opts, #validate_server

Instance Attribute Details

#accountObject

Returns the value of attribute account.



6
7
8
# File 'lib/knuverse/knufactor/api_client_base.rb', line 6

def 
  @account
end

#apikeyObject

Returns the value of attribute apikey.



6
7
8
# File 'lib/knuverse/knufactor/api_client_base.rb', line 6

def apikey
  @apikey
end

#base_uriObject (readonly)

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

#secretObject

Returns the value of attribute secret.



6
7
8
# File 'lib/knuverse/knufactor/api_client_base.rb', line 6

def secret
  @secret
end

#serverObject (readonly)

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_serviceObject



11
12
13
# File 'lib/knuverse/knufactor/api_client_base.rb', line 11

def about_service
  get 'about'
end

#api_uriObject



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

Returns:

  • (Boolean)

Raises:



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

#configured?Boolean

Returns:

  • (Boolean)


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(json_headers.merge(params: data))
    else
      JSON.parse client[uri].get(json_headers)
    end
  end
end

#json_headersObject



30
31
32
33
# File 'lib/knuverse/knufactor/api_client_base.rb', line 30

def json_headers
  base = { content_type: :json, accept: :json }
  authenticated? ? base.merge(authorization: auth_header) : 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), json_headers)
    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), json_headers)
    else
      JSON.parse client[uri].post(nil, json_headers)
    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), json_headers)
  end
end

#refresh_authObject



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_bearerObject



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