class Aptly::Connection
Connection
adaptor. This class wraps HTTP interactions for our purposes and adds general purpose automation on top of the raw HTTP actions.
Constants
- CODE_ERRORS
- DEFAULT_QUERY
- GETISH_ACTIONS
- HTTP_ACTIONS
- POSTISH_ACTIONS
- WRITE_ACTIONS
Public Class Methods
new(config: ::Aptly.configuration, query: DEFAULT_QUERY, uri: config.uri)
click to toggle source
New connection. @param config [Configuration] Configuration
instance to use @param query [Hash] Default HTTP query paramaters, these get the
specific query parameters merged upon.
@param uri [URI] Base URI for the remote (default from
{Configuration#uri}).
# File lib/aptly/connection.rb, line 54 def initialize(config: ::Aptly.configuration, query: DEFAULT_QUERY, uri: config.uri) @query = query @base_uri = uri raise if faraday_uri.nil? @config = config @connection = Faraday.new(faraday_uri) do |c| c.request :multipart c.request :url_encoded c.adapter :excon, @adapter_options end end
Private Instance Methods
add_api(relative_path)
click to toggle source
# File lib/aptly/connection.rb, line 103 def add_api(relative_path) "/api#{relative_path}" end
build_query(kwords)
click to toggle source
# File lib/aptly/connection.rb, line 95 def build_query(kwords) query = @query.merge(kwords.delete(:query) { {} }) if kwords.delete(:query_mangle) { true } query = query.map { |k, v| [k.to_s.capitalize, v] }.to_h end query end
faraday_uri()
click to toggle source
# File lib/aptly/connection.rb, line 81 def faraday_uri @adapter_options ||= {} @faraday_uri ||= begin uri = @base_uri.clone return uri unless uri.scheme == 'unix' # For Unix domain sockets we need to divide the bits apart as Excon # expects the path URI without a socket path and the socket path as # option. @adapter_options[:socket] = uri.path uri.host = nil uri end end
handle_error(response)
click to toggle source
# File lib/aptly/connection.rb, line 155 def handle_error(response) error = CODE_ERRORS.fetch(response.status, nil) raise error, response.body if error response end
http_call(action, path, kwords)
click to toggle source
# File lib/aptly/connection.rb, line 161 def http_call(action, path, kwords) if POSTISH_ACTIONS.include?(action) response = run_postish(action, path, kwords) elsif GETISH_ACTIONS.include?(action) response = run_getish(action, path, kwords) else raise "Unknown http action: #{action}" end handle_error(response) rescue Faraday::TimeoutError => e raise Errors::TimeoutError, e.message end
mangle_post(body, headers, kwords)
click to toggle source
# File lib/aptly/connection.rb, line 107 def mangle_post(body, headers, kwords) if body headers ||= {} headers['Content-Type'] = 'application/json' else kwords.each do |k, v| if k.to_s.start_with?('file_') body ||= {} body[k] = Faraday::UploadIO.new(v, 'application/binary') end end end [body, headers] end
run_getish(action, path, kwords)
click to toggle source
# File lib/aptly/connection.rb, line 141 def run_getish(action, path, kwords) body = kwords.delete(:body) params = kwords.delete(:query) headers = kwords.delete(:headers) @connection.send(action, path, params, headers) do |request| setup_request(action, request) if body request.headers[:content_type] = 'application/json' request.body = body end end end
run_postish(action, path, kwords)
click to toggle source
# File lib/aptly/connection.rb, line 128 def run_postish(action, path, kwords) body = kwords.delete(:body) params = kwords.delete(:query) headers = kwords.delete(:headers) body, headers = mangle_post(body, headers, kwords) @connection.send(action, path, body, headers) do |request| setup_request(action, request) request.params.update(params) if params end end
setup_request(action, request)
click to toggle source
# File lib/aptly/connection.rb, line 122 def setup_request(action, request) standard_timeout = @config.timeout standard_timeout = @config.write_timeout if WRITE_ACTIONS.include?(action) request.options.timeout = standard_timeout end