<commit_before><commit_msg>test2.rb<commit_after>require 'rubygems'
require 'ap' # awesome_print gem
require 'base64'
require 'hmac-sha2' # ruby-hmac gem
require 'rest_client' # rest-client gem

#vars
$hostname = 'wiutest.desire2learn.com'
$user_id = 'sJiaLZAl41bHfa_fnyfyAp'
$user_key = '<KEY>'
$app_id = 'ysuBVIqmRDaJpnVEOb8Ylg'
$app_key = '<KEY>'
path = '/d2l/api/lp/1.0/users/whoami'
httpmethod = 'GET'
$timestamp = Time.now.to_i + 3600 #current hour plus one hour

def build_authenticated_uri_query_string(signature, timestamp)
  "?x_a=#{$app_id}&x_b=#{$user_id}&x_c=#{get_base64_hash_string($app_key, signature)}&x_d=#{get_base64_hash_string($user_key, signature)}&x_t=#{timestamp}"
end

def authenticate_uri(path, httpmethod)
  query_string = get_query_string(path, httpmethod)
  ap "https://#{$hostname}#{$path}#{query_string}"
end

def format_signature(path, httpmethod, timestamp)
  "#{httpmethod.upcase}&#{path.downcase}&#{$timestamp}"
end

def get_base64_hash_string(key, signature)
  sha256_string = HMAC::SHA256.hexdigest(signature, key)
  sha256_string2 = HMAC::SHA256.hexdigest(key, signature)
  ap "sig first:  #{sha256_string}"
  ap "key first:  #{sha256_string2}"
  urlsafe_base64_string = Base64.urlsafe_encode64(sha256_string2).gsub('=', '')
end

def get_query_string(path, httpmethod)
  signature = format_signature(path, httpmethod, $timestamp)
  query_string = build_authenticated_uri_query_string(signature, $timestamp)
end

the_uri = authenticate_uri(path, httpmethod)

puts "THE_URI:  #{the_uri}"

RestClient.get(the_uri){ |response, request, result, &block|
  case response.code
  when 200
    p 'It worked !'
    ap response
  when 423
    fail SomeCustomExceptionIfYouWant
  when 403
    ap response
  else
    response.return!(request, result, &block)
  end
}

exit
{'fraction_non_alphanumeric': 0.133630289532294, 'fraction_numerical': 0.0334075723830735, 'mean_word_length': 6.45643153526971, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '4678233', 'n_tokens_mistral': 720, 'n_tokens_neox': 682, 'n_words': 126}