<commit_before>require 'google/api_client'

class GoogleIntegration::Client

  def initialize(user, api_client = nil, token_refresher = nil)
    @user            = user
    @api_client      = api_client || Google::APIClient
    @token_refresher = token_refresher || GoogleIntegration::RefreshAccessToken
  end

  def create
    client.authorization.access_token = access_token
    client
  end

  private

  def access_token
    verified_credentials.access_token
  end

  def verified_credentials
    @verified_credentials ||= @token_refresher.new(@user).refresh
  end

  def client
    @client ||= @api_client.new(application_name: 'quill')
  end
end<commit_msg>cherry pick google auth fix<commit_after>require 'google/api_client'

class GoogleIntegration::Client

  def initialize(user, api_client = nil, token_refresher = nil)
    @user            = user
    @api_client      = api_client || Google::APIClient
    @token_refresher = token_refresher || GoogleIntegration::RefreshAccessToken
  end

  def create
    client.authorization.access_token = access_token
    client
  end

  private

  def access_token
    verified_credentials.access_token
  end

  def verified_credentials
    @verified_credentials ||= @token_refresher.new(@user).refresh
  end

  def client
    @client ||= @api_client.new(application_name: 'quill', user_agent: user_agent)
  end

  # Builds a custom user agent to prevent Google::APIClient to
  # use an invalid auto-generated one
  # @see https://github.com/google/google-api-ruby-client/blob/15853007bf1fc8ad000bb35dafdd3ca6bfa8ae26/lib/google/api_client.rb#L112
  def user_agent
    [
      "quill/0.0.0",
      "google-api-ruby-client/#{Google::APIClient::VERSION::STRING}",
      Google::APIClient::ENV::OS_VERSION,
      '(gzip)'
    ].join(' ').delete("\n")
  end
end
{'fraction_non_alphanumeric': 0.09800664451827243, 'fraction_numerical': 0.014396456256921373, 'mean_word_length': 3.8315508021390374, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4330858', 'n_tokens_mistral': 602, 'n_tokens_neox': 587, 'n_words': 114}