@@ -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
105128end
0 commit comments