Sha256: d0e071304355de943de5ebb0f892119ea6feaf38ff7894d9ecec82ff60568fe1
Contents?: true
Size: 1.99 KB
Versions: 1
Compression:
Stored size: 1.99 KB
Contents
# frozen_string_literal: true module Fizzy module Api module Sessions class BasicAuthSession attr_reader :fizzy_url attr_reader :username attr_reader :password def initialize(fizzy_url: ENV['FIZZY_URL'], username: ENV['FIZZY_BASICAUTH_ID'], password: ENV['FIZZY_BASICAUTH_SECRET']) @fizzy_url = fizzy_url @username = username @password = password end def get(path, params = {}) perform_request_or_fail do HTTParty.get(full_url_for(path), query: params, basic_auth: basic_auth) end end def post(path, params = {}) perform_request_or_fail do HTTParty.post(full_url_for(path), headers: { 'Content-Type' => 'application/json' }, body: params.to_json, basic_auth: basic_auth) end end def delete(path, params = {}) perform_request_or_fail do HTTParty.delete(full_url_for(path), query: params, basic_auth: basic_auth) end end private def perform_request_or_fail(&_block) response = yield case response.code when 401 access_denied(response) when 500 raise((response.parsed_response || "Received HTTP response code #{response.code}!").to_s) else response end end def access_denied(response) raise NoSession if response.headers['WWW-Authenticate'] raise Unauthorized end def full_url_for(path) fizzy_url + api_base + path end def api_base '/api/v1' end def basic_auth { username: username, password: password } end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
fizzy-api-0.1.5 | lib/fizzy/api/sessions/basic_auth_session.rb |