Skip to content

Commit 28e8f7c

Browse files
committed
Move GlotpressDownaloder in dedicated file
1 parent 0031a46 commit 28e8f7c

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'net/http'
2+
3+
module Fastlane
4+
module Helper
5+
class GlotpressDownloader
6+
AUTO_RETRY_SLEEP_TIME = 20
7+
MAX_AUTO_RETRY_ATTEMPTS = 30
8+
9+
def initialize(
10+
auto_retry: true,
11+
auto_retry_sleep_time: 20,
12+
auto_retry_max_attempts: 30
13+
)
14+
@auto_retry = auto_retry
15+
@auto_retry_sleep_time = auto_retry_sleep_time
16+
@auto_retry_max_attempts = auto_retry_max_attempts
17+
@auto_retry_attempt_counter = 0
18+
end
19+
20+
def download(glotpress_url)
21+
uri = URI(glotpress_url)
22+
response = Net::HTTP.get_response(uri)
23+
24+
case response.code
25+
when '200' # All good pass the result along
26+
response
27+
when '301' # Follow the redirect
28+
UI.message("Received 301 for `#{response.uri}`. Following redirect...")
29+
download(response.header['location'])
30+
when '429' # We got rate-limited, auto_retry or offer to try again with a prompt
31+
if @auto_retry
32+
if @auto_retry_attempt_counter < @auto_retry_max_attempts
33+
UI.message("Received 429 for `#{response.uri}`. Auto retrying in #{@auto_retry_sleep_time} seconds...")
34+
sleep(@auto_retry_sleep_time)
35+
@auto_retry_attempt_counter += 1
36+
download(response.uri)
37+
else
38+
UI.error("Abandoning `#{response.uri}` download after #{@auto_retry_attempt_counter} retries.")
39+
end
40+
elsif UI.confirm("Retry downloading `#{response.uri}` after receiving 429 from the API?")
41+
download(response.uri)
42+
else
43+
UI.error("Abandoning `#{response.uri}` download as requested.")
44+
end
45+
else
46+
message = "Received unexpected #{response.code} from request to URI #{response.uri}."
47+
UI.abort_with_message!(message) unless UI.confirm("#{message} Continue anyway?")
48+
end
49+
end
50+
end
51+
end
52+
end

Diff for: lib/fastlane/plugin/wpmreleasetoolkit/helper/ios/ios_l10n_helper.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def self.download_glotpress_export_file(
168168
destination:,
169169
autoretry: true,
170170
autoretry_count: 0,
171-
autoretry_max: Fastlane::Helper::MetadataDownloader::MAX_AUTO_RETRY_ATTEMPTS,
172-
autoretry_sleep: Fastlane::Helper::MetadataDownloader::AUTO_RETRY_SLEEP_TIME
171+
autoretry_max: Fastlane::Helper::GlotpressDownloader::MAX_AUTO_RETRY_ATTEMPTS,
172+
autoretry_sleep: Fastlane::Helper::GlotpressDownloader::AUTO_RETRY_SLEEP_TIME
173173
)
174174
query_params = (filters || {}).transform_keys { |k| "filters[#{k}]" }.merge(format: 'strings')
175175
uri = URI.parse("#{project_url.chomp('/')}/#{locale}/default/export-translations/?#{URI.encode_www_form(query_params)}")

Diff for: lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb

-51
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
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(
11-
auto_retry: true,
12-
auto_retry_sleep_time: 20,
13-
auto_retry_max_attempts: 30
14-
)
15-
@auto_retry = auto_retry
16-
@auto_retry_sleep_time = auto_retry_sleep_time
17-
@auto_retry_max_attempts = auto_retry_max_attempts
18-
@auto_retry_attempt_counter = 0
19-
end
20-
21-
def download(glotpress_url)
22-
uri = URI(glotpress_url)
23-
response = Net::HTTP.get_response(uri)
24-
25-
case response.code
26-
when '200' # All good pass the result along
27-
response
28-
when '301' # Follow the redirect
29-
UI.message("Received 301 for `#{response.uri}`. Following redirect...")
30-
download(response.header['location'])
31-
when '429' # We got rate-limited, auto_retry or offer to try again with a prompt
32-
if @auto_retry
33-
if @auto_retry_attempt_counter < @auto_retry_max_attempts
34-
UI.message("Received 429 for `#{response.uri}`. Auto retrying in #{@auto_retry_sleep_time} seconds...")
35-
sleep(@auto_retry_sleep_time)
36-
@auto_retry_attempt_counter += 1
37-
download(response.uri)
38-
else
39-
UI.error("Abandoning `#{response.uri}` download after #{@auto_retry_attempt_counter} retries.")
40-
end
41-
elsif UI.confirm("Retry downloading `#{response.uri}` after receiving 429 from the API?")
42-
download(response.uri)
43-
else
44-
UI.error("Abandoning `#{response.uri}` download as requested.")
45-
end
46-
else
47-
message = "Received unexpected #{response.code} from request to URI #{response.uri}."
48-
UI.abort_with_message!(message) unless UI.confirm("#{message} Continue anyway?")
49-
end
50-
end
51-
end
52-
end
53-
end
54-
554
module Fastlane
565
module Helper
576
class MetadataDownloader

0 commit comments

Comments
 (0)