fetch token introspection
Co-authored-by: François VANTOMME <akarzim@gmail.com>
This commit is contained in:
parent
620a5374e8
commit
87cb16093f
5 changed files with 182 additions and 0 deletions
34
app/lib/api_particulier/api.rb
Normal file
34
app/lib/api_particulier/api.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
class APIParticulier::API
|
||||
include APIParticulier::Error
|
||||
|
||||
INTROSPECT_RESOURCE_NAME = "introspect"
|
||||
|
||||
TIMEOUT = 20
|
||||
|
||||
def initialize(token)
|
||||
@token = token
|
||||
end
|
||||
|
||||
def scopes
|
||||
get(INTROSPECT_RESOURCE_NAME)[:scopes]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get(resource_name, params = {})
|
||||
url = [API_PARTICULIER_URL, resource_name].join("/")
|
||||
|
||||
response = Typhoeus.get(url,
|
||||
headers: { accept: "application/json", "X-API-Key": @token },
|
||||
params: params,
|
||||
timeout: TIMEOUT)
|
||||
|
||||
if response.success?
|
||||
JSON.parse(response.body, symbolize_names: true)
|
||||
elsif response.code == 401
|
||||
raise Unauthorized.new(response)
|
||||
else
|
||||
raise RequestFailed.new(response)
|
||||
end
|
||||
end
|
||||
end
|
32
app/lib/api_particulier/error.rb
Normal file
32
app/lib/api_particulier/error.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module APIParticulier
|
||||
module Error
|
||||
class HttpError < ::StandardError
|
||||
def initialize(response)
|
||||
connect_time = response.connect_time
|
||||
curl_message = response.return_message
|
||||
http_error_code = response.code
|
||||
datetime = response.headers.fetch('Date', DateTime.current.inspect)
|
||||
total_time = response.total_time
|
||||
|
||||
uri = URI.parse(response.effective_url)
|
||||
url = "#{uri.host}#{uri.path}"
|
||||
|
||||
msg = <<~TEXT
|
||||
url: #{url}
|
||||
HTTP error code: #{http_error_code}
|
||||
#{response.body}
|
||||
curl message: #{curl_message}
|
||||
total time: #{total_time}
|
||||
connect time: #{connect_time}
|
||||
datetime: #{datetime}
|
||||
TEXT
|
||||
|
||||
super(msg)
|
||||
end
|
||||
end
|
||||
|
||||
class RequestFailed < HttpError; end
|
||||
|
||||
class Unauthorized < HttpError; end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue