Class: Supply::Client

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

Constant Summary collapse

SERVICE =
AndroidPublisher::AndroidPublisherService
SCOPE =
AndroidPublisher::AUTH_ANDROIDPUBLISHER

Instance Attribute Summary collapse

Attributes inherited from AbstractGoogleServiceClient

#client

Login collapse

Handling the edit lifecycle collapse

Getting data collapse

Modifying data collapse

Screenshots collapse

Methods inherited from AbstractGoogleServiceClient

#initialize, make_from_config

Constructor Details

This class inherits a constructor from Supply::AbstractGoogleServiceClient

Instance Attribute Details

#current_editObject

Editing something Reference to the entry we’re currently editing. Might be nil if don’t have one open



112
113
114
# File 'supply/lib/supply/client.rb', line 112

def current_edit
  @current_edit
end

#current_package_nameObject

Package name of the currently edited element



114
115
116
# File 'supply/lib/supply/client.rb', line 114

def current_package_name
  @current_package_name
end

Class Method Details

.service_account_authentication(params: nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'supply/lib/supply/client.rb', line 120

def self.(params: nil)
  if params[:json_key] || params[:json_key_data]
    super(params: params)
  elsif params[:key] && params[:issuer]
    require 'google/api_client/auth/key_utils'
    UI.important("This type of authentication is deprecated. Please consider using JSON authentication instead")
    key = Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(params[:key]), 'notasecret')
    cred_json = {
      private_key: key.to_s,
      client_email: params[:issuer]
    }
     = StringIO.new(JSON.dump(cred_json))
    
  else
    UI.user_error!("No authentication parameters were specified. These must be provided in order to authenticate with Google")
  end
end

Instance Method Details

#aab_version_codesObject

Get a list of all AAB version codes - returns the list of version codes



261
262
263
264
265
266
267
# File 'supply/lib/supply/client.rb', line 261

def aab_version_codes
  ensure_active_edit!

  result = call_google_api { client.list_edit_bundles(current_package_name, current_edit.id) }

  return Array(result.bundles).map(&:version_code)
end

#abort_current_editObject

Aborts the current edit deleting all pending changes



152
153
154
155
156
157
158
159
# File 'supply/lib/supply/client.rb', line 152

def abort_current_edit
  ensure_active_edit!

  call_google_api { client.delete_edit(current_package_name, current_edit.id) }

  self.current_edit = nil
  self.current_package_name = nil
end

#apks_version_codesObject

Get a list of all APK version codes - returns the list of version codes



252
253
254
255
256
257
258
# File 'supply/lib/supply/client.rb', line 252

def apks_version_codes
  ensure_active_edit!

  result = call_google_api { client.list_edit_apks(current_package_name, current_edit.id) }

  return Array(result.apks).map(&:version_code)
end

#begin_edit(package_name: nil) ⇒ Object

Begin modifying a certain package



143
144
145
146
147
148
149
# File 'supply/lib/supply/client.rb', line 143

def begin_edit(package_name: nil)
  UI.user_error!("You currently have an active edit") if @current_edit

  self.current_edit = call_google_api { client.insert_edit(package_name) }

  self.current_package_name = package_name
end

#clear_screenshot(image_type: nil, language: nil, image_id: nil) ⇒ Object

Remove a specific screenshot of a given image_type and language from the store listing

Parameters:

  • image_type (String) (defaults to: nil)

    (e.g. phoneScreenshots, sevenInchScreenshots, …)

  • language (String) (defaults to: nil)

    localization code (i.e. BCP-47 language tag as in ‘pt-BR`)

  • image_id (String) (defaults to: nil)

    The id of the screenshot to remove (as per ‘ImageListing#id`)



610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'supply/lib/supply/client.rb', line 610

def clear_screenshot(image_type: nil, language: nil, image_id: nil)
  ensure_active_edit!

  call_google_api do
    client.delete_edit_image(
      current_package_name,
      current_edit.id,
      language,
      image_type,
      image_id
    )
  end
end

#clear_screenshots(image_type: nil, language: nil) ⇒ Object

Remove all screenshots of a given image_type and language from the store listing

