Class: Cpaas::Api
Instance Attribute Summary collapse
-
#client_correlator ⇒ Object
Returns the value of attribute client_correlator.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Instance Method Summary collapse
- #auth_headers ⇒ Object
- #auth_token ⇒ Object
- #get_auth_token ⇒ Object
- #handle_response(response) ⇒ Object
- #headers(request_headers = {}, with_token = false) ⇒ Object
-
#initialize(config) ⇒ Api
constructor
A new instance of Api.
- #recursive_compact(hash_or_array) ⇒ Object
- #send_request(url, options = {}, verb = :get, with_token = true) ⇒ Object
- #set_tokens(tokens) ⇒ Object
- #token_expired ⇒ Object
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_correlator ⇒ Object
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_id ⇒ Object
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_headers ⇒ Object
74 75 76 77 78 |
# File 'lib/cpaas-sdk/api.rb', line 74 def auth_headers { 'Authorization' => "Bearer #{auth_token}" } end |
#auth_token ⇒ Object
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_token ⇒ Object
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 = { 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 [:body].merge!(credentials) response = send_request('/cpaas/auth/v1/token', , :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, = {}, verb = :get, with_token = true) body = recursive_compact([:body]) if [:body] [:headers] = headers([:headers] || {}, with_token) [:body] = body.to_json if [:headers]['Content-Type'] == 'application/json' [:query] = [:query] if .has_key? :query case verb when :get response = self.class.get(url, ) when :post response = self.class.post(url, ) when :put response = self.class.put(url, ) when :delete response = self.class.delete(url, ) 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_expired ⇒ Object
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 |