# frozen_string_literal: true module PlatformSdk module PowerSchool # Powerschool::Client class Client attr_reader :bearer_token, :conn def initialize(base_url:, bearer_token: nil, expansions: nil) @bearer_token = bearer_token @bearer_token ||= PlatformSdk::PowerSchool.bearer_token @expansions = expansions || self.class.expansions @conn = Faraday.new(base_url, headers:) do |conn| conn.response :raise_error conn.request :json conn.response :json conn.adapter :net_http end end def headers { "Authorization" => "Bearer #{@bearer_token}", "Content-Type" => "application/json", "Accept" => "application/json" } end def power_query(query_name, page_size: 0, params: nil) scrub_query(query_name) @conn.post("/ws/schema/query/com.strongmind.#{query_name}?pagesize=#{page_size}", params).body["record"] end def special_programs records_as_json = power_query("specialprograms") records_as_json.map { |record| SpecialProgram.new(record) } end def self.expansions { STUDENTS: %w[demographics addresses alerts phones school_enrollment ethnicity_race contact contact_info initial_enrollment schedule_setup fees lunch], SECTIONS: %w[term], TEACHERS: %w[addresses emails phones school_affiliations] } end def valid_power_query_names %w[attendance_daily_total specialprograms edkey.attendance_get_daily_total_minutes pitcrew.edkey.attendance_get_bulk] end def scrub_query(query_name) raise PowerQueryNotValid unless valid_power_query_names.include?(query_name) end end end end