class Armrest::Api::Base
Constants
- HTTP_READ_METHODS
- HTTP_WRITE_METHODS
- MAX_RETRIES
Public Class Methods
new(options={})
click to toggle source
# File lib/armrest/api/base.rb, line 9 def initialize(options={}) @options = options @camelize_request_data = true end
Public Instance Methods
append_api_version(path)
click to toggle source
# File lib/armrest/api/base.rb, line 96 def append_api_version(path) separator = path.include?('?') ? '&' : '?' path + separator + "api-version=#{api_version}" end
build_request(klass, path, data={})
click to toggle source
# File lib/armrest/api/base.rb, line 66 def build_request(klass, path, data={}) data = camelize(data) req = klass.new(path) # url includes query string and uri.path does not, must used url set_headers!(req) logger.debug "build_request data #{data}" # Note: Need to use to_s for case statement to work right case klass.to_s.split('::').last.underscore when "delete", "patch", "post", "put" text = JSON.dump(data) req.body = text req.content_length = text.bytesize when "get" req.set_form_data(data) if data && !data.empty? end req end
camelize(data)
click to toggle source
# File lib/armrest/api/base.rb, line 86 def camelize(data) return data unless @camelize_request_data data.deep_transform_keys { |k| k.to_s.camelize(:lower) } end
headers()
click to toggle source
interface method
# File lib/armrest/api/base.rb, line 108 def headers {} end
http(url)
click to toggle source
# File lib/armrest/api/base.rb, line 112 def http(url) uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = @open_timeout || 30 http.read_timeout = @read_timeout || 30 http.use_ssl = true if uri.scheme == 'https' http end
request(klass, path, data={})
click to toggle source
Always translate raw json response to ruby Hash
# File lib/armrest/api/base.rb, line 28 def request(klass, path, data={}) path = standarize_path(path) url = url(path) req = build_request(klass, path, data) http = http(url) meth = klass.to_s.split('::').last.underscore logger.debug "#{meth.upcase} #{url}".color(:yellow) # logger.debug "req['Authorization'] #{req['Authorization']}" resp = send_request(http, req) logger.debug "resp #{resp}" logger.debug "resp.code #{resp.code}" logger.debug "resp.body #{resp.body}" if HTTP_WRITE_METHODS.include?(meth) && resp.code !~ /^20/ raise Armrest::Error.new(resp) end resp end
send_request(http, req)
click to toggle source
# File lib/armrest/api/base.rb, line 51 def send_request(http, req) retries = 0 resp = http.request(req) # send request retry_codes = [/^5/, "429"] retry_code_detected = retry_codes.detect { |code| resp.code.match(code) } if retry_code_detected && retries < MAX_RETRIES retries += 1 delay = 2 ** retries logger.debug "retries #{retries} sleep #{delay} and will retry." sleep delay resp = http.request(req) # send request end resp end
set_headers!(req)
click to toggle source
# File lib/armrest/api/base.rb, line 101 def set_headers!(req) headers.each do |k,v| req[k.to_s] = v.to_s end end
standarize_path(path)
click to toggle source
# File lib/armrest/api/base.rb, line 91 def standarize_path(path) path = "/#{path}" unless path.starts_with?('/') path = append_api_version(path) end
url(path)
click to toggle source
interface method. API endpoint does not include the /. IE: login.microsoftonline.com
# File lib/armrest/api/base.rb, line 129 def url(path) "#{endpoint}#{path}" end
with_open_timeout(value) { || ... }
click to toggle source
# File lib/armrest/api/base.rb, line 122 def with_open_timeout(value) saved, @open_timeout = @open_timeout, value yield @open_timeout = saved end