Sha256: 38543084e0f7f93d8ff26c55ecaede877c667e1330961dd7debeeebda9f50b9f

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'fractal_api/configuration'
require 'fractal_api/errors'
require 'fractal_api/version'

module FractalApi
  class Authenticate
    def call(api_key, partner_id)
      response = post(
        '/token',
        api_key: api_key,
        partner_id: partner_id
      )

      if response.success?
        response.body[:access_token]
      elsif response.status == 403
        raise UnauthorizedError, response.body
      else
        raise GenericError, response.body
      end
    end

    private

    def post(path, api_key:, partner_id:)
      connection.post(path, nil, { 'X-Api-Key' => api_key, 'X-Partner-Id' => partner_id })
    end

    def connection
      auth_url = FractalApi.configuration.auth_url

      @connection ||= Faraday.new(url: auth_url) do |conn|
        conn.headers.merge!(
          content_type: 'application/json',
          user_agent: "finpoint/#{FractalApi::VERSION}"
        )
        conn.response :multi_json, symbolize_keys: true
        conn.response :logger if FractalApi.configuration.debug
        conn.adapter Faraday.default_adapter
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fractal_api-0.1.5 lib/fractal_api/authenticate.rb
fractal_api-0.1.4 lib/fractal_api/authenticate.rb
fractal_api-0.1.3 lib/fractal_api/authenticate.rb
fractal_api-0.1.2 lib/fractal_api/authenticate.rb
fractal_api-0.1.1 lib/fractal_api/authenticate.rb
fractal_api-0.1.0 lib/fractal_api/authenticate.rb