<commit_before>class CanvasWrapper

  attr_accessor :account_id, :is_account_admin

  def initialize(canvas_uri, canvas_api_key)
    @canvas_api = Canvas::API.new(host: canvas_uri, token: canvas_api_key)
    @per_page = 100
    self.is_account_admin = false
    begin
      result = api_get_request("accounts/self")
      self.account_id = result['id']
      self.is_account_admin = true
    rescue Canvas::ApiError => ex
      if CanvasWrapper.authorized_fail?(ex)
        accounts = api_get_request("course_accounts")
        self.account_id = accounts.first['id']
      else
        raise ex
      end
    end
  end

  def full_url(api_url)
    "/api/v1/#{api_url}"
  end

  def api_post_request(api_url, payload)
    @canvas_api.post(full_url(api_url), payload)
  end

  def api_get_request(api_url)
    @canvas_api.get(full_url(api_url))
  end

  def current_account
    api_get_request("accounts/self")
  end

  def course_accounts
    api_get_request("course_accounts")
  end

  def accounts
    api_get_request("accounts")
  end
  
  def create_subaccount(params)
    api_post_request("accounts/#{account_id}/sub_accounts", params)
  end

  def sub_accounts
    api_get_request("accounts/#{account_id}/sub_accounts")
  end

  def create_course(params, sub_account_id = nil)
    api_post_request("accounts/#{sub_account_id || account_id}/courses?enroll_me=true", params)
  end

  def get_courses_for_account(sub_account_id = nil, search_term = nil)
    url = "accounts/#{sub_account_id || account_id}/courses?per_page=#{@per_page}"
    url << "&search_term=#{ERB::Util.url_encode(search_term)}" if search_term.present?
    api_get_request(url)
  end

  def assignments(course_id)
    api_get_request("courses/#{course_id}/assignments")
  end

  def discussion_topics(course_id)
    api_get_request("courses/#{course_id}/discussion_topics")
  end

  def get_courses(user_id = nil)
    request = "courses"
    request << "?as_user_id=#{user_id}" if user_id
    api_get_request(request)
  end

  def list_users
    api_get_request("accounts/#{account_id}/users")
  end

  def recent_logins(course_id)
    api_get_request("courses/#{course_id}/recent_students")
  end

  def students(course_id)
    api_get_request("courses/#{course_id}/users?enrollment_type=student")
  end

  def enroll_user(course_id, params)
    api_post_request("courses/#{course_id}/enrollments", params)<commit_msg>Allow sub account when listing users.<commit_after>class CanvasWrapper

  attr_accessor :account_id, :is_account_admin

  def initialize(canvas_uri, canvas_api_key)
    @canvas_api = Canvas::API.new(host: canvas_uri, token: canvas_api_key)
    @per_page = 100
    self.is_account_admin = false
    begin
      result = api_get_request("accounts/self")
      self.account_id = result['id']
      self.is_account_admin = true
    rescue Canvas::ApiError => ex
      if CanvasWrapper.authorized_fail?(ex)
        accounts = api_get_request("course_accounts")
        self.account_id = accounts.first['id']
      else
        raise ex
      end
    end
  end

  def full_url(api_url)
    "/api/v1/#{api_url}"
  end

  def api_post_request(api_url, payload)
    @canvas_api.post(full_url(api_url), payload)
  end

  def api_get_request(api_url)
    @canvas_api.get(full_url(api_url))
  end

  def current_account
    api_get_request("accounts/self")
  end

  def course_accounts
    api_get_request("course_accounts")
  end

  def accounts
    api_get_request("accounts")
  end
  
  def create_subaccount(params)
    api_post_request("accounts/#{account_id}/sub_accounts", params)
  end

  def sub_accounts
    api_get_request("accounts/#{account_id}/sub_accounts")
  end

  def create_course(params, sub_account_id = nil)
    api_post_request("accounts/#{sub_account_id || account_id}/courses?enroll_me=true", params)
  end

  def get_courses_for_account(sub_account_id = nil, search_term = nil)
    url = "accounts/#{sub_account_id || account_id}/courses?per_page=#{@per_page}"
    url << "&search_term=#{ERB::Util.url_encode(search_term)}" if search_term.present?
    api_get_request(url)
  end

  def assignments(course_id)
    api_get_request("courses/#{course_id}/assignments")
  end

  def discussion_topics(course_id)
    api_get_request("courses/#{course_id}/discussion_topics")
  end

  def get_courses(user_id = nil)
    request = "courses"
    request << "?as_user_id=#{user_id}" if user_id
    api_get_request(request)
  end

  def list_users(sub_account_id = nil)
    api_get_request("accounts/#{sub_account_id || account_id}/users")
  end

  def recent_logins(course_id)
    api_get_request("courses/#{course_id}/recent_students")
  end

  def students(course_id)
    api_get_request("courses/#{course_id}/users?enrollment_type=student")
  end

  def enroll_user(course_id, params)
    api_post_request("courses/#{course_id}/enrollments", params)
{'fraction_non_alphanumeric': 0.10891089108910891, 'fraction_numerical': 0.0016501650165016502, 'mean_word_length': 4.358011049723757, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1962819', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1715, 'n_words': 270}