Sha256: dc4bdf5efe16d2a45a5ea32a341b862821c440b0ec425f45ed9c9bcf53c47397
Contents?: true
Size: 1.88 KB
Versions: 7
Compression:
Stored size: 1.88 KB
Contents
module Roqua module CoreApi module Sessions class AuthSession attr_reader :core_site attr_reader :default_timeout # timeout for requests in seconds def initialize(core_site: ENV.fetch('CORE_SITE'), timeout: nil) @core_site = core_site @default_timeout = timeout end def get(path, timeout: default_timeout, **params) perform_request_or_fail do HTTParty.get(full_url_for(path), headers: headers, query: params, basic_auth: basic_auth, timeout: timeout) end end def post(path, timeout: default_timeout, **params) perform_request_or_fail do HTTParty.post(full_url_for(path), headers: headers, body: params, basic_auth: basic_auth, timeout: timeout) end end def patch(path, timeout: default_timeout, **params) perform_request_or_fail do HTTParty.patch(full_url_for(path), headers: headers, body: params, basic_auth: basic_auth, timeout: timeout) end end def delete(path, timeout: default_timeout, **params) HTTParty.delete(full_url_for(path), headers: headers, query: params, basic_auth: basic_auth, timeout: timeout) end private def perform_request_or_fail(&block) response = yield case response.code when 200..299, 422 response when 401 if response['no_session'] fail NoSession else fail Unauthorized end else fail response.parsed_response || 'error' end end def full_url_for(path) core_site + api_base + path + '.json' end def api_base '/api/v1' end def headers end def basic_auth end end end end end
Version data entries
7 entries across 7 versions & 1 rubygems