Parameters:

  • image_type (String) (defaults to: nil)

    (e.g. phoneScreenshots, sevenInchScreenshots, …)

  • language (String) (defaults to: nil)

    localization code (i.e. BCP-47 language tag as in ‘pt-BR`)



591
592
593
594
595
596
597
598
599
600
601
602
# File 'supply/lib/supply/client.rb', line 591

def clear_screenshots(image_type: nil, language: nil)
  ensure_active_edit!

  call_google_api do
    client.deleteall_edit_image(
      current_package_name,
      current_edit.id,
      language,
      image_type
    )
  end
end

#commit_current_edit!Object

Commits the current edit saving all pending changes on Google Play



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'supply/lib/supply/client.rb', line 169

def commit_current_edit!
  ensure_active_edit!

  call_google_api do
    begin
      client.commit_edit(
        current_package_name,
        current_edit.id,
        changes_not_sent_for_review: Supply.config[:changes_not_sent_for_review]
      )
    rescue Google::Apis::ClientError => e
      unless Supply.config[:rescue_changes_not_sent_for_review]
        raise
      end

      error = begin
                JSON.parse(e.body)
              rescue
                nil
              end

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

      if message.include?("The query parameter changesNotSentForReview must not be set")
        client.commit_edit(
          current_package_name,
          current_edit.id
        )
      elsif message.include?("Please set the query parameter changesNotSentForReview to true")
        client.commit_edit(
          current_package_name,
          current_edit.id,
          changes_not_sent_for_review: true
        )
      else
        raise
      end
    end
  end

  self.current_edit = nil
  self.current_package_name = nil
end

#download_generated_universal_apk(generated_universal_apk:, destination:) ⇒ Object

Download a Universal APK generated by Google for a particular version code

Parameters:

  • generated_universal_apk (Supply::GeneratedUniversalApk)

    The GeneratedUniversalApk object retrieved from a call to ‘list_generated_univeral_apks`

  • destination (IO, String)

    IO stream or filename to receive content download



337
338
339
340
341
342
343
344
345
346
# File 'supply/lib/supply/client.rb', line 337

def download_generated_universal_apk(generated_universal_apk:, destination:)
  call_google_api {
    client.download_generatedapk(
      generated_universal_apk.package_name,
      generated_universal_apk.version_code,
      generated_universal_apk.download_id,
      download_dest: destination
    )
  }
end

#fetch_images(image_type: nil, language: nil) ⇒ Array<ImageListing>

Fetch all the images of a given type and for a given language of your store listing

Parameters:

  • image_type (String) (defaults to: nil)

    Typically one of the elements of either Supply::IMAGES_TYPES or Supply::SCREENSHOT_TYPES

  • language (String) (defaults to: nil)

    Localization code (a BCP-47 language tag; for example, “de-AT” for Austrian German).

Returns:

  • (Array<ImageListing>)

    A list of ImageListing instances describing each image with its id, sha256 and url



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'supply/lib/supply/client.rb', line 548

def fetch_images(image_type: nil, language: nil)
  ensure_active_edit!

  result = call_google_api do
    client.list_edit_images(
      current_package_name,
      current_edit.id,
      language,
      image_type
    )
  end

  (result.images || []).map do |row|
    full_url = "#{row.url}=s0" # '=s0' param ensures full image size is returned (https://github.com/fastlane/fastlane/pull/14322#issuecomment-473012462)
    ImageListing.new(row.id, row.sha1, row.sha256, full_url)
  end
end

#latest_version(track) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
# File 'supply/lib/supply/client.rb', line 302

def latest_version(track)
  latest_version = tracks.select { |t| t.track == Supply::Tracks::DEFAULT }.map(&:releases).flatten.reject { |r| r.name.nil? }.max_by(&:name)

  # Check if user specified '--track' option if version information from 'production' track is nil
  if latest_version.nil? && track == Supply::Tracks::DEFAULT
    UI.user_error!(%(Unable to find latest version information from "#{Supply::Tracks::DEFAULT}" track. Please specify track information by using the '--track' option.))
  else
    latest_version = tracks.select { |t| t.track == track }.map(&:releases).flatten.reject { |r| r.name.nil? }.max_by(&:name)
  end

  return latest_version
end

#list_generated_universal_apks(package_name:, version_code:) ⇒ Array<GeneratedUniversalApk>

Get the list of Universal APKs generated by Google from the AAB for a particular version code

Parameters:

  • version_code (Fixnum)

    Version code of the app bundle.

Returns:

Raises:

  • (FastlaneError)

    (Google Api Error) If no APK was found for the provided ‘package_name` and `version_code`



322
323
324
325
326
327
328
# File 'supply/lib/supply/client.rb', line 322

def list_generated_universal_apks(package_name:, version_code:)
  result = call_google_api { client.list_generatedapks(package_name, version_code) }

  result.generated_apks.map do |row|
    GeneratedUniversalApk.new(package_name, version_code, row.certificate_sha256_hash, row.generated_universal_apk.download_id)
  end
end

#listing_for_language(language) ⇒ Object

Returns the listing for the given language filled with the current values if it already exists



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'supply/lib/supply/client.rb', line 234

def listing_for_language(language)
  ensure_active_edit!

  begin
    result = client.get_edit_listing(
      current_package_name,
      current_edit.id,
      language
    )

    return Listing.new(self, language, result)
  rescue Google::Apis::ClientError => e
    return Listing.new(self, language) if e.status_code == 404 # create a new empty listing
    raise
  end
end

#listingsObject

Get a list of all languages - returns the list make sure to have an active edit



223
224
225
226
227
228
229
230
231
# File 'supply/lib/supply/client.rb', line 223

def listings
  ensure_active_edit!

  result = call_google_api { client.list_edit_listings(current_package_name, current_edit.id) }

  return result.listings.map do |row|
    Listing.new(self, row.language, row)
  end
end

#release_listings(version) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'supply/lib/supply/client.rb', line 269

def release_listings(version)
  ensure_active_edit!

  # Verify that tracks have releases
  filtered_tracks = tracks.select { |t| !t.releases.nil? && t.releases.any? { |r| r.name == version } }

  if filtered_tracks.length > 1
    # Prefer tracks in production, beta, alpha, internal order
    # E.g.: A release might've been promoted from Alpha/Beta track. This means the release will be present in two or more tracks
    filtered_tracks = filtered_tracks.sort_by { |t| Supply::Tracks::DEFAULTS.index(t.track) || Float::INFINITY }
  end

  filtered_track = filtered_tracks.first
  if filtered_track.nil?
    UI.user_error!("Unable to find version '#{version}' for '#{current_package_name}' in all tracks. Please double check the version number.")
    return nil
  else
    UI.message("Found '#{version}' in '#{filtered_track.track}' track.")
  end

  filtered_release = filtered_track.releases.first { |r| !r.name.nil? && r.name == version }

  # Since we can release on Internal/Alpha/Beta without release notes.
  if filtered_release.release_notes.nil?
    UI.message("Version '#{version}' for '#{current_package_name}' does not seem to have any release notes. Nothing to download.")
    return []
  end

  return filtered_release.release_notes.map do |row|
    Supply::ReleaseListing.new(filtered_track, filtered_release.name, filtered_release.version_codes, row.language, row.text)
  end
end

#track_releases(track) ⇒ Object

Get list of release names for track



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'supply/lib/supply/client.rb', line 492

def track_releases(track)
  ensure_active_edit!

  begin
    result = client.get_edit_track(
      current_package_name,
      current_edit.id,
      track
    )
    return result.releases || []
  rescue Google::Apis::ClientError => e
    return [] if e.status_code == 404 && e.to_s.include?("trackEmpty")
    raise
  end
end

#track_version_codes(track) ⇒ Object

Get list of version codes for track



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'supply/lib/supply/client.rb', line 475

def track_version_codes(track)
  ensure_active_edit!

  begin
    result = client.get_edit_track(
      current_package_name,
      current_edit.id,
      track
    )
    return result.releases.flat_map(&:version_codes) || []
  rescue Google::Apis::ClientError => e
    return [] if e.status_code == 404 && (e.to_s.include?("trackEmpty") || e.to_s.include?("Track not found"))
    raise
  end
end

#tracks(*tracknames) ⇒ Object

Get a list of all tracks - returns the list



448
449
450
451
452
453
454
455
456
457
458
459
# File 'supply/lib/supply/client.rb', line 448

def tracks(*tracknames)
  ensure_active_edit!

  all_tracks = call_google_api { client.list_edit_tracks(current_package_name, current_edit.id) }.tracks
  all_tracks = [] unless all_tracks

  if tracknames.length > 0
    all_tracks = all_tracks.select { |track| tracknames.include?(track.track) }
  end

  return all_tracks
end

#update_listing_for_language(language: nil, title: nil, short_description: nil, full_description: nil, video: nil) ⇒ Object

Updates or creates the listing for the specified language



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'supply/lib/supply/client.rb', line 353

def update_listing_for_language(language: nil, title: nil, short_description: nil, full_description: nil, video: nil)
  ensure_active_edit!

  listing = AndroidPublisher::Listing.new(
    language: language,
    title: title,
    full_description: full_description,
    short_description: short_description,
    video: video
  )

  call_google_api do
    client.update_edit_listing(
      current_package_name,
      current_edit.id,
      language,
      listing
    )
  end
end

#update_obb(apk_version_code, expansion_file_type, references_version, file_size) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'supply/lib/supply/client.rb', line 521

def update_obb(apk_version_code, expansion_file_type, references_version, file_size)
  ensure_active_edit!

  call_google_api do
    client.update_edit_expansionfile(
      current_package_name,
      current_edit.id,
      apk_version_code,
      expansion_file_type,
      AndroidPublisher::ExpansionFile.new(
        references_version: references_version,
        file_size: file_size
      )
    )
  end
end

#update_track(track_name, track_object) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
# File 'supply/lib/supply/client.rb', line 461

def update_track(track_name, track_object)
  ensure_active_edit!

  call_google_api do
    client.update_edit_track(
      current_package_name,
      current_edit.id,
      track_name,
      track_object
    )
  end
end

#upload_apk(path_to_apk) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'supply/lib/supply/client.rb', line 374

def upload_apk(path_to_apk)
  ensure_active_edit!

  result_upload = call_google_api do
    client.upload_edit_apk(
      current_package_name,
      current_edit.id,
      upload_source: path_to_apk
    )
  end

  return result_upload.version_code
end

#upload_apk_to_internal_app_sharing(package_name, path_to_apk) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
# File 'supply/lib/supply/client.rb', line 388

def upload_apk_to_internal_app_sharing(package_name, path_to_apk)
  # NOTE: This Google API is a little different. It doesn't require an active edit.
  result_upload = call_google_api do
    client.uploadapk_internalappsharingartifact(
      package_name,
      upload_source: path_to_apk,
      content_type: "application/octet-stream"
    )
  end

  return result_upload.download_url
end

#upload_bundle(path_to_aab) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'supply/lib/supply/client.rb', line 418

def upload_bundle(path_to_aab)
  ensure_active_edit!

  result_upload = call_google_api do
    client.upload_edit_bundle(
      current_package_name,
      self.current_edit.id,
      upload_source: path_to_aab,
      content_type: "application/octet-stream",
      ack_bundle_installation_warning: Supply.config[:ack_bundle_installation_warning]
    )
  end

  return result_upload.version_code
end

#upload_bundle_to_internal_app_sharing(package_name, path_to_aab) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
# File 'supply/lib/supply/client.rb', line 434

def upload_bundle_to_internal_app_sharing(package_name, path_to_aab)
  # NOTE: This Google API is a little different. It doesn't require an active edit.
  result_upload = call_google_api do
    client.uploadbundle_internalappsharingartifact(
      package_name,
      upload_source: path_to_aab,
      content_type: "application/octet-stream"
    )
  end

  return result_upload.download_url
end

#upload_changelogs(track, track_name) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
# File 'supply/lib/supply/client.rb', line 508

def upload_changelogs(track, track_name)
  ensure_active_edit!

  call_google_api do
    client.update_edit_track(
      current_package_name,
      self.current_edit.id,
      track_name,
      track
    )
  end
end

#upload_image(image_path: nil, image_type: nil, language: nil) ⇒ Object

Upload an image or screenshot of a specific type for a given language to your store listing

Parameters:

  • image_type (String) (defaults to: nil)

    (e.g. phoneScreenshots, sevenInchScreenshots, …)

  • language (String) (defaults to: nil)

    localization code (i.e. BCP-47 language tag as in ‘pt-BR`)



