Class: RestfulResource::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/restful_resource/http_client.rb

Defined Under Namespace

Classes: BadGateway, ClientError, Conflict, HttpError, OtherHttpError, ResourceNotFound, RetryableError, ServiceUnavailable, Timeout, UnprocessableEntity

Instance Method Summary collapse

Constructor Details

#initialize(username: nil, password: nil, logger: nil, cache_store: nil, connection: nil, instrumentation: {}, open_timeout: 2, timeout: 10) ⇒ HttpClient

Returns a new instance of HttpClient.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/restful_resource/http_client.rb', line 65

def initialize(username: nil,
               password: nil,
               logger: nil,
               cache_store: nil,
               connection: nil,
               instrumentation: {},
               open_timeout: 2,
               timeout: 10)
  api_name = instrumentation[:api_name]            ||= 'api'
  instrumentation[:request_instrument_name]        ||= "http.#{api_name}"
  instrumentation[:cache_instrument_name]          ||= "http_cache.#{api_name}"
  instrumentation[:server_cache_instrument_name]   ||= "cdn_metrics.#{api_name}"

  if instrumentation[:metric_class]
    @metrics = Instrumentation.new(instrumentation.slice(:app_name,
                                                         :api_name,
                                                         :request_instrument_name,
                                                         :cache_instrument_name,
                                                         :server_cache_instrument_name,
                                                         :metric_class))
    @metrics.subscribe_to_notifications
  end

  # Use a provided faraday client or initalize a new one
  @connection = connection || initialize_connection(logger: logger,
                                                    cache_store: cache_store,
                                                    instrumenter: ActiveSupport::Notifications,
                                                    request_instrument_name: instrumentation.fetch(:request_instrument_name, nil),
                                                    cache_instrument_name: instrumentation.fetch(:cache_instrument_name, nil),
                                                    server_cache_instrument_name: instrumentation.fetch(:server_cache_instrument_name, nil))

  @connection.basic_auth(username, password) if username && password
  @default_open_timeout = open_timeout
  @default_timeout = timeout
end

Instance Method Details

#delete(url, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/restful_resource/http_client.rb', line 113

def delete(url, headers: {}, open_timeout: nil, timeout: nil)
  http_request(
    Request.new(
      :delete,
      url,
      headers: headers,
      open_timeout: open_timeout,
      timeout: timeout
    )
  )
end

#get(url, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/restful_resource/http_client.rb', line 101

def get(url, headers: {}, open_timeout: nil, timeout: nil)
  http_request(
    Request.new(
      :get,
      url,
      headers: headers,
      open_timeout: open_timeout,
      timeout: timeout
    )
  )
end

#post(url, data: {}, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/restful_resource/http_client.rb', line 138

def post(url, data: {}, headers: {}, open_timeout: nil, timeout: nil)
  http_request(
    Request.new(
      :post,
      url,
      body: data,
      headers: headers,
      open_timeout: open_timeout,
      timeout: timeout
    )
  )
end

#put(url, data: {}, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/restful_resource/http_client.rb', line 125

def put(url, data: {}, headers: {}, open_timeout: nil, timeout: nil)
  http_request(
    Request.new(
      :put,
      url,
      body: data,
      headers: headers,
      open_timeout: open_timeout,
      timeout: timeout
    )
  )
end