Class: WebFunction::Request

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

Overview

A request allows you to invoke a Web Function endpoint via an HTTP client.

Examples:

request = WebFunction::Request.new("https://api.example.com/endpoint")
request.execute # => { "a" => 1 }

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_clientObject

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

Examples:

WebFunction::Endpoint.http_client = ->(url, headers, args) {
  http_response = MyHTTP.post(url, headers: headers, body: JSON.generate(args))
  [http_response.status, http_response.body]
}


43
44
45
# File 'lib/web_function/request.rb', line 43

def http_client
  @http_client
end

Instance Attribute Details

#argsHash (readonly)

The arguments to send to the request.

Returns:

  • (Hash)

    The arguments to send to the request



86
87
88
# File 'lib/web_function/request.rb', line 86

def args
  @args
end

#bearer_authString (readonly)

The bearer authentication token.

Returns:

  • (String)

    The bearer authentication token



74
75
76
# File 'lib/web_function/request.rb', line 74

def bearer_auth
  @bearer_auth
end

#urlString (readonly)

The URL of the request.

Returns:

  • (String)

    The URL of the request



68
69
70
# File 'lib/web_function/request.rb', line 68

def url
  @url
end

#versionString (readonly)

The API version to use.

Returns:

  • (String)

    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.

Parameters:

  • url (String)

    The URL of the request

  • bearer_auth (String) (defaults to: nil)

    The bearer authentication token

  • version (String) (defaults to: nil)

    The API version to use

  • args (Hash) (defaults to: {})

    The arguments to send to the request

Returns:

  • (Object)

    The response returned by the 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_stepHash

Returns the request as a pipeline step.

Returns:

  • (Hash)

    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

#executeObject

Executes the request.

Returns:

  • (Object)

    The response returned by the request

Raises:



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.message,
      details: {
        status_code: status,
        raw_body: body,
        original_exception: e,
      },
    )
  end

  if status == 400
    code = "WFN_BAD_REQUEST_ERROR"
    message = "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]
      message = result[1]
      details = result[2]
    end

    raise WebFunction::BadRequestError.new(message, code: code, details: details)
  end

  result
end

#headersHash

The headers to send to the request.

Returns:

  • (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