<commit_before>require 'httparty'
require 'json'

module GooderData

  class ApiClient
    include HTTParty

    BAD_REQUEST = 400
    NOT_FOUND = 404
    UNAUTHORIZED = 401
    STILL_PROCESSING = 202

    STATUS_WAIT = 'WAIT'

    def initialize(options = {})
      @super_secure_token = ""
      @temp_token = ""
      @options = GooderData.configuration.merge(options)
      self.class.base_uri(@options.require(:base_uri))
    end

    def super_secure_token=(super_secure_token)
      @super_secure_token = super_secure_token
    end

    def api_token=(api_token)
      @temp_token = api_token
    end

    def connect!(user = options[:user], password = options[:<PASSWORD>])
      login!(user, password)
      api_token!
      self
    end

    def login!(user, password)
      self.super_secure_token = login(user, password)
    end

    def login(user, password)
      api_to("login the user '#{ user }'", :no_validations) do
        response = post("/account/login", {
          postUserLogin: {
            login: user,
            password: password,
            remember: 1
          }
        })
        raise "wrong user or password for user #{ user }" if response.code == UNAUTHORIZED
        response
      end.that_responds do |response|
        extract_cookie(response, 'GDCAuthSST')
      end
    end

    def api_token!
      self.api_token = api_token
    end

    def api_token
      api_to("get api_token", :needs_login) do
        get("/account/token")
      end.that_responds do |response|
        extract_cookie(response, 'GDCAuthTT')
      end
    end

    def api_to(description, pre_validation = :needs_api_token)
      send(pre_validation)
      response = yield options
      validate(response, "could not #{ description }")
    end

    def retry_api_to(description, pre_validation = :needs_api_token, &api_call_block)
      response = nil
      (0..options.require(:max_retries)).each do
        response = api_to(description, pre_validation, &api_call_block)
        break unless processing?(response)
        sleep options.require(:retry_delay_in_seconds).to_f
      end
      response
    end

    def post(path, data)
      options = basic_options
      options[:headers]["Content-Type"] = "application/json"
      options[:body] = data.to_json
      send_request(:post, path, options)
    end

    def get(path)<commit_msg>Add retry log for deub<commit_after>require 'httparty'
require 'json'

module GooderData

  class ApiClient
    include HTTParty

    BAD_REQUEST = 400
    NOT_FOUND = 404
    UNAUTHORIZED = 401
    STILL_PROCESSING = 202

    STATUS_WAIT = 'WAIT'

    def initialize(options = {})
      @super_secure_token = ""
      @temp_token = ""
      @options = GooderData.configuration.merge(options)
      self.class.base_uri(@options.require(:base_uri))
    end

    def super_secure_token=(super_secure_token)
      @super_secure_token = super_secure_token
    end

    def api_token=(api_token)
      @temp_token = api_token
    end

    def connect!(user = options[:user], password = options[:user_password])
      login!(user, password)
      api_token!
      self
    end

    def login!(user, password)
      self.super_secure_token = login(user, password)
    end

    def login(user, password)
      api_to("login the user '#{ user }'", :no_validations) do
        response = post("/account/login", {
          postUserLogin: {
            login: user,
            password: password,
            remember: 1
          }
        })
        raise "wrong user or password for user #{ user }" if response.code == UNAUTHORIZED
        response
      end.that_responds do |response|
        extract_cookie(response, 'GDCAuthSST')
      end
    end

    def api_token!
      self.api_token = api_token
    end

    def api_token
      api_to("get api_token", :needs_login) do
        get("/account/token")
      end.that_responds do |response|
        extract_cookie(response, 'GDCAuthTT')
      end
    end

    def api_to(description, pre_validation = :needs_api_token)
      send(pre_validation)
      response = yield options
      validate(response, "could not #{ description }")
    end

    def retry_api_to(description, pre_validation = :needs_api_token, &api_call_block)
      response = nil
      (0..options.require(:max_retries)).each do
        response = api_to(description, pre_validation, &api_call_block)
        break unless processing?(response)
        puts "Retrying #{description}: #{response.code}: #{response.task_status}"
        sleep options.require(:retry_delay_in_seconds).to_f
      end
      response
    end

    def post(path, data)
      options = basic_options
      options[:headers]["Content-Type"] = "application/json"
      options[:body] = data.to_json
      send_request(:post, path, options)
    end

    def get(path)
{'fraction_non_alphanumeric': 0.09651307596513076, 'fraction_numerical': 0.005811540058115401, 'mean_word_length': 2.6070359281437128, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '4095942', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1444, 'n_words': 328}