class ThreeScale::API::HttpClient
Attributes
admin_domain[R]
endpoint[R]
format[R]
headers[R]
provider_key[R]
Public Class Methods
new(endpoint:, provider_key:, format: :json, verify_ssl: true)
click to toggle source
# 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
Public Instance Methods
delete(path, params: nil)
click to toggle source
# 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)
click to toggle source
# File lib/3scale/api/http_client.rb, line 73 def forbidden!(response) raise ForbiddenError.new(response, format_response(response)) end
get(path, params: nil)
click to toggle source
# 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)
click to toggle source
# File lib/3scale/api/http_client.rb, line 77 def notfound!(response) raise NotFoundError.new(response, format_response(response)) end
parse(response)
click to toggle source
@param [::Net::HTTPResponse] response
# 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
parser()
click to toggle source
# File lib/3scale/api/http_client.rb, line 97 def parser case format when :json then JSONParser else unknownformat! end end
patch(path, body:, params: nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/3scale/api/http_client.rb, line 89 def serialize(body) case body when nil then nil when String then body else parser.encode(body) end end
unexpected!(response)
click to toggle source
# File lib/3scale/api/http_client.rb, line 81 def unexpected!(response) raise UnexpectedResponseError.new(response, format_response(response)) end
unknownformat!()
click to toggle source
# File lib/3scale/api/http_client.rb, line 85 def unknownformat! raise UnknownFormatError, "unknown format #{format}" end
Protected Instance Methods
debug?()
click to toggle source
# File lib/3scale/api/http_client.rb, line 106 def debug? ENV.fetch('THREESCALE_DEBUG', '0') == '1' end
format_path_n_query(path, params)
click to toggle source
Helper to create a string representing a path plus a query string
# File lib/3scale/api/http_client.rb, line 111 def format_path_n_query(path, params) path = "#{path}.#{format}" path << "?#{URI.encode_www_form(params)}" unless params.nil? path end
format_response(response)
click to toggle source
# File lib/3scale/api/http_client.rb, line 117 def format_response(response) body = response.body if text_based?(response) "#{response.inspect} body=#{body}" end
text_based?(response)
click to toggle source
# File lib/3scale/api/http_client.rb, line 122 def text_based?(response) response.content_type =~ /^text/ || response.content_type =~ /^application/ && !['application/octet-stream', 'application/pdf'].include?(response.content_type) end