Skip to content

Commit

Permalink
Move GlotpressDownaloder in dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio committed Oct 5, 2023
1 parent 0031a46 commit 28e8f7c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'net/http'

module Fastlane
module Helper
class GlotpressDownloader
AUTO_RETRY_SLEEP_TIME = 20
MAX_AUTO_RETRY_ATTEMPTS = 30

def initialize(
auto_retry: true,
auto_retry_sleep_time: 20,
auto_retry_max_attempts: 30
)
@auto_retry = auto_retry
@auto_retry_sleep_time = auto_retry_sleep_time
@auto_retry_max_attempts = auto_retry_max_attempts
@auto_retry_attempt_counter = 0
end

def download(glotpress_url)
uri = URI(glotpress_url)
response = Net::HTTP.get_response(uri)

case response.code
when '200' # All good pass the result along
response
when '301' # Follow the redirect
UI.message("Received 301 for `#{response.uri}`. Following redirect...")
download(response.header['location'])
when '429' # We got rate-limited, auto_retry or offer to try again with a prompt
if @auto_retry
if @auto_retry_attempt_counter < @auto_retry_max_attempts
UI.message("Received 429 for `#{response.uri}`. Auto retrying in #{@auto_retry_sleep_time} seconds...")
sleep(@auto_retry_sleep_time)
@auto_retry_attempt_counter += 1
download(response.uri)
else
UI.error("Abandoning `#{response.uri}` download after #{@auto_retry_attempt_counter} retries.")
end
elsif UI.confirm("Retry downloading `#{response.uri}` after receiving 429 from the API?")
download(response.uri)
else
UI.error("Abandoning `#{response.uri}` download as requested.")
end
else
message = "Received unexpected #{response.code} from request to URI #{response.uri}."
UI.abort_with_message!(message) unless UI.confirm("#{message} Continue anyway?")
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def self.download_glotpress_export_file(
destination:,
autoretry: true,
autoretry_count: 0,
autoretry_max: Fastlane::Helper::MetadataDownloader::MAX_AUTO_RETRY_ATTEMPTS,
autoretry_sleep: Fastlane::Helper::MetadataDownloader::AUTO_RETRY_SLEEP_TIME
autoretry_max: Fastlane::Helper::GlotpressDownloader::MAX_AUTO_RETRY_ATTEMPTS,
autoretry_sleep: Fastlane::Helper::GlotpressDownloader::AUTO_RETRY_SLEEP_TIME
)
query_params = (filters || {}).transform_keys { |k| "filters[#{k}]" }.merge(format: 'strings')
uri = URI.parse("#{project_url.chomp('/')}/#{locale}/default/export-translations/?#{URI.encode_www_form(query_params)}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,6 @@
require 'net/http'
require 'json'

module Fastlane
module Helper
class GlotpressDownloader
AUTO_RETRY_SLEEP_TIME = 20
MAX_AUTO_RETRY_ATTEMPTS = 30

def initialize(
auto_retry: true,
auto_retry_sleep_time: 20,
auto_retry_max_attempts: 30
)
@auto_retry = auto_retry
@auto_retry_sleep_time = auto_retry_sleep_time
@auto_retry_max_attempts = auto_retry_max_attempts
@auto_retry_attempt_counter = 0
end

def download(glotpress_url)
uri = URI(glotpress_url)
response = Net::HTTP.get_response(uri)

case response.code
when '200' # All good pass the result along
response
when '301' # Follow the redirect
UI.message("Received 301 for `#{response.uri}`. Following redirect...")
download(response.header['location'])
when '429' # We got rate-limited, auto_retry or offer to try again with a prompt
if @auto_retry
if @auto_retry_attempt_counter < @auto_retry_max_attempts
UI.message("Received 429 for `#{response.uri}`. Auto retrying in #{@auto_retry_sleep_time} seconds...")
sleep(@auto_retry_sleep_time)
@auto_retry_attempt_counter += 1
download(response.uri)
else
UI.error("Abandoning `#{response.uri}` download after #{@auto_retry_attempt_counter} retries.")
end
elsif UI.confirm("Retry downloading `#{response.uri}` after receiving 429 from the API?")
download(response.uri)
else
UI.error("Abandoning `#{response.uri}` download as requested.")
end
else
message = "Received unexpected #{response.code} from request to URI #{response.uri}."
UI.abort_with_message!(message) unless UI.confirm("#{message} Continue anyway?")
end
end
end
end
end

module Fastlane
module Helper
class MetadataDownloader
Expand Down

0 comments on commit 28e8f7c

Please sign in to comment.