<commit_before>class Canvas

  def initialize(canvas_uri, canvas_api_key, refresh_token_options=nil)
    @per_page = 100
    @canvas_uri = UrlHelper.scheme_host(canvas_uri)
    @canvas_api_key = canvas_api_key
    @refresh_token_options = refresh_token_options
  end

  def headers
    {
      "Authorization" => "Bearer #{@canvas_api_key}",
      "content_type" => "json",
      'User-Agent' => "CanvasAPI Ruby"
    }
  end

  def full_url(api_url, use_api_prefix=true)
    if api_url[0...4] == 'http'
      api_url
    else
      if use_api_prefix
        "#{@canvas_uri}/api/v1/#{api_url}"
      else
        "#{@canvas_uri}/#{api_url}"
      end
    end
  end

  def api_put_request(api_url, payload)
    @method = 'PUT'
    @api_url = api_url
    @payload = payload
    check_result(HTTParty.put(full_url(api_url), :headers => headers, :body => payload))
  end

  def api_post_request(api_url, payload, use_api_prefix=true)
    @method = 'POST'
    @api_url = api_url
    @payload = payload
    result = HTTParty.post(full_url(api_url, use_api_prefix), :headers => headers, :body => payload)
    check_result(result)
  end

  def api_get_request(api_url)
    @method = 'GET'
    @api_url = api_url
    check_result(HTTParty.get(full_url(api_url), :headers => headers))
  end

  def refresh_token
    result = api_post_request("login/oauth2/token", {
      grant_type: 'refresh_token',
      client_id: @refresh_token_options[:client_id],
      client_secret: @refresh_token_options[:client_secret],
      refresh_token: @refresh_token_options[:refresh_token]
    }, false)
    if result
      @canvas_api_key = result['access_token']
      @refresh_token_options[:auth].update_attribute(:token, @canvas_api_key)
      repeat_request_once
    end
  end

  def repeat_request_once
    case @method
      when 'GET'<commit_msg>Don't store off the request if we are making the token refresh request<commit_after>class Canvas

  def initialize(canvas_uri, canvas_api_key, refresh_token_options=nil)
    @per_page = 100
    @canvas_uri = UrlHelper.scheme_host(canvas_uri)
    @canvas_api_key = canvas_api_key
    @refresh_token_options = refresh_token_options
  end

  def headers
    {
      "Authorization" => "Bearer #{@canvas_api_key}",
      "content_type" => "json",
      'User-Agent' => "CanvasAPI Ruby"
    }
  end

  def full_url(api_url, use_api_prefix=true)
    if api_url[0...4] == 'http'
      api_url
    else
      if use_api_prefix
        "#{@canvas_uri}/api/v1/#{api_url}"
      else
        "#{@canvas_uri}/#{api_url}"
      end
    end
  end

  def api_put_request(api_url, payload)
    @method = 'PUT'
    @api_url = api_url
    @payload = payload
    check_result(HTTParty.put(full_url(api_url), :headers => headers, :body => payload))
  end

  def api_post_request(api_url, payload, use_api_prefix=true)
    unless @refreshing_token
      @method = 'POST'
      @api_url = api_url
      @payload = payload
    end
    result = HTTParty.post(full_url(api_url, use_api_prefix), :headers => headers, :body => payload)
    check_result(result)
  end

  def api_get_request(api_url)
    @method = 'GET'
    @api_url = api_url
    check_result(HTTParty.get(full_url(api_url), :headers => headers))
  end

  def refresh_token
    result = api_post_request("login/oauth2/token", {
      grant_type: 'refresh_token',
      client_id: @refresh_token_options[:client_id],
      client_secret: @refresh_token_options[:client_secret],
      refresh_token: @refresh_token_options[:refresh_token]
    }, false)
    if result
      @canvas_api_key = result['access_token']
      @refresh_token_options[:auth].update_attribute(:token, @canvas_api_key)
      repeat_request_once
    end
  end

  def repeat_request_once
    case @method
      when 'GET'
{'fraction_non_alphanumeric': 0.11069717935071846, 'fraction_numerical': 0.0037253858435337944, 'mean_word_length': 3.4856801909307875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '5164863', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1295, 'n_words': 242}