Skip to content

Commit 7abf936

Browse files
authored
Handle 429 and prevent 301 in gp_downloadmetadata (#406)
2 parents 9f78ca9 + eafb2ed commit 7abf936

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
### Breaking Changes
88

9-
_None_
9+
- Propose to retry when `gp_downloadmetadata` receives a `429 - Too Many Requests` error. [#406]
1010

1111
### New Features
1212

1313
_None_
1414

1515
### Bug Fixes
1616

17-
_None_
17+
- Update the URL used by `gp_downloadmetadata` to prevent consistent `301` responses. [#406]
1818

1919
### Internal Changes
2020

lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_downloadmetadata_action.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def self.run(params)
1919

2020
params[:locales].each do |loc|
2121
if loc.is_a?(Array)
22-
puts "Downloading language: #{loc[1]}"
23-
complete_url = "#{params[:project_url]}#{loc[0]}/default/export-translations?filters[status]=current&format=json"
22+
UI.message "Downloading language: #{loc[1]}"
23+
complete_url = "#{params[:project_url]}#{loc[0]}/default/export-translations/?filters[status]=current&format=json"
2424
downloader.download(loc[1], complete_url, loc[1] == params[:source_locale])
2525
end
2626

2727
if loc.is_a?(String)
28-
puts "Downloading language: #{loc}"
29-
complete_url = "#{params[:project_url]}#{loc}/default/export-translations?filters[status]=current&format=json"
28+
UI.message "Downloading language: #{loc}"
29+
complete_url = "#{params[:project_url]}#{loc}/default/export-translations/?filters[status]=current&format=json"
3030
downloader.download(loc, complete_url, loc == params[:source_locale])
3131
end
3232
end

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ def initialize(target_folder, target_files)
1616
def download(target_locale, glotpress_url, is_source)
1717
uri = URI(glotpress_url)
1818
response = Net::HTTP.get_response(uri)
19-
response = Net::HTTP.get_response(URI.parse(response.header['location'])) if response.code == '301'
20-
21-
@alternates.clear
22-
loc_data = JSON.parse(response.body) rescue loc_data = nil
23-
parse_data(target_locale, loc_data, is_source)
24-
reparse_alternates(target_locale, loc_data, is_source) unless @alternates.length == 0
19+
handle_glotpress_download(response: response, locale: target_locale, is_source: is_source)
2520
end
2621

2722
# Parse JSON data and update the local files
@@ -100,6 +95,34 @@ def delete_existing_metadata(target_locale)
10095
def get_target_file_path(locale, file_name)
10196
"#{@target_folder}/#{locale}/#{file_name}"
10297
end
98+
99+
private
100+
101+
def handle_glotpress_download(response:, locale:, is_source:)
102+
case response.code
103+
when '200'
104+
# All good, parse the result
105+
UI.success("Successfully downloaded `#{locale}`.")
106+
@alternates.clear
107+
loc_data = JSON.parse(response.body) rescue loc_data = nil
108+
parse_data(locale, loc_data, is_source)
109+
reparse_alternates(target_locale, loc_data, is_source) unless @alternates.length == 0
110+
when '301'
111+
# Follow the redirect
112+
UI.message("Received 301 for `#{locale}`. Following redirect...")
113+
download(locale, response.header['location'], is_source)
114+
when '429'
115+
# We got rate-limited, offer to try again
116+
if UI.confirm("Retry downloading `#{locale}` after receiving 429 from the API?")
117+
download(locale, response.uri, is_source)
118+
else
119+
UI.error("Abandoning `#{locale}` download as requested.")
120+
end
121+
else
122+
message = "Received unexpected #{response.code} from request to URI #{response.uri}."
123+
UI.abort_with_message!(message) unless UI.confirm("#{message} Continue anyway?")
124+
end
125+
end
103126
end
104127
end
105128
end

0 commit comments

Comments
 (0)