class Supply::Client

Attributes

android_publisher[RW]

Connecting with Google

current_edit[RW]

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

current_package_name[RW]

Package name of the currently edited element

Public Class Methods

make_from_config() click to toggle source

instantiate a client given the supplied configuration

# File lib/supply/client.rb, line 24
def self.make_from_config
  unless Supply.config[:json_key] || (Supply.config[:key] && Supply.config[:issuer])
    UI.important("To not be asked about this value, you can specify it using 'json_key'")
    Supply.config[:json_key] = UI.input("The service account json file used to authenticate with Google: ")
  end

  return Client.new(path_to_key: Supply.config[:key],
                    issuer: Supply.config[:issuer],
                    path_to_service_account_json: Supply.config[:json_key])
end
new(path_to_key: nil, issuer: nil, path_to_service_account_json: nil) click to toggle source

Initializes the android_publisher and its auth_client using the specified information @param path_to_service_account_json: The path to your service account Json file @param path_to_key: The path to your p12 file (@deprecated) @param issuer: Email addresss for oauth (@deprecated)

# File lib/supply/client.rb, line 39
def initialize(path_to_key: nil, issuer: nil, path_to_service_account_json: nil)
  scope = Androidpublisher::AUTH_ANDROIDPUBLISHER

  if path_to_service_account_json
    key_io = File.open(File.expand_path(path_to_service_account_json))
  else
    require 'google/api_client/auth/key_utils'
    key = Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(path_to_key), 'notasecret')
    cred_json = {
      private_key: key.to_s,
      client_email: issuer
    }
    key_io = StringIO.new(MultiJson.dump(cred_json))
  end

  auth_client = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: key_io, scope: scope)

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

  auth_client.fetch_access_token!

  if ENV["DEBUG"]
    Google::Apis.logger.level = Logger::DEBUG
  end

  Google::Apis::ClientOptions.default.application_name = "fastlane - supply"
  Google::Apis::ClientOptions.default.application_version = Supply::VERSION
  Google::Apis::RequestOptions.default.timeout_sec = 300
  Google::Apis::RequestOptions.default.open_timeout_sec = 300
  Google::Apis::RequestOptions.default.retries = 5

  self.android_publisher = Androidpublisher::AndroidPublisherService.new
  self.android_publisher.authorization = auth_client
end

Public Instance Methods

abort_current_edit() click to toggle source

Aborts the current edit deleting all pending changes

# File lib/supply/client.rb, line 88
def abort_current_edit
  ensure_active_edit!

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

  self.current_edit = nil
  self.current_package_name = nil
end
apk_listings(apk_version_code) click to toggle source

Get a list of all apk listings (changelogs) - returns the list

# File lib/supply/client.rb, line 158
def apk_listings(apk_version_code)
  ensure_active_edit!

  result = call_google_api do
    android_publisher.list_apk_listings(
      current_package_name,
      current_edit.id,
      apk_version_code
    )
  end

  return (result.listings || []).map do |row|
    ApkListing.new(row.recent_changes, row.language, apk_version_code)
  end
end
apks_version_codes() click to toggle source

Get a list of all apks verion codes - returns the list of version codes

# File lib/supply/client.rb, line 149
def apks_version_codes
  ensure_active_edit!

  result = call_google_api { android_publisher.list_apks(current_package_name, current_edit.id) }

  return result.apks.map(&:version_code)
end
begin_edit(package_name: nil) click to toggle source

Begin modifying a certain package

# File lib/supply/client.rb, line 79
def begin_edit(package_name: nil)
  UI.user_error!("You currently have an active edit") if @current_edit

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

  self.current_package_name = package_name
end
clear_screenshots(image_type: nil, language: nil) click to toggle source
# File lib/supply/client.rb, line 305
def clear_screenshots(image_type: nil, language: nil)
  ensure_active_edit!

  call_google_api do
    android_publisher.delete_all_images(
      current_package_name,
      current_edit.id,
      language,
      image_type
    )
  end
end
commit_current_edit!() click to toggle source

Commits the current edit saving all pending changes on Google Play

# File lib/supply/client.rb, line 105
def commit_current_edit!
  ensure_active_edit!

  call_google_api { android_publisher.commit_edit(current_package_name, current_edit.id) }

  self.current_edit = nil
  self.current_package_name = nil
end
fetch_images(image_type: nil, language: nil) click to toggle source

