Skip to content

Commit ccba6e1

Browse files
committed
Extract retry logic in GlotPressDownloader helper
1 parent 1368924 commit ccba6e1

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
11
require 'net/http'
22
require 'json'
33

4+
module Fastlane
5+
module Helper
6+
class GlotPressDownloader
7+
AUTO_RETRY_SLEEP_TIME = 20
8+
MAX_AUTO_RETRY_ATTEMPTS = 30
9+
10+
def initialize(auto_retry)
11+
@auto_retry = auto_retry
12+
@auto_retry_attempt_counter = 0
13+
end
14+
15+
def download(target_locale, glotpress_url, is_source)
16+
uri = URI(glotpress_url)
17+
response = Net::HTTP.get_response(uri)
18+
19+
case response.code
20+
when '200'
21+
# All good pass the result along
22+
response
23+
when '301'
24+
# Follow the redirect
25+
UI.message("Received 301 for `#{locale}`. Following redirect...")
26+
download(locale, response.header['location'], is_source)
27+
when '429'
28+
# We got rate-limited, auto_retry or offer to try again with a prompt
29+
if @auto_retry && @auto_retry_attempt_counter <= MAX_AUTO_RETRY_ATTEMPTS
30+
UI.message("Received 429 for `#{response.uri}`. Auto retrying in #{AUTO_RETRY_SLEEP_TIME} seconds...")
31+
sleep(AUTO_RETRY_SLEEP_TIME)
32+
@auto_retry_attempt_counter += 1
33+
download(target_locale, response.uri, is_source)
34+
elsif UI.confirm("Retry downloading `#{response.uri}` after receiving 429 from the API?")
35+
download(target_locale, response.uri, is_source)
36+
else
37+
UI.error("Abandoning `#{response.uri}` download as requested.")
38+
end
39+
else
40+
message = "Received unexpected #{response.code} from request to URI #{response.uri}."
41+
UI.abort_with_message!(message) unless UI.confirm("#{message} Continue anyway?")
42+
end
43+
end
44+
end
45+
end
46+
end
47+
448
module Fastlane
549
module Helper
650
class MetadataDownloader
@@ -19,8 +63,8 @@ def initialize(target_folder, target_files, auto_retry)
1963

2064
# Downloads data from GlotPress, in JSON format
2165
def download(target_locale, glotpress_url, is_source)
22-
uri = URI(glotpress_url)
23-
response = Net::HTTP.get_response(uri)
66+
downloader = GlotPressDownloader.new(@auto_retry)
67+
response = downloader.download(target_locale, glotpress_url, is_source)
2468
handle_glotpress_download(response: response, locale: target_locale, is_source: is_source)
2569
end
2670

spec/metadata_download_helper_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
end
2525

2626
expect(Fastlane::UI).to receive(:message)
27-
.with(/Received 429 for `en-AU`. Auto retrying in 20 seconds.../)
27+
.with(/Received 429 for `#{fake_url}`. Auto retrying in 20 seconds.../)
2828

2929
expect(Fastlane::UI).to receive(:message)
3030
.with(/No translation available for en-AU/)

0 commit comments

Comments
 (0)