-
Notifications
You must be signed in to change notification settings - Fork 115
/
request.rb
206 lines (173 loc) · 5.2 KB
/
request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# frozen_string_literal: true
module DocAuth
module LexisNexis
class Request
attr_reader :config, :user_uuid, :uuid_prefix
def initialize(config:, user_uuid: nil, uuid_prefix: nil)
@config = config
@user_uuid = user_uuid
@uuid_prefix = uuid_prefix
end
def fetch
# return DocAuth::Respose with DocAuth:Error if workflow invalid
http_response = send_http_request
return handle_invalid_response(http_response) unless http_response.success?
handle_http_response(http_response)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError => e
handle_connection_error(exception: e)
end
def metric_name
raise NotImplementedError
end
private
def send_http_request
case method.downcase.to_sym
when :post
send_http_post_request
when :get
send_http_get_request
end
end
def handle_http_response(_response)
raise NotImplementedError
end
def handle_invalid_response(http_response)
message = [
self.class.name,
'Unexpected HTTP response',
http_response.status,
].join(' ')
exception = DocAuth::RequestError.new(message, http_response.status)
response_body = begin
http_response.body.present? ? JSON.parse(http_response.body) : {}
rescue JSON::JSONError
{}
end
handle_connection_error(
exception: exception,
status_code: response_body.dig('status', 'code'),
status_message: response_body.dig('status', 'message'),
)
end
def handle_connection_error(exception:, status_code: nil, status_message: nil)
NewRelic::Agent.notice_error(exception)
DocAuth::Response.new(
success: false,
errors: { network: true },
exception: exception,
extra: {
vendor: 'TrueID',
selfie_live: false,
selfie_quality_good: false,
vendor_status_code: status_code,
vendor_status_message: status_message,
reference: @reference,
}.compact,
)
end
def send_http_get_request
faraday_connection.get do |req|
req.options.context = { service_name: metric_name }
end
end
def send_http_post_request
faraday_connection.post do |req|
req.options.context = { service_name: metric_name }
req.body = body
end
end
def faraday_connection
retry_options = {
max: 2,
interval: 0.05,
interval_randomness: 0.5,
backoff_factor: 2,
retry_statuses: [404, 500],
retry_block: lambda do |env:, options:, retry_count:, exception:, will_retry_in:|
NewRelic::Agent.notice_error(exception, custom_params: { retry: retry_count })
end,
}
Faraday.new(url: url.to_s, headers: request_headers) do |conn|
conn.request :retry, retry_options
conn.request :instrumentation, name: 'request_metric.faraday'
conn.request :authorization, :basic, username, password unless hmac_auth_enabled?
conn.adapter :net_http
conn.options.timeout = timeout
conn.options.read_timeout = timeout
conn.options.open_timeout = timeout
conn.options.write_timeout = timeout
end
end
def path
"/restws/identity/v3/accounts/#{account_id}/workflows/#{workflow}/conversations"
end
def method
:get
end
def url
URI.join(config.base_url, path)
end
def request_headers
headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
}
headers['Authorization'] = hmac_authorization if hmac_auth_enabled?
headers
end
def hmac_auth_enabled?
IdentityConfig.store.lexisnexis_hmac_auth_enabled
end
# Example HMAC auth header from RDP_REST_V3_DecisioningGuide_March22.pdf, page 21
def hmac_authorization
Proofing::LexisNexis::RequestSigner.new(
config: config,
message_body: body,
path: path,
).hmac_authorization
end
def settings
@reference = uuid
{
Type: 'Initiate',
Settings: {
Mode: request_mode,
Locale: config.locale,
Venue: 'online',
Reference: @reference,
},
}
end
def uuid
return SecureRandom.uuid unless user_uuid
uuid = user_uuid
if uuid_prefix.present?
"#{uuid_prefix}:#{uuid}"
else
uuid
end
end
def username
raise NotImplementedError
end
def password
raise NotImplementedError
end
def account_id
config.account_id
end
def workflow
raise NotImplementedError
end
def body
raise NotImplementedError
end
def request_mode
config.request_mode
end
def timeout
IdentityConfig.store.lexisnexis_trueid_timeout
end
end
end
end