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.



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

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.



6
7
8
# File 'lib/3scale/api/http_client.rb', line 6

def admin_domain
  @admin_domain
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



6
7
8
# File 'lib/3scale/api/http_client.rb', line 6

def endpoint
  @endpoint
end

#formatObject (readonly)

Returns the value of attribute format.



6
7
8
# File 'lib/3scale/api/http_client.rb', line 6

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/3scale/api/http_client.rb', line 6

def headers
  @headers
end

#provider_keyObject (readonly)

Returns the value of attribute provider_key.



6
7
8
# File 'lib/3scale/api/http_client.rb', line 6

def provider_key
  @provider_key
end

Instance Method Details

#delete(path) ⇒ Object



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

def delete(path)
  parse @http.delete("#{path}.#{format}", headers)
end

#forbidden!(response) ⇒ Object

Raises:



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

def forbidden!(response)
  raise ForbiddenError, response
end

#get(path) ⇒ Object



31
32
33
# File 'lib/3scale/api/http_client.rb', line 31

def get(path)
  parse @http.get("#{path}.#{format}", headers)
end

#parse(response) ⇒ Object

Parameters:



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

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



65
66
67
68
69
70
# File 'lib/3scale/api/http_client.rb', line 65

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

#post(path, body:) ⇒ Object



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

def post(path, body: )
  parse @http.post("#{path}.#{format}", serialize(body), headers)
end

#serialize(body) ⇒ Object



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

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