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

Version Path
roqua-core-api-0.0.37 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.36 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.35 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.32 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.31 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.30 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.29 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.28 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.27 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.26 lib/roqua/core_api/sessions/auth_session.rb
roqua-core-api-0.0.25 lib/roqua/core_api/sessions/auth_session.rb