Class: Teams::Common::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/teams/common/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, headers: {}, token: nil, connection: nil) ⇒ HttpClient

Returns a new instance of HttpClient.



11
12
13
14
15
16
# File 'lib/teams/common/http_client.rb', line 11

def initialize(base_url: nil, headers: {}, token: nil, connection: nil)
  @base_url = base_url
  @headers = headers
  @token = token
  @connection = connection
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



9
10
11
# File 'lib/teams/common/http_client.rb', line 9

def base_url
  @base_url
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/teams/common/http_client.rb', line 9

def headers
  @headers
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/teams/common/http_client.rb', line 9

def token
  @token
end

Instance Method Details

#clone(base_url: nil, headers: nil, token: nil) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/teams/common/http_client.rb', line 58

def clone(base_url: nil, headers: nil, token: nil)
  self.class.new(
    base_url: base_url || self.base_url,
    headers: self.headers.merge(headers || {}),
    token: token || self.token,
    connection: @connection
  )
end

#delete(path, headers: {}, params: nil) ⇒ Object



34
35
36
# File 'lib/teams/common/http_client.rb', line 34

def delete(path, headers: {}, params: nil)
  request(:delete, path, headers:, params:)
end

#get(path, headers: {}, params: nil) ⇒ Object



18
19
20
# File 'lib/teams/common/http_client.rb', line 18

def get(path, headers: {}, params: nil)
  request(:get, path, headers:, params:)
end

#patch(path, json: nil, body: nil, headers: {}, params: nil) ⇒ Object



26
27
28
# File 'lib/teams/common/http_client.rb', line 26

def patch(path, json: nil, body: nil, headers: {}, params: nil)
  request(:patch, path, json:, body:, headers:, params:)
end

#post(path, json: nil, body: nil, headers: {}, params: nil) ⇒ Object



22
23
24
# File 'lib/teams/common/http_client.rb', line 22

def post(path, json: nil, body: nil, headers: {}, params: nil)
  request(:post, path, json:, body:, headers:, params:)
end

#put(path, json: nil, body: nil, headers: {}, params: nil) ⇒ Object



30
31
32
# File 'lib/teams/common/http_client.rb', line 30

def put(path, json: nil, body: nil, headers: {}, params: nil)
  request(:put, path, json:, body:, headers:, params:)
end

#request(method, path, json: nil, body: nil, headers: {}, params: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/teams/common/http_client.rb', line 38

def request(method, path, json: nil, body: nil, headers: {}, params: nil)
  resolved_token = resolve_token
  response = connection.public_send(method) do |request|
    request.url(path)
    request.params.update(params) if params
    request.headers.update(default_headers)
    request.headers.update(headers)
    request.headers["Authorization"] = "Bearer #{resolved_token}" if resolved_token

    if json
      request.headers["Content-Type"] ||= "application/json"
      request.body = JSON.generate(json)
    elsif body
      request.body = body
    end
  end

  parse_response(response)
end