<commit_before>require 'json'
require 'cgi'
require 'net/http' # also requires uri

class Fastly
  # The UserAgent to communicate with the API
  class Client #:nodoc: all
    attr_accessor :http, :api_key, :user, :password, :cookie, :customer

    def initialize(opts)
      @api_key  = opts.fetch(:api_key, nil)
      @user     = opts.fetch(:user, nil)
      @password = opts.fetch(:password, nil)
      @customer = opts.fetch(:customer, nil)

      base      = opts.fetch(:base_url, 'https://api.fastly.com')
      uri       = URI.parse(base)
      @http     = Net::HTTP.start(uri.host, uri.port, use_ssl: true)

      return self unless fully_authed?

      # If full auth creds (user/pass) then log in and set a cookie
      resp = http.post('/login', make_params(user: user, password: password))

      if resp.kind_of?(Net::HTTPSuccess)
        @cookie = resp['Set-Cookie']
      else
        fail Unauthorized, "Invalid auth credentials. Check username/password."
      end

      self
    end

    def require_key!
      raise Fastly::KeyAuthRequired.new("This request requires an API key") if api_key.nil?
      @require_key = true
    end

    def require_key?
      !!@require_key
    end

    def authed?
      !api_key.nil? || fully_authed?
    end

    # Some methods require full username and password rather than just auth token
    def fully_authed?
      !(user.nil? || password.nil?)
    end

    def get(path, params = {})
      path += "?#{make_params(params)}" unless params.empty?
      resp  = http.get(path, headers)
      fail Error, resp.message unless resp.kind_of?(Net::HTTPSuccess)
      JSON.parse(resp.body)
    end

    def get_stats(path, params = {})
      resp = get(path, params)

      # return meta data, not just the actual stats data
      if resp['status'] == 'success'
        resp
      else
        fail Error, resp['msg']
      end
    end

    def post(path, params = {})
      post_and_put(:post, path, params)
    end
<commit_msg>Better error reporting<commit_after>require 'json'
require 'cgi'
require 'net/http' # also requires uri

class Fastly
  # The UserAgent to communicate with the API
  class Client #:nodoc: all
    attr_accessor :http, :api_key, :user, :password, :cookie, :customer

    def initialize(opts)
      @api_key  = opts.fetch(:api_key, nil)
      @user     = opts.fetch(:user, nil)
      @password = opts.fetch(:password, nil)
      @customer = opts.fetch(:customer, nil)

      base      = opts.fetch(:base_url, 'https://api.fastly.com')
      uri       = URI.parse(base)
      @http     = Net::HTTP.start(uri.host, uri.port, use_ssl: true)

      return self unless fully_authed?

      # If full auth creds (user/pass) then log in and set a cookie
      resp = http.post('/login', make_params(user: user, password: password))

      if resp.kind_of?(Net::HTTPSuccess)
        @cookie = resp['Set-Cookie']
      else
        fail Unauthorized, "Invalid auth credentials. Check username/password."
      end

      self
    end

    def require_key!
      raise Fastly::KeyAuthRequired.new("This request requires an API key") if api_key.nil?
      @require_key = true
    end

    def require_key?
      !!@require_key
    end

    def authed?
      !api_key.nil? || fully_authed?
    end

    # Some methods require full username and password rather than just auth token
    def fully_authed?
      !(user.nil? || password.nil?)
    end

    def get(path, params = {})
      path += "?#{make_params(params)}" unless params.empty?
      resp  = http.get(path, headers)
      fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
      JSON.parse(resp.body)
    end

    def get_stats(path, params = {})
      resp = get(path, params)

      # return meta data, not just the actual stats data
      if resp['status'] == 'success'
        resp
      else
        fail Error, resp['msg']
      end
    end

    def post(path, params = {})
      post_and_put(:post, path, params)
    end

{'fraction_non_alphanumeric': 0.11453077699293643, 'fraction_numerical': 0.0, 'mean_word_length': 2.549686660698299, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4113818', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1226, 'n_words': 360}