module PlatformSdk module Central class Client attr_reader :access_token, :base_url, :conn def initialize(base_url, access_token, conn: nil) @access_token = access_token @base_url = base_url @conn = conn || build_connection end def user(uid) resource_path = "/api/users/#{uid}" response = get(resource_path) response.body end def teacher_enrollments(external_course_id = nil) resource_path = '/api/teacher_enrollments' resource_path += "?external_course_id=#{external_course_id}" if external_course_id response = get(resource_path) response.body end def create_message(message_params: {}) resource_path = '/api/messages' response = post(resource_path, message_params) response.body end private def build_connection Faraday.new(@base_url) do |faraday| faraday.headers = default_headers faraday.adapter Faraday.default_adapter faraday.response :json, content_type: /\bjson$/, parser_options: { symbolize_names: true } faraday.response :raise_error end end def default_headers { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{access_token}" } end def get(path, params = {}) @conn.get(path, params) end def post(path, params = {}) @conn.post(path, params.to_json) end end end end