<commit_before># OAUTH2
# http://tools.ietf.org/html/rfc6749#section-1.2
# https://canvas.instructure.com/doc/api/file.oauth.html

require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

require 'json'

Dotenv.load

$last_url = ""

class URLCapture < Faraday::Middleware

  def initialize(app, options = {})
    super(app)
  end

  def call(env)
    $last_url = env[:url]
    @app.call(env)
  end
end

enable :sessions


helpers do
  def h(text)
    Rack::Utils.escape_html(text)
  end
end

def log_faraday_ex
  yield
rescue Faraday::Error => e
  puts "ERROR: #$last_url"
  p e.response
  raise
end

$conn = Faraday.new(url: "http://#{ENV['API_HOST']}:#{ENV['API_PORT']}/") do |c|
  c.use Faraday::Request::UrlEncoded
  c.use Faraday::Response::Logger
  c.use URLCapture
  c.use Faraday::Response::RaiseError
  c.adapter :net_http
end

get '/' do
  response_type = "code"
  redirect_uri = url("/grant")
  state = "XXXXX" # FIXME unique ID saved in state, check
                  # session[state] == request[state]

  # request authorization grant
  url = "http://#{ENV['API_HOST']}:#{ENV['API_PORT']}/login/oauth2/auth"
  url << "?client_id=#{ENV['API_CLIENT_ID']}"
  url << "&response_type=#{response_type}"
  url << "&redirect_uri=#{redirect_uri}"
  url << "&state=#{state}"
  redirect url
end

get '/grant' do
  code = request["code"]
  state = request["state"]
  redirect_uri = url("/token")

  res = log_faraday_ex do
    $conn.post "/login/oauth2/token", {
                 client_id: ENV['API_CLIENT_ID'],
                 client_secret: ENV['API_CLIENT_KEY'],
                 redirect_uri: redirect_uri,
                 code: code,
               }
  end
  res = JSON.parse res.body
  p res
  session['access_token'] = res['access_token']

  redirect '/courses'
end

get '/courses' do
  @session = session

  p "TOKEN: ", session

  log_faraday_ex do
    res = $conn.get "/api/v1/accounts/1/courses", {published: true} do |req|
      req.headers['Authorization'] = "Bearer #{session['access_token']}"
    end
    @courses_url = $last_url
    @courses = JSON.parse res.body

    res = $conn.get "/api/v1/courses/1", {'include[]' => 'syllabus_body'} do |req|
      req.headers['Authorization'] = "Bearer #{session['access_token']}"
    end
    @course_1_url = $last_url
    @course_1 = JSON.parse res.body<commit_msg>Update poc<commit_after># OAUTH2
# http://tools.ietf.org/html/rfc6749#section-1.2
# https://canvas.instructure.com/doc/api/file.oauth.html

require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

require 'json'

Dotenv.load

$last_url = ""

class URLCapture < Faraday::Middleware

  def initialize(app, options = {})
    super(app)
  end

  def call(env)
    $last_url = env[:url]
    @app.call(env)
  end
end

enable :sessions


helpers do
  def h(text)
    Rack::Utils.escape_html(text)
  end
end

def log_faraday_ex
  yield
rescue Faraday::Error => e
  puts "ERROR: #$last_url"
  p e.response
  raise
end

$conn = Faraday.new(url: "http://#{ENV['API_HOST']}:#{ENV['API_PORT']}/") do |c|
  c.use Faraday::Request::UrlEncoded
  c.use Faraday::Response::Logger
  c.use URLCapture
  c.use Faraday::Response::RaiseError
  c.adapter :net_http
end

get '/' do
  response_type = "code"
  redirect_uri = url("/grant")
  state = "XXXXX" # FIXME unique ID saved in state, check
                  # session[state] == request[state]

  # request authorization grant
  url = "http://#{ENV['API_HOST']}:#{ENV['API_PORT']}/login/oauth2/auth"
  url << "?client_id=#{ENV['API_CLIENT_ID']}"
  url << "&response_type=#{response_type}"
  url << "&redirect_uri=#{redirect_uri}"
  url << "&state=#{state}"
  redirect url
end

get '/grant' do
  code = request["code"]
  state = request["state"]
  redirect_uri = url("/token")

  res = log_faraday_ex do
    $conn.post "/login/oauth2/token", {
                 client_id: ENV['API_CLIENT_ID'],
                 client_secret: ENV['API_CLIENT_KEY'],
                 redirect_uri: redirect_uri,
                 code: code,
               }
  end
  res = JSON.parse res.body
  puts "TOKEN: ", session['access_token']
  session['access_token'] = res['access_token']

  redirect '/courses'
end

get '/courses' do
  @session = session

  log_faraday_ex do
    res = $conn.get "/api/v1/accounts/1/courses", {published: true} do |req|
      req.headers['Authorization'] = "Bearer #{session['access_token']}"
    end
    @courses_url = $last_url
    @courses = JSON.parse res.body

    res = $conn.get "/api/v1/courses/1", {'include[]' => 'syllabus_body'} do |req|
      req.headers['Authorization'] = "Bearer #{session['access_token']}"
    end
    @course_1_url = $last_url
    @course_1 = JSON.parse res.body
{'fraction_non_alphanumeric': 0.15880469583778015, 'fraction_numerical': 0.0064034151547491995, 'mean_word_length': 3.3429101019462464, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1136586', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1702, 'n_words': 346}