Class: ThreeScale::API::HttpClient

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

Defined Under Namespace

Modules: JSONParser Classes: ForbiddenError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, provider_key:, format: :json) ⇒ HttpClient

Returns a new instance of HttpClient.



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

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

  @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.



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

def admin_domain
  @admin_domain
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#provider_keyObject (readonly)

Returns the value of attribute provider_key.



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

def provider_key
  @provider_key
end

Instance Method Details

#delete(path, params: nil) ⇒ Object



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

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

#forbidden!(response) ⇒ Object

Raises:



64
65
66
# File 'lib/3scale/api/http_client.rb', line 64

def forbidden!(response)
  raise ForbiddenError, response
end

#get(path, params: nil) ⇒ Object



33
34
35
# File 'lib/3scale/api/http_client.rb', line 33

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

#parse(response) ⇒ Object

Parameters:

  • response (::Net::HTTPResponse)


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

def parse(response)
  case response
  when Net::HTTPUnprocessableEntity, Net::HTTPSuccess then parser.decode(response.body)
  when Net::HTTPForbidden then forbidden!(response)
  else "Can't handle #{response.inspect}"
  end
end

#parserObject



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

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

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



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

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



41
42
43
# File 'lib/3scale/api/http_client.rb', line 41

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



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

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

#serialize(body) ⇒ Object



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

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