571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'supply/lib/supply/client.rb', line 571

def upload_image(image_path: nil, image_type: nil, language: nil)
  ensure_active_edit!

  call_google_api do
    client.upload_edit_image(
      current_package_name,
      current_edit.id,
      language,
      image_type,
      upload_source: image_path,
      content_type: 'image/*'
    )
  end
end

#upload_mapping(path_to_mapping, apk_version_code) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'supply/lib/supply/client.rb', line 401

def upload_mapping(path_to_mapping, apk_version_code)
  ensure_active_edit!

  extension = File.extname(path_to_mapping).downcase

  call_google_api do
    client.upload_edit_deobfuscationfile(
      current_package_name,
      current_edit.id,
      apk_version_code,
      extension == ".zip" ? "nativeCode" : "proguard",
      upload_source: path_to_mapping,
      content_type: "application/octet-stream"
    )
  end
end

#upload_obb(obb_file_path: nil, apk_version_code: nil, expansion_file_type: nil) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'supply/lib/supply/client.rb', line 624

def upload_obb(obb_file_path: nil, apk_version_code: nil, expansion_file_type: nil)
  ensure_active_edit!

  call_google_api do
    client.upload_edit_expansionfile(
      current_package_name,
      current_edit.id,
      apk_version_code,
      expansion_file_type,
      upload_source: obb_file_path,
      content_type: 'application/octet-stream'
    )
  end
end

#validate_current_edit!Object

Validates the current edit - does not change data on Google Play



162
163
164
165
166
# File 'supply/lib/supply/client.rb', line 162

def validate_current_edit!
  ensure_active_edit!

  call_google_api { client.validate_edit(current_package_name, current_edit.id) }
end