Class: WebFunction::Request
- Inherits:
-
Object
- Object
- WebFunction::Request
- Defined in:
- lib/web_function/request.rb
Overview
A request allows you to invoke a Web Function endpoint via an HTTP client.
Class Attribute Summary collapse
-
.http_client ⇒ Object
The HTTP client used to execute the request.
Instance Attribute Summary collapse
-
#args ⇒ Hash
readonly
The arguments to send to the request.
-
#bearer_auth ⇒ String
readonly
The bearer authentication token.
-
#url ⇒ String
readonly
The URL of the request.
-
#version ⇒ String
readonly
The API version to use.
Class Method Summary collapse
-
.execute(url, bearer_auth: nil, version: nil, args: {}) ⇒ Object
Executes a request.
Instance Method Summary collapse
-
#as_pipeline_step ⇒ Hash
Returns the request as a pipeline step.
-
#execute ⇒ Object
Executes the request.
-
#headers ⇒ Hash
The headers to send to the request.
-
#initialize(url, bearer_auth: nil, version: nil, args: {}) ⇒ Request
constructor
A new instance of Request.
Constructor Details
#initialize(url, bearer_auth: nil, version: nil, args: {}) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 16 |
# File 'lib/web_function/request.rb', line 11 def initialize(url, bearer_auth: nil, version: nil, args: {}) @url = url @bearer_auth = bearer_auth @version = version @args = args || {} end |
Class Attribute Details
.http_client ⇒ Object
The HTTP client used to execute the request.
To provide a custom HTTP client instead of the default (which uses Excon), set this to any object responding to #call. For example, a Proc or a lambda.
The contract is:
client.call(url, headers, body)
-
url: [String] The full URL to post to (not just the hostname or path).
-
headers: HTTP headers, e.g. { “Content-Type” => “application/json” }
-
body: [String] The JSON body to post.
The client must return a two-element Array: [status, body]:
-
status: [Integer] HTTP status code (e.g. 200, 400, 500)
-
body: [String] Raw response body as a string
43 44 45 |
# File 'lib/web_function/request.rb', line 43 def http_client @http_client end |
Instance Attribute Details
#args ⇒ Hash (readonly)
The arguments to send to the request.
86 87 88 |
# File 'lib/web_function/request.rb', line 86 def args @args end |
#bearer_auth ⇒ String (readonly)
The bearer authentication token.
74 75 76 |
# File 'lib/web_function/request.rb', line 74 def bearer_auth @bearer_auth end |
#url ⇒ String (readonly)
The URL of the request.
68 69 70 |
# File 'lib/web_function/request.rb', line 68 def url @url end |
#version ⇒ String (readonly)
The API version to use.
80 81 82 |
# File 'lib/web_function/request.rb', line 80 def version @version end |
Class Method Details
.execute(url, bearer_auth: nil, version: nil, args: {}) ⇒ Object
Executes a request.
53 54 55 56 |
# File 'lib/web_function/request.rb', line 53 def execute(url, bearer_auth: nil, version: nil, args: {}) request = new(url, bearer_auth: bearer_auth, version: version, args: args) request.execute end |
Instance Method Details
#as_pipeline_step ⇒ Hash
Returns the request as a pipeline step.
114 115 116 117 118 119 120 |
# File 'lib/web_function/request.rb', line 114 def as_pipeline_step { url: @url, headers: headers, body: @args, } end |
#execute ⇒ Object
Executes the request.
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 |
# File 'lib/web_function/request.rb', line 130 def execute status, body = self.class.http_client.call(@url, headers, JSON.generate(@args)) unless [200, 400].include?(status) raise WebFunction::UnexpectedStatusCodeError.new("Unexpected status code (#{status})", details: { status_code: status, raw_body: body, }, ) end begin result = JSON.parse(body) rescue JSON::ParserError => e raise WebFunction::JsonParseError.new(e., details: { status_code: status, raw_body: body, original_exception: e, }, ) end if status == 400 code = "WFN_BAD_REQUEST_ERROR" = "Bad request" details = { body: result } if result.is_a?(Array) && result.length == 3 && result[0].is_a?(String) && result[1].is_a?(String) code = result[0] = result[1] details = result[2] end raise WebFunction::BadRequestError.new(, code: code, details: details) end result end |
#headers ⇒ Hash
The headers to send to the request.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/web_function/request.rb', line 92 def headers headers = { "Content-Type": "application/json", "Accept": "application/json", "User-Agent": "webfunction/#{WebFunction::VERSION}", } if @bearer_auth headers["Authorization"] = "Bearer #{@bearer_auth}" end if @version headers["Api-Version"] = @version end headers end |