class Supply::AbstractGoogleServiceClient

Constants

SCOPE
SERVICE

Attributes

client[RW]

Connecting with Google

Public Class Methods

make_from_config(params: nil) click to toggle source
# File supply/lib/supply/client.rb, line 16
def self.make_from_config(params: nil)
  params ||= Supply.config
  service_account_data = self.service_account_authentication(params: params)
  return self.new(service_account_json: service_account_data, params: params)
end
new(service_account_json: nil, params: nil) click to toggle source

Initializes the service and its auth_client using the specified information @param service_account_json: The raw service account Json data

# File supply/lib/supply/client.rb, line 48
def initialize(service_account_json: nil, params: nil)
  auth_client = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: service_account_json, scope: self.class::SCOPE)

  UI.verbose("Fetching a new access token from Google...")

  auth_client.fetch_access_token!

  if FastlaneCore::Env.truthy?("DEBUG")
    Google::Apis.logger.level = Logger::DEBUG
  end

  Google::Apis::ClientOptions.default.application_name = "fastlane (supply client)"
  Google::Apis::ClientOptions.default.application_version = Fastlane::VERSION
  Google::Apis::ClientOptions.default.read_timeout_sec = params[:timeout]
  Google::Apis::ClientOptions.default.open_timeout_sec = params[:timeout]
  Google::Apis::ClientOptions.default.send_timeout_sec = params[:timeout]
  Google::Apis::RequestOptions.default.retries = 5

  service = self.class::SERVICE.new
  service.authorization = auth_client

  if params[:root_url]
    # Google's client expects the root_url string to end with "/".
    params[:root_url] << '/' unless params[:root_url].end_with?('/')
    service.root_url = params[:root_url]
  end

  self.client = service
end
service_account_authentication(params: nil) click to toggle source

Supply authentication file

# File supply/lib/supply/client.rb, line 23
def self.service_account_authentication(params: nil)
  unless params[:json_key] || params[:json_key_data]
    if UI.interactive?
      UI.important("To not be asked about this value, you can specify it using 'json_key'")
      json_key_path = UI.input("The service account json file used to authenticate with Google: ")
      json_key_path = File.expand_path(json_key_path)

      UI.user_error!("Could not find service account json file at path '#{json_key_path}'") unless File.exist?(json_key_path)
      params[:json_key] = json_key_path
    else
      UI.user_error!("Could not load Google authentication. Make sure it has been added as an environment variable in 'json_key' or 'json_key_data'")
    end
  end

  if params[:json_key]
    service_account_json = File.open(File.expand_path(params[:json_key]))
  elsif params[:json_key_data]
    service_account_json = StringIO.new(params[:json_key_data])
  end

  service_account_json
end

Private Instance Methods

call_google_api() { || ... } click to toggle source
# File supply/lib/supply/client.rb, line 80
def call_google_api
  yield if block_given?
rescue Google::Apis::Error => e
  error = begin
            JSON.parse(e.body)
          rescue
            nil
          end

  if error
    message = error["error"] && error["error"]["message"]
  else
    message = e.body
  end

  UI.user_error!("Google Api Error: #{e.message} - #{message}")
end