Class: Infrawrench::Transport
- Inherits:
-
Object
- Object
- Infrawrench::Transport
- Defined in:
- lib/infrawrench/transport.rb,
sig/infrawrench/sdk.rbs
Overview
Request plumbing shared by every namespace.
The generated namespace classes each hold one of these; reach for APIV1Client instead unless you need the resolved #base_url.
Instance Attribute Summary collapse
-
#base_url ⇒ String
readonly
Normalized base URL, without a trailing slash.
Instance Method Summary collapse
-
#initialize(base_url: nil, api_key: nil, org_id: nil, headers: {}, timeout: nil, open_timeout: nil, http_handler: nil) ⇒ Transport
constructor
A new instance of Transport.
-
#request(http_method:, path:, path_params: nil, query: nil, body: nil, form: nil, form_files: [], accept: :json, request_options: nil) ⇒ Object?
Perform one call.
Constructor Details
#initialize(base_url: nil, api_key: nil, org_id: nil, headers: {}, timeout: nil, open_timeout: nil, http_handler: nil) ⇒ Transport
Returns a new instance of Transport.
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/infrawrench/transport.rb', line 130 def initialize(base_url: nil, api_key: nil, org_id: nil, headers: {}, timeout: nil, open_timeout: nil, http_handler: nil) @base_url = (base_url || DEFAULT_BASE_URL).sub(%r{/+\z}, "") @api_key = api_key @defaults = SCOPE_PARAM.nil? ? {} : { SCOPE_PARAM => org_id } @headers = headers.transform_keys(&:to_s) @timeout = timeout @open_timeout = open_timeout @http_handler = http_handler end |
Instance Attribute Details
#base_url ⇒ String (readonly)
Returns Normalized base URL, without a trailing slash.
118 119 120 |
# File 'lib/infrawrench/transport.rb', line 118 def base_url @base_url end |
Instance Method Details
#request(http_method:, path:, path_params: nil, query: nil, body: nil, form: nil, form_files: [], accept: :json, request_options: nil) ⇒ Object?
Perform one call. The generated methods are thin wrappers around this.
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 |
# File 'lib/infrawrench/transport.rb', line 155 def request(http_method:, path:, path_params: nil, query: nil, body: nil, form: nil, form_files: [], accept: :json, request_options: nil) = || {} url = "#{@base_url}#{resolve_path(http_method, path, path_params)}#{serialize_query(query)}" uri = URI.parse(url) request_class = METHOD_CLASSES.fetch(http_method) req = request_class.new(uri) req["accept"] = accept == :binary ? "application/octet-stream" : "application/json" @headers.each { |name, value| req[name] = value } ([:headers] || {}).each { |name, value| req[name.to_s] = value } req["authorization"] = "Bearer #{@api_key}" unless @api_key.nil? || @api_key.empty? if form boundary = SecureRandom.hex(16) req["content-type"] = "multipart/form-data; boundary=#{boundary}" req.body = encode_multipart(form, form_files, boundary) elsif !body.nil? req["content-type"] = "application/json" req.body = JSON.generate(body) end response = perform(uri, req, ) status = response.code.to_i raise build_error(response, status, http_method, url) unless (200..299).cover?(status) decode(response, status, accept) end |