1
1
require 'net/http'
2
2
require 'json'
3
3
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
+
4
48
module Fastlane
5
49
module Helper
6
50
class MetadataDownloader
@@ -19,8 +63,8 @@ def initialize(target_folder, target_files, auto_retry)
19
63
20
64
# Downloads data from GlotPress, in JSON format
21
65
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 )
24
68
handle_glotpress_download ( response : response , locale : target_locale , is_source : is_source )
25
69
end
26
70
0 commit comments