Class: Fastlane::Helper::AmazonAppstoreHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

BASE_URL =
'https://developer.amazon.com'
AUTH_URL =
'https://api.amazon.com/auth/o2/token'
IMAGE_TYPE_MAPPING =
{
  'screenshots' => 'phoneScreenshots',
  'small-icons' => 'icon.png',
  'large-icons' => 'large_icon.png',
  'promo-images' => 'featureGraphic.png',
  'firetv-icons' => 'tvBanner.png',
  'firetv-backgrounds' => 'tvBackground.png',
  'firetv-screenshots' => 'tvScreenshots',
  'firetv-featured-backgrounds' => 'tvFeaturedBackground.png',
  'firetv-featured-logos' => 'tvFeaturedLogo.png'
}.freeze

Class Method Summary collapse

Class Method Details

.commit_edits(app_id:, edit_id:, token:) ⇒ Object

Raises:

  • (StandardError)


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 366

def self.commit_edits(app_id:, edit_id:, token:)
  get_etag_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}"
  etag_response = api_client.get(get_etag_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, etag_response.body unless etag_response.success?

  etag = etag_response.headers['Etag']

  commit_edits_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/commit"
  commit_edits_response = api_client.post(commit_edits_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
    request.headers['If-Match'] = etag
  end
  raise StandardError, commit_edits_response.body unless commit_edits_response.success?

  nil
end

.create_edits(app_id:, token:) ⇒ Object

Raises:

  • (StandardError)


39
40
41
42
43
44
45
46
47
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 39

def self.create_edits(app_id:, token:)
  create_edits_response = api_client.post("api/appstore/v1/applications/#{app_id}/edits") do |request|
    request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, create_edits_response.body unless create_edits_response.success?

  create_edits_response.body[:id]
end

.delete_all_images(app_id:, edit_id:, language:, image_type:, token:) ⇒ Object

Raises:

  • (StandardError)


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 261

def self.delete_all_images(app_id:, edit_id:, language:, image_type:, token:)
  images_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/listings/#{language}/#{image_type}"
  etag_response = api_client.get(images_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, etag_response.body unless etag_response.success?

  etag = etag_response.headers['Etag']
  delete_response = api_client.delete(images_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
    request.headers['If-Match'] = etag
  end
  raise StandardError, delete_response.body unless delete_response.success?

  nil
end

.delete_apk(app_id:, edit_id:, apk_id:, token:) ⇒ Object

Raises:

  • (StandardError)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 163

def self.delete_apk(app_id:, edit_id:, apk_id:, token:)
  # Get ETag for the APK to be deleted
  get_etag_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/apks/#{apk_id}"
  etag_response = api_client.get(get_etag_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, etag_response.body unless etag_response.success?

  etag = etag_response.headers['Etag']

  # Delete the APK
  delete_apk_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/apks/#{apk_id}"
  delete_apk_response = api_client.delete(delete_apk_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
    request.headers['If-Match'] = etag
  end
  raise StandardError, delete_apk_response.body unless delete_apk_response.success?

  UI.message("Successfully deleted APK #{apk_id}")
end

.delete_edits_if_exists(app_id:, token:) ⇒ Object

Raises:

  • (StandardError)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 49

def self.delete_edits_if_exists(app_id:, token:)
  edits_id, etag = self.get_edits(app_id: app_id, token: token)
  return nil if edits_id.nil? || etag.nil? # Do nothing if edits do not exist

  edits_path = "api/appstore/v1/applications/#{app_id}/edits"
  delete_edits_response = api_client.delete("#{edits_path}/#{edits_id}") do |request|
    request.headers['Authorization'] = "Bearer #{token}"
    request.headers['If-Match'] = etag
  end

  raise StandardError, delete_edits_response.body unless delete_edits_response.success?
end

.find_images_for_type(metadata_path:, language:, image_type:) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 350

def self.find_images_for_type(metadata_path:, language:, image_type:)
  images_path = File.join(, language, 'images')
  mapping = IMAGE_TYPE_MAPPING[image_type]
  return [] if mapping.nil?

  target_path = File.join(images_path, mapping)

  if File.directory?(target_path)
    Dir.glob(File.join(target_path, '*.{png,jpg,jpeg}'))
  elsif File.exist?(target_path)
    [target_path]
  else
    []
  end
end

.get_edits(app_id:, token:) ⇒ Object

Raises:

  • (StandardError)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 62

def self.get_edits(app_id:, token:)
  edits_path = "api/appstore/v1/applications/#{app_id}/edits"
  edits_response = api_client.get(edits_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, edits_response.body unless edits_response.success?

  edits_id = edits_response.body[:id]
  etag = edits_response.headers['Etag']

  return edits_id, etag
end

.get_images(app_id:, edit_id:, language:, image_type:, token:) ⇒ Object

Raises:

  • (StandardError)


251
252
253
254
255
256
257
258
259
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 251

def self.get_images(app_id:, edit_id:, language:, image_type:, token:)
  images_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/listings/#{language}/#{image_type}"
  images_response = api_client.get(images_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, images_response.body unless images_response.success?

  images_response.body
end

.load_metadata_from_files(metadata_path:, language:) ⇒ Object



321
322
323
324
325
326
327
328
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 321

def self.(metadata_path:, language:)
  lang_path = File.join(, language)
  {
    title: (lang_path, 'title.txt'),
    shortDescription: (lang_path, 'short_description.txt'),
    fullDescription: (lang_path, 'full_description.txt')
  }
end

.replace_apks(apk_paths:, app_id:, edit_id:, token:) ⇒ Object

Raises:

  • (StandardError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 91

def self.replace_apks(apk_paths:, app_id:, edit_id:, token:)
  # Get existing APKs in the edit
  get_apks_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/apks"
  get_apks_response = api_client.get(get_apks_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, get_apks_response.body unless get_apks_response.success?

  existing_apks = get_apks_response.body
  raise StandardError, 'No existing APKs found in edit' if existing_apks.empty?

  apk_results = []

  apk_paths.each_with_index do |apk_path, index|
    if index < existing_apks.length
      # Replace existing APK at the specified index
      apk_id = existing_apks[index][:id]
      raise StandardError, "apk_id is nil for index #{index}" if apk_id.nil?

      # Get ETag for the specific APK
      get_etag_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/apks/#{apk_id}"
      etag_response = api_client.get(get_etag_path) do |request|
        request.headers['Authorization'] = "Bearer #{token}"
      end
      raise StandardError, etag_response.body unless etag_response.success?

      etag = etag_response.headers['Etag']

      # Replace the APK
      replace_apk_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/apks/#{apk_id}/replace"
      replace_apk_response = api_client.put(replace_apk_path) do |request|
        request.body = Faraday::UploadIO.new(apk_path, 'application/vnd.android.package-archive')
        request.headers['Content-Length'] = request.body.stat.size.to_s
        request.headers['Content-Type'] = 'application/vnd.android.package-archive'
        request.headers['Authorization'] = "Bearer #{token}"
        request.headers['If-Match'] = etag
      end
      raise StandardError, replace_apk_response.body unless replace_apk_response.success?

      version_code = replace_apk_response.body[:versionCode]
      apk_results << { version_code: version_code, apk_id: apk_id }
    else
      # Upload new APK if there are more APK paths than existing APKs
      result = upload_apk(
        local_apk_path: apk_path,
        app_id: app_id,
        edit_id: edit_id,
        token: token
      )
      apk_results << result
    end
  end

  # Delete remaining APKs if there are more existing APKs than specified APK paths
  if existing_apks.length > apk_paths.length
    remaining_apks = existing_apks[apk_paths.length..]
    UI.message("Deleting #{remaining_apks.length} remaining APK(s)...")

    remaining_apks.each_with_index do |apk, index|
      delete_apk(
        app_id: app_id,
        edit_id: edit_id,
        apk_id: apk[:id],
        token: token
      )
      UI.message("Deleted APK ID: #{apk[:id]} (position #{apk_paths.length + index + 1})")
    end
  end

  apk_results
end

.setup(timeout:) ⇒ Object



17
18
19
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 17

def self.setup(timeout:)
  @timeout = timeout
end

.token(client_id:, client_secret:) ⇒ Object

Raises:

  • (StandardError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 21

def self.token(client_id:, client_secret:)
  grant_type = 'client_credentials'
  scope = 'appstore::apps:readwrite'
  data = {
    grant_type: grant_type,
    client_id: client_id,
    client_secret: client_secret,
    scope: scope
  }
  auth_response = auth_client.post do |request|
    # without escaping
    request.body = JSON.parse(data.to_json)
  end
  raise StandardError, auth_response.body unless auth_response.success?

  auth_response.body[:access_token]
end

.update_changelogs(app_id:, edit_id:, token:, version_codes:, skip_upload_changelogs:, metadata_path:) ⇒ Object

Raises:

  • (StandardError)


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
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 184

def self.update_changelogs(app_id:, edit_id:, token:, version_codes:, skip_upload_changelogs:, metadata_path:)
  # Use the highest version code's changelog (same as Fastlane's approach)
  max_version_code = version_codes.max
  if max_version_code.nil?
    UI.message("Ensuring release notes placeholder is present...")
  else
    UI.message("Updating changelogs using highest version code: #{max_version_code}")
  end

  listings_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/listings"
  listings_response = api_client.get(listings_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, listings_response.body unless listings_response.success?

  listings_response.body[:listings].each do |lang, listing|
    # When only the "-" placeholder would be written, keep existing
    # release notes untouched and fill it in only when they are missing.
    next if (skip_upload_changelogs || max_version_code.nil?) && !listing[:recentChanges].to_s.strip.empty?

    # Get fresh ETag for each language update to avoid conflicts
    etag_response = api_client.get(listings_path) do |request|
      request.headers['Authorization'] = "Bearer #{token}"
    end
    raise StandardError, etag_response.body unless etag_response.success?

    etag = etag_response.headers['Etag']

    listing[:recentChanges] = find_changelog(
      language: listing[:language],
      version_code: max_version_code,
      skip_upload_changelogs: skip_upload_changelogs,
      metadata_path: 
    )

    update_listings_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/listings/#{lang}"
    update_listings_response = api_client.put(update_listings_path) do |request|
      request.body = listing.to_json
      request.headers['Authorization'] = "Bearer #{token}"
      request.headers['If-Match'] = etag
    end
    raise StandardError, update_listings_response.body unless update_listings_response.success?
  end
  nil
end

.update_listing_metadata(app_id:, edit_id:, language:, listing_data:, token:) ⇒ Object

Raises:

  • (StandardError)


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 298

def self.(app_id:, edit_id:, language:, listing_data:, token:)
  listings_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/listings/#{language}"
  etag_response = api_client.get(listings_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, etag_response.body unless etag_response.success?

  etag = etag_response.headers['Etag']
  existing_data = etag_response.body
  merged_data = existing_data.merge(listing_data.compact)
  merged_data[:recentChanges] = '-' if merged_data[:recentChanges].nil? || merged_data[:recentChanges].empty?

  update_response = api_client.put(listings_path) do |request|
    request.body = merged_data.to_json
    request.headers['Content-Type'] = 'application/json'
    request.headers['Authorization'] = "Bearer #{token}"
    request.headers['If-Match'] = etag
  end
  raise StandardError, update_response.body unless update_response.success?

  nil
end

.upload_apk(local_apk_path:, app_id:, edit_id:, token:) ⇒ Object

Raises:

  • (StandardError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 75

def self.upload_apk(local_apk_path:, app_id:, edit_id:, token:)
  upload_apk_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/apks/upload"
  upload_apk_response = api_client.post(upload_apk_path) do |request|
    request.body = Faraday::UploadIO.new(local_apk_path, 'application/vnd.android.package-archive')
    request.headers['Content-Length'] = request.body.stat.size.to_s
    request.headers['Content-Type'] = 'application/vnd.android.package-archive'
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, upload_apk_response.body unless upload_apk_response.success?

  {
    version_code: upload_apk_response.body[:versionCode],
    apk_id: upload_apk_response.body[:id]
  }
end

.upload_image(app_id:, edit_id:, language:, image_type:, image_path:, token:) ⇒ Object

Raises:

  • (StandardError)


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 230

def self.upload_image(app_id:, edit_id:, language:, image_type:, image_path:, token:)
  images_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/listings/#{language}/#{image_type}"
  etag_response = api_client.get(images_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, etag_response.body unless etag_response.success?

  etag = etag_response.headers['Etag']
  upload_path = "#{images_path}/upload"
  upload_response = api_client.post(upload_path) do |request|
    request.body = File.binread(image_path)
    request.headers['Content-Length'] = request.body.bytesize.to_s
    request.headers['Content-Type'] = 'application/octet-stream'
    request.headers['Authorization'] = "Bearer #{token}"
    request.headers['If-Match'] = etag
  end
  raise StandardError, upload_response.body unless upload_response.success?

  upload_response.body[:id]
end

.upload_video(app_id:, edit_id:, language:, video_path:, token:) ⇒ Object

Raises:

  • (StandardError)


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/fastlane/plugin/amazon_appstore/helper/amazon_appstore_helper.rb', line 278

def self.upload_video(app_id:, edit_id:, language:, video_path:, token:)
  videos_path = "api/appstore/v1/applications/#{app_id}/edits/#{edit_id}/listings/#{language}/videos"
  etag_response = api_client.get(videos_path) do |request|
    request.headers['Authorization'] = "Bearer #{token}"
  end
  raise StandardError, etag_response.body unless etag_response.success?

  etag = etag_response.headers['Etag']
  upload_response = api_client.post(videos_path) do |request|
    request.body = File.binread(video_path)
    request.headers['Content-Length'] = request.body.bytesize.to_s
    request.headers['Content-Type'] = 'application/octet-stream'
    request.headers['Authorization'] = "Bearer #{token}"
    request.headers['If-Match'] = etag
  end
  raise StandardError, upload_response.body unless upload_response.success?

  upload_response.body[:id]
end