Sha256: 29f3dc74f92ed1d64e9b606916b7efe987bb606e094e624f85d37eea5ca2b1b2

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.

module Async
	module REST
		module Wrapper
			class Generic
				def call(resource, verb = "GET", payload = nil, &block)
					request = ::Protocol::HTTP::Request[verb, nil]
					self.prepare_request(request, payload)
					
					response = resource.call(request)
					
					# If we exit this block because of an exception, we close the response. This ensures we don't have any dangling connections.
					begin
						self.process_response(request, response)
						
						yield response
					rescue
						response.close
						
						raise
					end
				end
				
				# @param payload [Object] a request payload to send.
				# @param headers [Protocol::HTTP::Headers] the mutable HTTP headers for the request.
				# @return [Body | nil] an optional request body based on the given payload.
				def prepare_request(request, payload)
					request.body = ::Protocol::HTTP::Body::Buffered.wrap(payload)
				end
				
				# @param request [Protocol::HTTP::Request] the request that was made.
				# @param response [Protocol::HTTP::Response] the response that was received.
				# @return [Object] some application specific representation of the response.
				def process_response(request, response)
					wrap_response(response)
				end
				
				def parser_for(response)
					# It's not always clear why this error is being thrown.
					return Unsupported
				end
				
				# Wrap the response body in the given klass.
				def wrap_response(response)
					if body = response.body
						response.body = parser_for(response).new(body)
					end
					
					return response
				end
				
				class Unsupported < HTTP::Body::Wrapper
					def join
						raise UnsupportedError, super
					end
				end
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
async-rest-0.13.0 lib/async/rest/wrapper/generic.rb