2
2
3
3
module Knock
4
4
# A Net::HTTP based API client for interacting with the Knock API
5
+ # rubocop:disable Metrics/ModuleLength
5
6
module Client
6
7
include Kernel
7
8
8
9
def client
9
- return @client if defined? ( @client )
10
-
11
- @client = Net :: HTTP . new ( Knock :: API_HOSTNAME , 443 )
12
- @client . use_ssl = true
13
-
14
- @client
10
+ Net :: HTTP . new ( Knock . config . api_hostname , 443 ) . tap do | http_client |
11
+ http_client . use_ssl = true
12
+ http_client . open_timeout = Knock . config . timeout
13
+ http_client . read_timeout = Knock . config . timeout
14
+ http_client . write_timeout = Knock . config . timeout if RUBY_VERSION >= '2.6.0'
15
+ end
15
16
end
16
17
17
18
def execute_request ( request :)
18
- response = client . request ( request )
19
+ begin
20
+ response = client . request ( request )
21
+ rescue Net ::OpenTimeout , Net ::ReadTimeout , Net ::WriteTimeout
22
+ raise TimeoutError . new (
23
+ message : 'API Timeout Error'
24
+ )
25
+ end
19
26
20
27
http_status = response . code . to_i
21
28
handle_error_response ( response : response ) if http_status >= 400
@@ -32,15 +39,15 @@ def get_request(path:, auth: false, params: {}, access_token: nil)
32
39
'Content-Type' => 'application/json'
33
40
)
34
41
35
- request [ 'Authorization' ] = "Bearer #{ access_token || Knock . key! } " if auth
42
+ request [ 'Authorization' ] = "Bearer #{ access_token || Knock . config . key! } " if auth
36
43
request [ 'User-Agent' ] = user_agent
37
44
request
38
45
end
39
46
40
47
def post_request ( path :, auth : false , idempotency_key : nil , body : nil )
41
48
request = Net ::HTTP ::Post . new ( path , 'Content-Type' => 'application/json' )
42
49
request . body = body . to_json if body
43
- request [ 'Authorization' ] = "Bearer #{ Knock . key! } " if auth
50
+ request [ 'Authorization' ] = "Bearer #{ Knock . config . key! } " if auth
44
51
request [ 'User-Agent' ] = user_agent
45
52
request [ 'Idempotency-Key' ] = idempotency_key if idempotency_key
46
53
request
@@ -56,15 +63,15 @@ def delete_request(path:, auth: false, params: {}, body: nil)
56
63
)
57
64
58
65
request . body = body . to_json if body
59
- request [ 'Authorization' ] = "Bearer #{ Knock . key! } " if auth
66
+ request [ 'Authorization' ] = "Bearer #{ Knock . config . key! } " if auth
60
67
request [ 'User-Agent' ] = user_agent
61
68
request
62
69
end
63
70
64
71
def put_request ( path :, auth : false , idempotency_key : nil , body : nil )
65
72
request = Net ::HTTP ::Put . new ( path , 'Content-Type' => 'application/json' )
66
73
request . body = body . to_json if body
67
- request [ 'Authorization' ] = "Bearer #{ Knock . key! } " if auth
74
+ request [ 'Authorization' ] = "Bearer #{ Knock . config . key! } " if auth
68
75
request [ 'User-Agent' ] = user_agent
69
76
request [ 'Idempotency-Key' ] = idempotency_key if idempotency_key
70
77
request
@@ -122,4 +129,5 @@ def extract_error(errors)
122
129
end . join ( '; ' )
123
130
end
124
131
end
132
+ # rubocop:enable Metrics/ModuleLength
125
133
end
0 commit comments