Class: FriendlyShipping::HttpClient

Inherits:
Object
  • Object
show all
Includes:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/http_client.rb

Overview

A façade for RestClient which constructs requests, wraps responses in Dry::Monad results, and calls the API error handler with failures.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_handler: FriendlyShipping::ApiErrorHandler.new) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • error_handler (.call) (defaults to: FriendlyShipping::ApiErrorHandler.new)

    called if an API error occurs



13
14
15
# File 'lib/friendly_shipping/http_client.rb', line 13

def initialize(error_handler: FriendlyShipping::ApiErrorHandler.new)
  @error_handler = error_handler
end

Instance Attribute Details

#error_handler.call (readonly)

Returns the API error handler.

Returns:

  • (.call)

    the API error handler



10
11
12
# File 'lib/friendly_shipping/http_client.rb', line 10

def error_handler
  @error_handler
end

Instance Method Details

#delete(request) ⇒ Success<Response>, Failure<ApiResult>

Makes a DELETE request and handles the response.

Parameters:

  • request (Request)

    the request to DELETE

Returns:



33
34
35
36
37
38
39
# File 'lib/friendly_shipping/http_client.rb', line 33

def delete(request)
  http_response = ::RestClient.delete(request.url, request.headers)

  Success(Response.new_from_rest_client_response(http_response))
rescue ::RestClient::Exception => e
  error_handler.call(e, original_request: request, original_response: e.response)
end

#get(request) ⇒ Success<Response>, Failure<ApiResult>

Makes a GET request and handles the response.

Parameters:

  • request (Request)

    the request to GET

Returns:



20
21
22
23
24
25
26
27
28
# File 'lib/friendly_shipping/http_client.rb', line 20

def get(request)
  http_response = ::RestClient.get(
    request.url, request.headers
  )

  Success(Response.new_from_rest_client_response(http_response))
rescue ::RestClient::Exception => e
  error_handler.call(e, original_request: request, original_response: e.response)
end

#post(request) ⇒ Success<Response>, Failure<ApiResult>

Makes a POST request and handles the response.

Parameters:

  • request (Request)

    the request to POST

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/friendly_shipping/http_client.rb', line 44

def post(request)
  http_response = ::RestClient.post(
    request.url,
    request.body,
    request.headers
  )

  Success(Response.new_from_rest_client_response(http_response))
rescue ::RestClient::Exception => e
  error_handler.call(e, original_request: request, original_response: e.response)
end

#put(request) ⇒ Success<Response>, Failure<ApiResult>

Makes a PUT request and handles the response.

Parameters:

  • request (Request)

    the request to PUT

Returns:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/friendly_shipping/http_client.rb', line 59

def put(request)
  http_response = ::RestClient.put(
    request.url,
    request.body,
    request.headers
  )

  Success(Response.new_from_rest_client_response(http_response))
rescue ::RestClient::Exception => e
  error_handler.call(e, original_request: request, original_response: e.response)
end