Class: ThreeScale::API::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale/api/http_client.rb

Defined Under Namespace

Modules: JSONParser Classes: ForbiddenError, NotFoundError, UnexpectedResponseError, UnknownFormatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, provider_key:, format: :json, verify_ssl: true, keep_alive: false) ⇒ HttpClient

Returns a new instance of HttpClient.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/3scale/api/http_client.rb', line 11

def initialize(endpoint:, provider_key:, format: :json, verify_ssl: true, keep_alive: false)
  @endpoint = URI(endpoint).freeze
  @admin_domain = @endpoint.host.freeze
  @provider_key = provider_key.freeze
  @http = Net::HTTP.new(admin_domain, @endpoint.port)
  @http.use_ssl = @endpoint.is_a?(URI::HTTPS)
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless verify_ssl

  @headers = {
    'Accept' => "application/#{format}",
    'Content-Type' => "application/#{format}",
    'Authorization' => 'Basic ' + [":#{@provider_key}"].pack('m').delete("\r\n")
  }

  if debug?
    @http.set_debug_output($stdout)
    @headers['Accept-Encoding'] = 'identity'
  end

  @headers.freeze

  @format = format

  @keep_alive = keep_alive
end

Instance Attribute Details

#admin_domainObject (readonly)

Returns the value of attribute admin_domain.



9
10
11
# File 'lib/3scale/api/http_client.rb', line 9

def admin_domain
  @admin_domain
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



9
10
11
# File 'lib/3scale/api/http_client.rb', line 9

def endpoint
  @endpoint
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/3scale/api/http_client.rb', line 9

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/3scale/api/http_client.rb', line 9

def headers
  @headers
end

#keep_aliveObject (readonly)

Returns the value of attribute keep_alive.



9
10
11
# File 'lib/3scale/api/http_client.rb', line 9

def keep_alive
  @keep_alive
end

#provider_keyObject (readonly)

Returns the value of attribute provider_key.



9
10
11
# File 'lib/3scale/api/http_client.rb', line 9

def provider_key
  @provider_key
end

Instance Method Details

#delete(path, params: nil) ⇒ Object



61
62
63
64
65
# File 'lib/3scale/api/http_client.rb', line 61

def delete(path, params: nil)
  @http.start if keep_alive && !@http.started?

  parse @http.delete(format_path_n_query(path, params), headers)
end

#forbidden!(response) ⇒ Object

Raises:



85
86
87
# File 'lib/3scale/api/http_client.rb', line 85

def forbidden!(response)
  raise ForbiddenError.new(response, format_response(response))
end

#get(path, params: nil) ⇒ Object



37
38
39
40
41
# File 'lib/3scale/api/http_client.rb', line 37

def get(path, params: nil)
  @http.start if keep_alive && !@http.started?

  parse @http.get(format_path_n_query(path, params), headers)
end

#notfound!(response) ⇒ Object

Raises:



89
90
91
# File 'lib/3scale/api/http_client.rb', line 89

def notfound!(response)
  raise NotFoundError.new(response, format_response(response))
end

#parse(response) ⇒ Object

Parameters:

  • response (::Net::HTTPResponse)


68
69
70
71
72
73
74
75
# File 'lib/3scale/api/http_client.rb', line 68

def parse(response)
  case response
  when Net::HTTPUnprocessableEntity, Net::HTTPSuccess then parser.decode(response.body)
  when Net::HTTPForbidden then forbidden!(response)
  when Net::HTTPNotFound then notfound!(response)
  else unexpected!(response)
  end
end

#parserObject



109
110
111
112
113
114
# File 'lib/3scale/api/http_client.rb', line 109

def parser
  case format
  when :json then JSONParser
  else unknownformat!
  end
end

#patch(path, body:, params: nil) ⇒ Object



43
44
45
46
47
# File 'lib/3scale/api/http_client.rb', line 43

def patch(path, body:, params: nil)
  @http.start if keep_alive && !@http.started?

  parse @http.patch(format_path_n_query(path, params), serialize(body), headers)
end

#post(path, body:, params: nil) ⇒ Object



49
50
51
52
53
# File 'lib/3scale/api/http_client.rb', line 49

def post(path, body:, params: nil)
  @http.start if keep_alive && !@http.started?

  parse @http.post(format_path_n_query(path, params), serialize(body), headers)
end

#put(path, body: nil, params: nil) ⇒ Object



55
56
57
58
59
# File 'lib/3scale/api/http_client.rb', line 55

def put(path, body: nil, params: nil)
  @http.start if keep_alive && !@http.started?

  parse @http.put(format_path_n_query(path, params), serialize(body), headers)
end

#serialize(body) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/3scale/api/http_client.rb', line 101

def serialize(body)
  case body
  when nil then nil
  when String then body
  else parser.encode(body)
  end
end

#unexpected!(response) ⇒ Object



93
94
95
# File 'lib/3scale/api/http_client.rb', line 93

def unexpected!(response)
  raise UnexpectedResponseError.new(response, format_response(response))
end

#unknownformat!Object

Raises:



97
98
99
# File 'lib/3scale/api/http_client.rb', line 97

def unknownformat!
  raise UnknownFormatError, "unknown format #{format}"
end