Skip to content

Commit

Permalink
Revert "feat: raise HTTP::TimeoutError for CircuitBreaker (#13)"
Browse files Browse the repository at this point in the history
This reverts commit f88bec2.
  • Loading branch information
uncoder committed Oct 8, 2024
1 parent 38af104 commit e3375e7
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 647 deletions.
1 change: 1 addition & 0 deletions lib/factiva.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Dir[File.join(__dir__, "factiva", "*.rb")].each { |file| require file }

module Factiva

REQUEST_API_ACCOUNT = "request_api_account"
MONITORING_API_ACCOUNT = "monitoring_api_account"

Expand Down
24 changes: 20 additions & 4 deletions lib/factiva/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

module Factiva
class Authentication
attr_reader :config, :client
attr_reader :config

def initialize(config)
@config = config
@client = Client.new(config)
end

def token
Expand All @@ -26,17 +25,34 @@ def authz
end

def set_authn
client.make_authentication_request(:post, config.auth_url, set_authn_params)
make_request(set_authn_params)
end

def set_authz(refresh: false)
params = refresh ? refresh_authz_params : set_authz_params

new_authz = client.make_authentication_request(:post, config.auth_url, params)
new_authz = make_request(params)
new_authz["expiration_timestamp"] = time_now + new_authz["expires_in"]
new_authz
end

def make_request(params)
response = HTTP
.timeout(config.timeout)
.post(
config.auth_url,
params
)

response_body = JSON.parse(response.body.to_s)

if !response.status.success?
raise RequestError.new({ code: response.code, error: response_body["error"] }.to_s)
end

response_body
end

def expired?(timestamp)
timestamp < time_now
end
Expand Down
62 changes: 0 additions & 62 deletions lib/factiva/client.rb

This file was deleted.

4 changes: 1 addition & 3 deletions lib/factiva/errors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module Factiva
class Error < StandardError; end
class RequestError < Error; end
class TimeoutError < Error; end
class RequestError < StandardError; end
end
122 changes: 90 additions & 32 deletions lib/factiva/monitoring.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
require "http"
require "json"
require "dry/monads"

module Factiva
class Monitoring
include Dry::Monads[:result]

private_class_method :new
attr_reader :auth

COUNTRY_IDS = {
"ES" => "SPAIN",
"FR" => "FRA",
"IT" => "ITALY",
"PT" => "PORL",
}.freeze

private_class_method :new
attr_reader :client

def self.create_case(...)
instance.create_case(...)
def self.create_case(**args)
instance.create_case(**args)
end

def self.create_association(...)
instance.create_association(...)
def self.create_association(**args)
instance.create_association(**args)
end

def self.add_association_to_case(...)
instance.add_association_to_case(...)
def self.add_association_to_case(**args)
instance.add_association_to_case(**args)
end

def self.get_matches(...)
instance.get_matches(...)
def self.get_matches(**args)
instance.get_matches(**args)
end

def self.log_decision(...)
instance.log_decision(...)
def self.log_decision(**args)
instance.log_decision(**args)
end

def self.reset_auth
instance.client.set_auth!
instance.set_auth
end

def self.stub!(create_case: {},
Expand Down Expand Up @@ -71,47 +74,50 @@ def initialize(stubbed_create_case,
stubbed_get_matches,
stubbed_log_decision
)
@stubbed_create_case = stubbed_create_case
@stubbed_create_association = stubbed_create_association
@stubbed_add_association_to_case = stubbed_add_association_to_case
@stubbed_create_case = stubbed_create_case
@stubbed_create_association = stubbed_create_association
@stubbed_add_association_to_case = stubbed_add_association_to_case
@stubbed_get_matches = stubbed_get_matches
@stubbed_log_decision = stubbed_log_decision
end

def create_case(...)
def create_case(**args)
stubbed_create_case
end

def create_association(...)
def create_association(**args)
stubbed_create_association
end

def add_association_to_case(...)
def add_association_to_case(**args)
stubbed_add_association_to_case
end

def get_matches(...)
def get_matches(**args)
stubbed_get_matches
end

def log_decision(...)
def log_decision(**args)
stubbed_log_decision
end
end

def initialize
@client = Client.new(config)
set_auth
end

def create_case(case_body_payload = nil)
params = { json: case_body_payload || create_case_body }

client.make_authorized_request(:post, create_case_url, params)
# If the request fails auth is reset and the request retried
post(create_case_url, params)
.or { set_auth; post(create_case_url, params) }
.value_or { |error| raise RequestError.new(error) }
end

def create_association(first_name:, last_name:, birth_year:, external_id:, nin:, country_code:)
params = { json: association_body(
first_name,
first_name,
last_name,
birth_year,
external_id,
Expand All @@ -120,37 +126,89 @@ def create_association(first_name:, last_name:, birth_year:, external_id:, nin:,
)
}

client.make_authorized_request(:post, association_url, params)
# If the request fails auth is reset and the request retried
post(association_url, params)
.or { set_auth; post(association_url, params) }
.value_or { |error| raise RequestError.new(error) }
end

def add_association_to_case(case_id:, association_id:)
params = { json: case_association_body(
association_id,
association_id,
)
}

client.make_authorized_request(:post, case_association_url(case_id), params)
# If the request fails auth is reset and the request retried
post(case_association_url(case_id), params)
.or { set_auth; post(case_association_url(case_id), params) }
.value_or { |error| raise RequestError.new(error) }
end

def get_matches(case_id:)
client.make_authorized_request(:get, matches_url(case_id))
# If the request fails auth is reset and the request retried
get(matches_url(case_id))
.or { set_auth; get(matches_url(case_id)) }
.value_or { |error| raise RequestError.new(error) }
end

def log_decision(case_id:, match_id:, comment:, state:, risk_rating:)
params = { json: log_decision_body(
comment, state, risk_rating,
comment, state, risk_rating,
)
}

client.make_authorized_request(:patch, log_decision_url(case_id, match_id), params)
# If the request fails auth is reset and the request retried
patch(log_decision_url(case_id, match_id), params)
.or { set_auth; patch(log_decision_url(case_id, match_id), params) }
.value_or { |error| raise RequestError.new(error) }
end

private

def self.instance
@instance ||= new
end

def set_auth
@auth = Authentication.new(config)
end

def post(url, params)
make_request(:post, url, params)
end

def patch(url, params)
make_request(:patch, url, params)
end

def get(url)
make_request(:get, url)
end

def make_request(http_method, url, params = nil)
http_params = [http_method, url, params].compact

begin
response = HTTP
.headers(:accept => "application/json")
.headers("Content-Type" => "application/json")
.timeout(config.timeout)
.auth("Bearer #{auth.token}")
.send(*http_params)

response_body = JSON.parse(response.body.to_s)

if response.status.success?
Success(response_body)
else
Failure({ code: response.code, error: response_body["error"] }.to_s)
end
rescue SocketError, HTTP::Error => error
Failure("Failed to connect to Factiva: #{error.message}")
rescue JSON::ParserError => error
Failure(error.message)
end
end

def make_url(suffix)
url = config.base_url
url = url.delete_suffix("/") if url.end_with?("/")
Expand Down
Loading

0 comments on commit e3375e7

Please sign in to comment.