@!group Screenshots

# File lib/supply/client.rb, line 274
def fetch_images(image_type: nil, language: nil)
  ensure_active_edit!

  result = call_google_api do
    android_publisher.list_images(
      current_package_name,
      current_edit.id,
      language,
      image_type
    )
  end

  (result.images || []).map(&:url)
end
listing_for_language(language) click to toggle source

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

# File lib/supply/client.rb, line 131
def listing_for_language(language)
  ensure_active_edit!

  begin
    result = android_publisher.get_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
listings() click to toggle source

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

# File lib/supply/client.rb, line 120
def listings
  ensure_active_edit!

  result = call_google_api { android_publisher.list_listings(current_package_name, current_edit.id) }

  return result.listings.map do |row|
    Listing.new(self, row.language, row)
  end
end
track_version_codes(track) click to toggle source

Get list of version codes for track

# File lib/supply/client.rb, line 237
def track_version_codes(track)
  ensure_active_edit!

  result = call_google_api do
    android_publisher.get_track(
      current_package_name,
      current_edit.id,
      track
    )
  end

  return result.version_codes
end
update_apk_listing_for_language(apk_listing) click to toggle source
# File lib/supply/client.rb, line 251
def update_apk_listing_for_language(apk_listing)
  ensure_active_edit!

  apk_listing_object = Androidpublisher::ApkListing.new({
    language: apk_listing.language,
    recent_changes: apk_listing.recent_changes
  })

  call_google_api do
    android_publisher.update_apk_listing(
      current_package_name,
      current_edit.id,
      apk_listing.apk_version_code,
      apk_listing.language,
      apk_listing_object
    )
  end
end
update_listing_for_language(language: nil, title: nil, short_description: nil, full_description: nil, video: nil) click to toggle source

Updates or creates the listing for the specified language

# File lib/supply/client.rb, line 179
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
    android_publisher.update_listing(
      current_package_name,
      current_edit.id,
      language,
      listing
    )
  end
end
update_track(track, rollout, apk_version_code) click to toggle source

Updates the track for the provided version code(s)

# File lib/supply/client.rb, line 215
def update_track(track, rollout, apk_version_code)
  ensure_active_edit!

  track_version_codes = apk_version_code.kind_of?(Array) ? apk_version_code : [apk_version_code]

  track_body = Androidpublisher::Track.new({
    track: track,
    user_fraction: rollout,
    version_codes: track_version_codes
  })

  call_google_api do
    android_publisher.update_track(
      current_package_name,
      current_edit.id,
      track,
      track_body
    )
  end
end
upload_apk(path_to_apk) click to toggle source
# File lib/supply/client.rb, line 200
def upload_apk(path_to_apk)
  ensure_active_edit!

  result_upload = call_google_api do
    android_publisher.upload_apk(
      current_package_name,
      current_edit.id,
      upload_source: path_to_apk
    )
  end

  return result_upload.version_code
end
upload_image(image_path: nil, image_type: nil, language: nil) click to toggle source

@param image_type (e.g. phoneScreenshots, sevenInchScreenshots, …)

# File lib/supply/client.rb, line 290
def upload_image(image_path: nil, image_type: nil, language: nil)
  ensure_active_edit!

  call_google_api do
    android_publisher.upload_image(
      current_package_name,
      current_edit.id,
      language,
      image_type,
      upload_source: image_path,
      content_type: 'image/*'
    )
  end
end
upload_obb(obb_file_path: nil, apk_version_code: nil, expansion_file_type: nil) click to toggle source
# File lib/supply/client.rb, line 318
def upload_obb(obb_file_path: nil, apk_version_code: nil, expansion_file_type: nil)
  ensure_active_edit!

  call_google_api do
    android_publisher.upload_expansion_file(
      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!() click to toggle source

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

# File lib/supply/client.rb, line 98
def validate_current_edit!
  ensure_active_edit!

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

Private Instance Methods

call_google_api() { || ... } click to toggle source
# File lib/supply/client.rb, line 339
def call_google_api
  yield if block_given?
rescue Google::Apis::ClientError => e
  UI.user_error! "Google Api Error: #{e.message}"
end
ensure_active_edit!() click to toggle source
# File lib/supply/client.rb, line 335
def ensure_active_edit!
  UI.user_error!("You need to have an active edit, make sure to call `begin_edit`") unless @current_edit
end