Class: HttpClientGenerator::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client_generator/request.rb

Constant Summary collapse

CONTENT_TYPES =
i[json text].freeze
DEFAULT_HEADERS_BY_CONTENT_TYPE =
{
  json: { accept: 'application/json', content_type: 'application/json' },
  text: { content_type: 'text/plain' },
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base:, name:, verb:, content_type:, url:, body:, rest_args:) ⇒ Request

Returns a new instance of Request.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/http_client_generator/request.rb', line 20

def initialize(base:, name:, verb:, content_type:, url:, body:, rest_args:)
  @base = base
  @name = name
  @verb = verb
  @content_type = content_type
  @url = url
  @rest_args = rest_args
  @body = body
  @headers = {}
  @extra = {}
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def base
  @base
end

#bodyObject

Returns the value of attribute body.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def body
  @body
end

#content_typeObject

Returns the value of attribute content_type.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def content_type
  @content_type
end

#extraObject

Returns the value of attribute extra.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def extra
  @extra
end

#headersObject

Returns the value of attribute headers.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def headers
  @headers
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def name
  @name
end

#response_bodyObject

Returns the value of attribute response_body.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def response_body
  @response_body
end

#rest_argsObject

Returns the value of attribute rest_args.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def rest_args
  @rest_args
end

#urlObject

Returns the value of attribute url.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def url
  @url
end

#verbObject

Returns the value of attribute verb.



18
19
20
# File 'lib/http_client_generator/request.rb', line 18

def verb
  @verb
end

Instance Method Details

#current_headersObject



32
33
34
# File 'lib/http_client_generator/request.rb', line 32

def current_headers
  default_headers.merge(headers)
end

#raise_error(e) ⇒ Object

Raises:



44
45
46
47
# File 'lib/http_client_generator/request.rb', line 44

def raise_error(e)
  Sentry.capture_exception(e, extra: { request_id: extra[:request_id] }) if Object.const_defined?(:Sentry)
  raise base::RequestError, e.message, e.backtrace, cause: nil
end

#raise_message(message) ⇒ Object

Raises:



49
50
51
52
# File 'lib/http_client_generator/request.rb', line 49

def raise_message(message)
  Sentry.capture_message(message, extra: { request_id: extra[:request_id] }) if Object.const_defined?(:Sentry)
  raise base::RequestError, message
end

#raw_bodyObject



36
37
38
39
40
41
42
# File 'lib/http_client_generator/request.rb', line 36

def raw_body
  if json?
    body.to_json
  else
    body
  end
end