Class: Supply::AbstractGoogleServiceClient

Inherits:
Object
  • Object
show all
Defined in:
supply/lib/supply/client.rb

Direct Known Subclasses

PlaycustomappClient, Client

Constant Summary collapse

SCOPE =
nil
SERVICE =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_account_json: nil, auth_client: nil, params: nil) ⇒ AbstractGoogleServiceClient

Initializes the service and its auth_client using the specified information

Parameters:

  • service_account_json: (defaults to: nil)

    The raw service account Json data

  • auth_client: (defaults to: nil)

    A pre-built auth client (e.g. from Application Default Credentials discovery)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'supply/lib/supply/client.rb', line 59

def initialize(service_account_json: nil, auth_client: nil, params: nil)
  if auth_client.nil?
    # decode the json and check its type
    begin
      json_content = .read
      google_credentials = JSON.parse(json_content)
      .rewind
    rescue JSON::ParserError
      UI.user_error!("Invalid Google Credentials file provided - unable to parse json.")
    end

    # use correct credential class based on type
    case google_credentials['type']
    when "authorized_user"
      auth_client = Google::Auth::UserRefreshCredentials.make_creds(json_key_io: , scope: self.class::SCOPE)
    when "external_account"
      auth_client = Google::Auth::ExternalAccount::Credentials.make_creds(json_key_io: , scope: self.class::SCOPE)
    when "service_account"
      auth_client = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: , scope: self.class::SCOPE)
    else
      UI.user_error!("Invalid Google Credentials file provided - no credential type found.")
    end
  end

  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

Instance Attribute Details

#clientObject

Connecting with Google



14
15
16
# File 'supply/lib/supply/client.rb', line 14

def client
  @client
end

Class Method Details

.make_from_config(params: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'supply/lib/supply/client.rb', line 16

def self.make_from_config(params: nil)
  params ||= Supply.config
   = self.(params: params)

  if 
    return self.new(service_account_json: , params: params)
  end

  # No explicit credentials — try Application Default Credentials discovery
  # (GOOGLE_APPLICATION_CREDENTIALS env var, ~/.config/gcloud/application_default_credentials.json, or GCE metadata)
  UI.verbose("No json_key provided, attempting Application Default Credentials...")
  begin
    auth_client = Google::Auth.get_application_default(self::SCOPE)
    return self.new(auth_client: auth_client, params: params)
  rescue RuntimeError => e
    UI.verbose("Application Default Credentials not available: #{e.message}")
  end

  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
    return self.new(service_account_json: File.open(json_key_path), params: params)
  else
    UI.user_error!("Could not load Google credentials. Provide 'json_key'/'json_key_data', set GOOGLE_APPLICATION_CREDENTIALS, or run: gcloud auth application-default login --scopes=openid,email,https://www.googleapis.com/auth/androidpublisher")
  end
end

.service_account_authentication(params: nil) ⇒ Object

Supply authentication file Returns an IO object for the credentials file, or nil if no explicit credentials were provided.



48
49
50
51
52
53
54
# File 'supply/lib/supply/client.rb', line 48

def self.(params: nil)
  if params[:json_key]
    File.open(File.expand_path(params[:json_key]))
  elsif params[:json_key_data]
    StringIO.new(params[:json_key_data])
  end
end