# frozen_string_literal: true module PlatformSdk module IdMapper # IdMapper::Client class Client attr_reader :token, :domain, :conn def initialize(secret) @token = secret[:token] @domain = secret[:domain] @token ||= PlatformSdk::IdMapper.token @domain ||= PlatformSdk::IdMapper.domain @conn = Faraday.new("https://#{@domain}") do |faraday| faraday.headers = headers faraday.adapter Faraday.default_adapter faraday.response :json, content_type: /\bjson$/ faraday.response :raise_error end end private def models PlatformSdk::IdMapper::Models end def get(path, params = nil) response = @conn.get(path, params) response.body end def headers { "Authorization" => "Token #{@token}" } end def list_of_models(model, list) list.map do |item| model.new(item) end end public def pairs(guid) path = "api/v1/pairs/#{guid}/" begin get(path) rescue Faraday::ResourceNotFound raise PlatformSdk::IdMapper::PairNotFoundError, msg = guid end end def domains(**params) path = 'api/v1/domains/' body = get(path, params) list_of_models(models::Domain, body) end def domain_by_name(name) path = "api/v1/domains/#{name}/" body = get(path) models::Domain.new(body) end def domain_ltis(**params) path = 'api/v1/domainLtis/' body = get(path, params) list_of_models(models::DomainLti, body) end def domain_ltis_by_domain(domain_name) path = "api/v1/domains/#{domain_name}/ltis/" body = get(path) list_of_models(models::DomainLti, body) end def domain_ltis_by_lti(lti_identifier) path = "api/v1/domainLtis/?lti__identifier=#{lti_identifier}" body = get(path) list_of_models(models::DomainLti, body) end def domain_lti_by_domain_and_lti(domain_name, lti_identifier) path = "api/v1/domains/#{domain_name}/ltis/#{lti_identifier}/" body = get(path) list_of_models(models::DomainLti, body) end def ltis(**params) path = 'api/v1/ltis/' body = get(path, params) list_of_models(models::Lti, body) end def lti_by_identifier(identifier) path = "api/v1/ltis/#{identifier}/" body = get(path) models::Lti.new(body) end end end end