Class: RestfulResource::HttpClient

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

Defined Under Namespace

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

Constant Summary collapse

DEFAULT_TIMEOUT_IN_SECS =
10
DEFAULT_OPEN_TIMEOUT_IN_SECS =
2

Instance Method Summary collapse

Constructor Details

#initialize(username: nil, password: nil, auth_token: nil, logger: nil, cache_store: nil, connection: nil, instrumentation: {}, timeout: nil, open_timeout: nil, faraday_config: nil, faraday_options: {}) ⇒ HttpClient

Returns a new instance of HttpClient.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/restful_resource/http_client.rb', line 81

def initialize(username: nil,
  password: nil,
  auth_token: nil,
  logger: nil,
  cache_store: nil,
  connection: nil,
  instrumentation: {},
  timeout: nil,
  open_timeout: nil,
  faraday_config: nil,
  faraday_options: {})
  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,
                                                    timeout: timeout,
                                                    open_timeout: open_timeout,
                                                    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),
                                                    faraday_config: faraday_config,
                                                    faraday_options: faraday_options
                                                   )

  if auth_token
    @connection.headers[:authorization] = "Bearer #{auth_token}"
  elsif username && password
    @connection.basic_auth(username, password)
  end

  @connection.headers[:user_agent] = build_user_agent(instrumentation[:app_name])
end

Instance Method Details

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



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/restful_resource/http_client.rb', line 143

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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/restful_resource/http_client.rb', line 131

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

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



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/restful_resource/http_client.rb', line 155

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

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



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/restful_resource/http_client.rb', line 181

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



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/restful_resource/http_client.rb', line 168

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