Class: Slk::Services::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/services/api_client.rb

Overview

HTTP client for Slack API with connection pooling rubocop:disable Metrics/ClassLength

Constant Summary collapse

BASE_URL =
ENV.fetch('SLACK_API_BASE', 'https://slack.com/api')
NETWORK_ERRORS =

Network errors that should be wrapped in ApiError

[
  SocketError,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ETIMEDOUT,
  Errno::EHOSTUNREACH,
  Net::OpenTimeout,
  Net::ReadTimeout,
  OpenSSL::SSL::SSLError
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiClient

Returns a new instance of ApiClient.



25
26
27
28
29
30
31
32
# File 'lib/slk/services/api_client.rb', line 25

def initialize
  @call_count = 0
  @on_request = nil
  @on_response = nil
  @on_request_body = nil
  @on_response_body = nil
  @http_cache = {}
end

Instance Attribute Details

#call_countObject (readonly)

Returns the value of attribute call_count.



22
23
24
# File 'lib/slk/services/api_client.rb', line 22

def call_count
  @call_count
end

#on_requestObject

Returns the value of attribute on_request.



23
24
25
# File 'lib/slk/services/api_client.rb', line 23

def on_request
  @on_request
end

#on_request_bodyObject

Returns the value of attribute on_request_body.



23
24
25
# File 'lib/slk/services/api_client.rb', line 23

def on_request_body
  @on_request_body
end

#on_responseObject

Returns the value of attribute on_response.



23
24
25
# File 'lib/slk/services/api_client.rb', line 23

def on_response
  @on_response
end

#on_response_bodyObject

Returns the value of attribute on_response_body.



23
24
25
# File 'lib/slk/services/api_client.rb', line 23

def on_response_body
  @on_response_body
end

Instance Method Details

#closeObject

Close all cached HTTP connections



35
36
37
38
# File 'lib/slk/services/api_client.rb', line 35

def close
  @http_cache.each_value { |http| safe_close(http) }
  @http_cache.clear
end

#get(workspace, method, params = {}) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/slk/services/api_client.rb', line 50

def get(workspace, method, params = {})
  execute_request(method, params) do |uri, http|
    request = Net::HTTP::Get.new(uri)
    apply_auth_headers(request, workspace)
    http.request(request)
  end
end

#post(workspace, method, params = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/slk/services/api_client.rb', line 40

def post(workspace, method, params = {})
  body = params.empty? ? nil : JSON.generate(params)
  execute_request(method, body: body) do |uri, http|
    request = Net::HTTP::Post.new(uri)
    workspace.headers.each { |k, v| request[k] = v }
    request.body = body
    http.request(request)
  end
end

#post_form(workspace, method, params = {}) ⇒ Object

Form-encoded POST (some Slack endpoints require this)



59
60
61
62
63
64
65
66
67
# File 'lib/slk/services/api_client.rb', line 59

def post_form(workspace, method, params = {})
  body = params.empty? ? nil : URI.encode_www_form(params)
  execute_request(method, body: body) do |uri, http|
    request = Net::HTTP::Post.new(uri)
    apply_auth_headers(request, workspace)
    request.set_form_data(params) unless params.empty?
    http.request(request)
  end
end