Class: RestfulResource::HttpClient
- Inherits:
-
Object
- Object
- RestfulResource::HttpClient
- 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
- #delete(url, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object
- #get(url, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object
-
#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: {}, default_headers: {}) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #patch(url, data: {}, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object
- #post(url, data: {}, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object
- #put(url, data: {}, headers: {}, open_timeout: nil, timeout: nil) ⇒ Object
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: {}, default_headers: {}) ⇒ HttpClient
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 130 131 132 133 134 |
# File 'lib/restful_resource/http_client.rb', line 82 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: {}, default_headers: {} ) 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: ) @connection.headers.merge!(default_headers) 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
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/restful_resource/http_client.rb', line 148 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
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/restful_resource/http_client.rb', line 136 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
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/restful_resource/http_client.rb', line 160 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
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/restful_resource/http_client.rb', line 186 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
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/restful_resource/http_client.rb', line 173 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 |