Class: Teems::Services::ApiClient
Overview
HTTP client for Teams API with connection pooling and multi-endpoint support
Constant Summary
collapse
- NETWORK_ERRORS =
[
SocketError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT,
Errno::EHOSTUNREACH, Net::OpenTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError
].freeze
{
msgservice: ->(account) { ['Authentication', account.] },
teams: ->(account) { ['Authorization', "Bearer #{account.skype_token}"] },
presence: ->(account) { ['Authorization', "Bearer #{account.presence_token}"] }
}.freeze
->(account) { ['Authorization', account.] }
ConnectionPool::DEFAULT_ENDPOINTS, ConnectionPool::TIMEOUTS
Instance Attribute Summary collapse
Instance Method Summary
collapse
#patch, #post
Constructor Details
#initialize(on_request: nil, on_response: nil, endpoints: {}) ⇒ ApiClient
Returns a new instance of ApiClient.
126
127
128
129
130
131
|
# File 'lib/teems/services/api_client.rb', line 126
def initialize(on_request: nil, on_response: nil, endpoints: {})
@call_count = 0
@callbacks = { on_request: on_request, on_response: on_response }
@http_cache = {}
@endpoints = DEFAULT_ENDPOINTS.merge(endpoints.transform_keys(&:to_sym))
end
|
Instance Attribute Details
#call_count ⇒ Object
Returns the value of attribute call_count.
124
125
126
|
# File 'lib/teems/services/api_client.rb', line 124
def call_count
@call_count
end
|
Instance Method Details
#close ⇒ Object
146
147
148
149
150
151
152
153
|
# File 'lib/teems/services/api_client.rb', line 146
def close
@http_cache.each_value do |http|
http.finish if http.started?
rescue IOError
nil
end
@http_cache.clear
end
|
#delete(endpoint_key, path, account:) ⇒ Object
165
166
167
168
169
170
171
172
|
# File 'lib/teems/services/api_client.rb', line 165
def delete(endpoint_key, path, account:)
uri = URI("#{resolve_endpoint(endpoint_key)}#{path}")
run_request(path, get_http_for_endpoint(endpoint_key)) do |http|
req = Net::HTTP::Delete.new(uri)
apply_auth(req, account, endpoint_key)
http.request(req)
end
end
|
#get(endpoint_key, path, **options) ⇒ Object
155
156
157
158
159
160
161
162
163
|
# File 'lib/teems/services/api_client.rb', line 155
def get(endpoint_key, path, **options)
account = options.delete(:account)
base_url = resolve_endpoint(endpoint_key)
uri = build_request_uri(base_url, path, options.fetch(:params, {}))
req = Net::HTTP::Get.new(uri)
(req, options.fetch(:headers, {}))
apply_auth(req, account, endpoint_key)
run_request(path, get_http_for_endpoint(endpoint_key)) { |http| http.request(req) }
end
|
#on_request ⇒ Object
133
|
# File 'lib/teems/services/api_client.rb', line 133
def on_request = @callbacks[:on_request]
|
#on_request=(callback) ⇒ Object
136
137
138
139
|
# File 'lib/teems/services/api_client.rb', line 136
def on_request=(callback)
close_idle_connections
@callbacks[:on_request] = callback
end
|
#on_response ⇒ Object
134
|
# File 'lib/teems/services/api_client.rb', line 134
def on_response = @callbacks[:on_response]
|
#on_response=(callback) ⇒ Object
141
142
143
144
|
# File 'lib/teems/services/api_client.rb', line 141
def on_response=(callback)
close_idle_connections
@callbacks[:on_response] = callback
end
|