@@ -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,31 @@ 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+ @alternates . clear
106+ loc_data = JSON . parse ( response . body ) rescue loc_data = nil
107+ parse_data ( locale , loc_data , is_source )
108+ reparse_alternates ( target_locale , loc_data , is_source ) unless @alternates . length == 0
109+ when '301'
110+ # Follow the redirect
111+ download ( locale , response . header [ 'location' ] , is_source )
112+ when '429'
113+ # We got rate-limited, offer to try again
114+ if UI . confirm ( "Retry downloading `#{ locale } ` after receiving 429 from the API?" )
115+ download ( locale , response . uri , is_source )
116+ else
117+ UI . message ( "Giving up on attempting to download #{ locale } ." )
118+ end
119+ else
120+ UI . error ( "Received unexpected #{ response . code } from request to URI #{ response . uri } " )
121+ end
122+ end
103123 end
104124 end
105125end
0 commit comments