Sha256: e1c49fbdd6bb1b0796f9fbdcbc91793ab144a599941ff410bb330202992a0017

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

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

      def orgs(roster_id: nil)
        resource_path = '/api/orgs'
        resource_path += "?roster_id=#{roster_id}" if roster_id
        response = get(resource_path)
        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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
strongmind-platform-sdk-3.23.4 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.23.3 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.23.2 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.23.1 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.23.0 lib/platform_sdk/central/client.rb
strongmind-platform-sdk-3.22.1 lib/platform_sdk/central/client.rb