Sha256: 14b14ab65bae4d2b0a375bbb1aae68c3135d86bd1b4e350d41b9cc30002b3835
Contents?: true
Size: 1.78 KB
Versions: 11
Compression:
Stored size: 1.78 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 access_denied(response) 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
11 entries across 11 versions & 1 rubygems