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, faraday_config: nil) ⇒ 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
100
101
102
# 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,
               faraday_config: nil)
  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),
                                                    faraday_config: faraday_config)

  @connection.basic_auth(username, password) if username && password
  @connection.headers[:user_agent] = build_user_agent(instrumentation[:app_name])
  @default_open_timeout = open_timeout
  @default_timeout = timeout
end

Instance Method Details

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



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/restful_resource/http_client.rb', line 116

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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/restful_resource/http_client.rb', line 104

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



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

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



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

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