class ShafClient
Constants
- DEFAULT_ACCEPT_HEADER
- DEFAULT_ADAPTER
- VERSION
Attributes
auth_header[R]
options[R]
Public Class Methods
new(root_uri, **options)
click to toggle source
# File lib/shaf_client.rb, line 36 def initialize(root_uri, **options) @root_uri = root_uri.dup @options = options setup_default_headers setup_basic_auth setup_client end
Public Instance Methods
get_root(**options)
click to toggle source
# File lib/shaf_client.rb, line 45 def get_root(**options) get(@root_uri, **options) end
Private Instance Methods
adapter_args()
click to toggle source
# File lib/shaf_client.rb, line 113 def adapter_args [@adapter] end
basic_auth?()
click to toggle source
# File lib/shaf_client.rb, line 85 def basic_auth? @user && @pass end
connect_adapter(connection)
click to toggle source
# File lib/shaf_client.rb, line 109 def connect_adapter(connection) connection.adapter(*adapter_args) end
default_headers(http_method)
click to toggle source
# File lib/shaf_client.rb, line 130 def default_headers(http_method) headers = @default_headers.dup headers.delete('Content-Type') unless %i[put patch post].include? http_method headers end
faraday_cache_params(options)
click to toggle source
# File lib/shaf_client.rb, line 101 def faraday_cache_params(options) options.fetch(:faraday_http_cache, {}).tap do |cache_params| cache_params[:store] ||= options[:http_cache_store] if options[:http_cache_store] cache_params[:shared_cache] ||= false cache_params[:serializer] ||= Marshal end end
request(method:, uri:, payload: nil, opts: {})
click to toggle source
# File lib/shaf_client.rb, line 117 def request(method:, uri:, payload: nil, opts: {}) payload = JSON.generate(payload) if payload&.is_a?(Hash) headers = default_headers(method).merge(opts.fetch(:headers, {})) @client.send(method) do |req| req.url uri req.body = payload if payload req.headers.merge! headers end rescue StandardError => e raise Error, e.message end
setup_basic_auth()
click to toggle source
# File lib/shaf_client.rb, line 80 def setup_basic_auth @user, @pass = options.slice(:user, :password).values @auth_header = options.fetch(:auth_header, 'Authorization') if basic_auth? end
setup_client()
click to toggle source
# File lib/shaf_client.rb, line 89 def setup_client @adapter = options.fetch(:faraday_adapter, DEFAULT_ADAPTER) cache_params = faraday_cache_params(options) @client = Faraday.new(url: @root_uri) do |conn| conn.basic_auth(@user, @pass) if basic_auth? conn.use Middleware::Redirect conn.use Faraday::HttpCache, **cache_params connect_adapter(conn) end end
setup_default_headers()
click to toggle source
# File lib/shaf_client.rb, line 69 def setup_default_headers @default_headers = { 'Content-Type' => options.fetch(:content_type, MIME_TYPE_JSON), 'Accept' => options.fetch(:accept, DEFAULT_ACCEPT_HEADER) } return unless token = options[:auth_token] @auth_header = options.fetch(:auth_header, 'X-Auth-Token') @default_headers[@auth_header] = token end