66module Evervault
77 module Http
88 class Request
9- def initialize ( api_key :, base_url :, cage_run_url :, timeout :)
10- @api_key = api_key
9+ def initialize ( timeout :, api_key :)
1110 @timeout = timeout
12- @base_url = base_url
13- @cage_run_url = cage_run_url
14- end
15-
16- def get ( path , params = nil )
17- execute ( :get , build_url ( path ) , params )
18- end
19-
20- def put ( path , params )
21- execute ( :put , build_url ( path ) , params )
22- end
23-
24- def delete ( path , params )
25- execute ( :delete , build_url ( path ) , params )
26- end
27-
28- def post ( path , params , options : { } , cage_run : false )
29- execute ( :post , build_url ( path , cage_run ) , params , build_cage_run_headers ( options , cage_run ) )
30- end
31-
32- private def build_url ( path , cage_run = false )
33- return "#{ @base_url } #{ path } " unless cage_run
34- "#{ @cage_run_url } #{ path } "
11+ @api_key = api_key
3512 end
3613
37- def execute ( method , url , params , optional_headers = { } )
14+ def execute ( method , url , params , optional_headers = { } , is_ca = false )
3815 resp = Faraday . send ( method , url ) do |req |
3916 req . body = params . nil? || params . empty? ? nil : params . to_json
4017 req . headers = build_headers ( optional_headers )
18+ req . options . timeout = @timeout
4119 end
42- return JSON . parse ( resp . body ) if resp . status >= 200 && resp . status <= 300
43- Evervault ::Errors ::ErrorMap . raise_errors_on_failure ( resp . status , resp . body )
20+ if resp . status >= 200 && resp . status <= 300
21+ if is_ca
22+ return resp . body
23+ end
24+ return JSON . parse ( resp . body )
25+ end
26+ Evervault ::Errors ::ErrorMap . raise_errors_on_failure ( resp . status , resp . body , resp . headers )
4427 end
4528
4629 private def build_headers ( optional_headers )
@@ -49,27 +32,9 @@ def execute(method, url, params, optional_headers = {})
4932 "Accept" : "application/json" ,
5033 "Content-Type" : "application/json" ,
5134 "User-Agent" : "evervault-ruby/#{ VERSION } " ,
52- "Api-Key" : @api_key
35+ "Api-Key" : @api_key ,
5336 } )
5437 end
55-
56- private def build_cage_run_headers ( options , cage_run = false )
57- optional_headers = { }
58- return optional_headers unless cage_run
59- if options . key? ( :async )
60- if options [ :async ]
61- optional_headers [ "x-async" ] = "true"
62- end
63- options . delete ( :async )
64- end
65- if options . key? ( :version )
66- if options [ :version ] . is_a? Integer
67- optional_headers [ "x-version-id" ] = options [ :version ] . to_s
68- end
69- options . delete ( :version )
70- end
71- optional_headers . merge ( options )
72- end
7338 end
7439 end
7540end
0 commit comments