Class: Cpaas::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cpaas-sdk/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Api

Returns a new instance of Api.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cpaas-sdk/api.rb', line 14

def initialize(config)
  @config = config
  @id_token_parsed = nil
  @access_token = nil
  @user_id = nil
  @client_correlator = "#{config.client_id}-ruby"

  self.class.base_uri config.base_url

  auth_token
end

Instance Attribute Details

#client_correlatorObject

Returns the value of attribute client_correlator.



12
13
14
# File 'lib/cpaas-sdk/api.rb', line 12

def client_correlator
  @client_correlator
end

#user_idObject

Returns the value of attribute user_id.



12
13
14
# File 'lib/cpaas-sdk/api.rb', line 12

def user_id
  @user_id
end

Instance Method Details

#auth_headersObject



74
75
76
77
78
# File 'lib/cpaas-sdk/api.rb', line 74

def auth_headers
  {
    'Authorization' => "Bearer #{auth_token}"
  }
end

#auth_tokenObject



80
81
82
83
84
# File 'lib/cpaas-sdk/api.rb', line 80

def auth_token
  set_tokens(get_auth_token) if token_expired

  @access_token
end

#get_auth_tokenObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cpaas-sdk/api.rb', line 86

def get_auth_token
  options = {
    body: {
      client_id: @config.client_id,
      scope: 'openid'
    },
    headers: {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
  }

  if !@config.client_secret.nil?
    credentials = {
      grant_type: 'client_credentials',
      client_secret: @config.client_secret
    }
  else
    credentials = {
      grant_type: 'password',
      username: @config.email,
      password: @config.password
    }
  end

  options[:body].merge!(credentials)

  response = send_request('/cpaas/auth/v1/token', options, :post , false)

  process_response(response, false)
end

#handle_response(response) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cpaas-sdk/api.rb', line 48

def handle_response(response)
  @parsed_response = begin
    res = convert_hash_keys(response.parsed_response)

    if response.code >= 400 && !res.nil?
      compose_error_from(res)
    else
      res || { status_code: response.code, response: response }
    end
  rescue JSON::ParserError => e
    response.success? ? { message: response.body } : { error: response.body }
  end
end

#headers(request_headers = {}, with_token = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cpaas-sdk/api.rb', line 62

def headers(request_headers = {}, with_token = false)
  base_headers = {
    'X-Cpaas-Agent' => "ruby-sdk/#{Cpaas::VERSION}",
    'Content-Type' => 'application/json',
    'Accept' => '*/*',
  }.merge(request_headers)

  return base_headers.merge(auth_headers) if with_token

  base_headers
end

#recursive_compact(hash_or_array) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/cpaas-sdk/api.rb', line 141

def recursive_compact(hash_or_array)
  p = proc do |*args|
    v = args.last
    v.delete_if(&p) if v.respond_to? :delete_if
    v.nil? || v.respond_to?(:"empty?") && v.empty?
  end

  hash_or_array.delete_if(&p)
end

#send_request(url, options = {}, verb = :get, with_token = true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cpaas-sdk/api.rb', line 26

def send_request(url, options = {}, verb = :get, with_token = true)
  body = recursive_compact(options[:body]) if options[:body]
  options[:headers] = headers(options[:headers] || {}, with_token)
  options[:body] = body.to_json if options[:headers]['Content-Type'] == 'application/json'
  options[:query] = options[:query] if options.has_key? :query

  case verb
  when :get
    response = self.class.get(url, options)
  when :post
    response = self.class.post(url, options)
  when :put
    response = self.class.put(url, options)
  when :delete
    response = self.class.delete(url, options)
  else
    raise 'Invalid Verb'
  end

  handle_response(response)
end

#set_tokens(tokens) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cpaas-sdk/api.rb', line 126

def set_tokens(tokens)
  if tokens[:access_token].nil?
    @access_token = nil
    @id_token = nil
    @id_token_parsed = nil
    @user_id = nil
  else
    @access_token = tokens[:access_token]
    @id_token = tokens[:id_token]
    @id_token_parsed = JWT.decode(tokens[:id_token], nil, false).first
    @token_parsed = JWT.decode(tokens[:access_token], nil, false).first
    @user_id = @id_token_parsed['preferred_username']
  end
end

#token_expiredObject



117
118
119
120
121
122
123
124
# File 'lib/cpaas-sdk/api.rb', line 117

def token_expired
  return true if @access_token.nil?

  min_buffer = (@token_parsed['exp'] - @token_parsed['iat']) / 2
  expires_in = @token_parsed['exp'] - Time.now.to_i - min_buffer

  expires_in < 0
end