Class: Teems::Services::ApiClient

Inherits:
Object
  • Object
show all
Includes:
ConnectionPool, ResponseHandler, WriteRequests
Defined in:
lib/teems/services/api_client.rb

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
AUTH_HEADERS =
{
  msgservice: ->() { ['Authentication', .skype_auth_header] },
  teams: ->() { ['Authorization', "Bearer #{.skype_token}"] },
  presence: ->() { ['Authorization', "Bearer #{.presence_token}"] }
}.freeze
DEFAULT_AUTH_HEADER =
->() { ['Authorization', .teams_auth_header] }

Constants included from ConnectionPool

ConnectionPool::DEFAULT_ENDPOINTS, ConnectionPool::TIMEOUTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WriteRequests

#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_countObject (readonly)

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

#closeObject



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, , 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)
   = 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)
  apply_headers(req, options.fetch(:headers, {}))
  apply_auth(req, , endpoint_key)
  run_request(path, get_http_for_endpoint(endpoint_key)) { |http| http.request(req) }
end

#on_requestObject



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_responseObject



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