Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expire token using expired_at #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
timeout-minutes: 20
strategy:
matrix:
ruby: [ '2.6', '2.7', '3.0', 'jruby-9.3.4.0' ]
ruby: [ '3.0', '3.2', 'jruby-9.4.3.0' ]
steps:
- name: Checkout icd-api
uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@

# rspec failure tracking
.rspec_status

.byebug_history

Gemfile.lock
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.6
TargetRubyVersion: 3
NewCops: enable


Metrics/BlockLength:
Exclude:
Expand Down
21 changes: 21 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-08-10 15:08:36 UTC using RuboCop version 0.93.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, Regex, IgnoreExecutableScripts, AllowedAcronyms.
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
Naming/FileName:
Exclude:
- 'lib/icd-api.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
# SupportedStylesForLeadingUnderscores: disallowed, required, optional
Naming/MemoizedInstanceVariableName:
Exclude:
- 'lib/icd/api/authorize_token.rb'
9 changes: 6 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ gemspec

gem 'rake', '~> 13.0'

gem 'rspec', '~> 3.0'

gem 'rubocop', '~> 0.80'
group :development, :test do
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 0.80'
gem 'vcr'
gem 'webmock'
end
85 changes: 0 additions & 85 deletions Gemfile.lock

This file was deleted.

12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
Configure your credentials using a block

```ruby
Icd::Api::AuthorizeToken.configure do |conf|
conf.client_id = 'your_client_id'
conf.client_secret = 'your_client_secret'
end
```
Or declare the env variables:
- ICD_API_CLIENT_ID
- ICD_API_CLIENT_SECRET

## Development

Expand Down
9 changes: 4 additions & 5 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# frozen_string_literal: true

#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "icd/api"
require 'bundler/setup'
require 'icd/api'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -12,5 +11,5 @@ require "icd/api"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start(__FILE__)
7 changes: 4 additions & 3 deletions icd-api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
spec.description = 'Desc'
spec.homepage = 'https://www.github.com'
spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
spec.required_ruby_version = Gem::Requirement.new('>= 3.0.0')

spec.metadata['allowed_push_host'] = 'http://mygemserver.com'

Expand All @@ -29,8 +29,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency('faraday', '>= 1.7')
spec.add_dependency('faraday_middleware', '>= 1.2.0')
spec.add_dependency('dry-configurable', '< 2')
spec.add_dependency('faraday', '< 3')
spec.add_dependency('zeitwerk', '< 3')

# gem.add_development_dependency "bundler"
spec.add_development_dependency 'rspec'
Expand Down
3 changes: 3 additions & 0 deletions lib/icd-api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require 'icd/api'
13 changes: 9 additions & 4 deletions lib/icd/api.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# frozen_string_literal: true

require 'icd/api/version'

require 'icd/api/client'
require 'zeitwerk'
require 'dry-configurable'
require 'faraday'

module Icd
module Api
class Error < StandardError; end
# Your code goes here...
end
end
root = File.expand_path('..', __dir__)

loader = Zeitwerk::Loader.new
loader.push_dir(root)
loader.ignore("#{root}/icd-api.rb", "#{root}/icd/api/version.rb")
loader.setup
45 changes: 45 additions & 0 deletions lib/icd/api/authorize_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Icd
module Api
class AuthorizeToken
extend Dry::Configurable
TOKEN_ENDPOINT = 'https://icdaccessmanagement.who.int/connect/token'
SCOPE = 'icdapi_access'
GRANT_TYPE = 'client_credentials'

setting :client_id, default: ENV['ICD_API_CLIENT_ID']
setting :client_secret, default: ENV['ICD_API_CLIENT_SECRET']

class << self
attr_reader :access_token

def call
return @access_token if valid?

response = http_adapter.post TOKEN_ENDPOINT,
{
scope: SCOPE,
grant_type: GRANT_TYPE,
client_id: config.client_id,
client_secret: config.client_secret
}

@access_token = AccessToken.new(::JSON.parse(response.body))
end

private

def valid?
return false if @access_token.nil?

@access_token.expires_at.to_i > Time.now.to_i
end

def http_adapter
Faraday.new
end
end
end
end
end
34 changes: 0 additions & 34 deletions lib/icd/api/authorizer.rb

This file was deleted.

27 changes: 11 additions & 16 deletions lib/icd/api/client.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
# frozen_string_literal: true

require 'icd/api/version'
require 'icd/api/options'
require 'icd/api/connection'
require 'icd/api/entity'

require 'faraday'
require 'json'

module Icd
module Api
class Client
def initialize(client_id:, client_secret:, **_options)
@client_id = client_id
@client_secret = client_secret
@options = Options.new
end

def search(term)
response = connection.get('search',
{
Expand All @@ -29,11 +18,17 @@ def search(term)
end
end

def fetch_by_code(code)
response = connection.get("codeinfo/#{code}", { flexiblemode: 'false' })
def fetch_stem_id_by_code(code)
connection
.get("codeinfo/#{code}", { flexiblemode: 'false' })
.body['stemId']
end

stem_id = response.body['stemId'].split('/').last
connection.get(stem_id.to_s, {})
def fetch_info_by_stem_id(stem_id)
entity_id = stem_id.split('/').last
entity_id = stem_id.split('/')[-2] if entity_id == 'unknown'
response = connection.get(entity_id, {})
response.body
end

private
Expand All @@ -48,7 +43,7 @@ def api_default_params
end

def connection
Connection.new(@client_id, @client_secret, @options)
Connection.new
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/icd/api/connection.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# frozen_string_literal: true

require 'icd/api/authorizer'

module Icd
module Api
class Connection
attr_reader :options

def initialize(client_id, client_secret, options)
def initialize(options: Options.new)
@options = options
@client_id = client_id
@client_secret = client_secret
end

def get(endpoint, params)
Expand All @@ -37,7 +33,7 @@ def headers
end

def access_token
Authorizer.new(client_id: @client_id, client_secret: @client_secret).retrieve_access_token
AuthorizeToken.call
end
end
end
Expand Down
Loading