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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, provider_key:, format: :json, verify_ssl: true) ⇒ 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
# File 'lib/3scale/api/http_client.rb', line 11

def initialize(endpoint:, provider_key:, format: :json, verify_ssl: true)
  @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
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

#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



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

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

#forbidden!(response) ⇒ Object

Raises:



71
72
73
# File 'lib/3scale/api/http_client.rb', line 71

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

#get(path, params: nil) ⇒ Object



35
36
37
# File 'lib/3scale/api/http_client.rb', line 35

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

#notfound!(response) ⇒ Object

Raises:



75
76
77
# File 'lib/3scale/api/http_client.rb', line 75

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

#parse(response) ⇒ Object

Parameters:

  • response (::Net::HTTPResponse)


56
57
58
59
60
61
62
63
# File 'lib/3scale/api/http_client.rb', line 56

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



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

def parser
  case format
  when :json then JSONParser
  else "unknown format #{format}"
  end
end

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



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

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

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



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

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

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



47
48
49
# File 'lib/3scale/api/http_client.rb', line 47

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

#serialize(body) ⇒ Object



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

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

#unexpected!(response) ⇒ Object



79
80
81
# File 'lib/3scale/api/http_client.rb', line 79

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