用ruby下载.txt.gz

时间:2018-12-13 06:18:45

标签: ruby gz

Apple有一个API,我想从Apple下载销售报告数据。 如果全部有效,则响应为.txt.gz文件,我的所有尝试都无法正确下载该文件,并且没有超时 例如:

token = JWT.encode(
   {
    iss: ISSUER_ID,
    exp: Time.now.to_i + 20 * 60,
    aud: "appstoreconnect-v1"
   },
   private_key,
   "ES256",
   header_fields={
     kid: KEY_ID }
 )
puts token

max_retries = 3
times_retried = 0
begin
    filename = "fiance_monthly.txt.gz"
    File.open(filename, "w") do |file|
    file.binmode
    HTTParty.get("https://api.appstoreconnect.apple.com/v1/salesReports?filter[frequency]=MONTHLY&filter[reportSubType]=SUMMARY&filter[reportType]=SALES&filter[vendorNumber]=&filter[reportDate]=2018-11", 
        :query => {},
        :headers => { 
            "Authorization" => "Bearer " + token,
            "Content-Type" => "application/a-gzip",
            "Transfer-Encoding" => "chunked",
            "Connection" => "keep-alive",
            "Content-Encoding" => "agzip",
        }, 
        stream_body: true) do |fragment|
            file.write(fragment)
        end
        puts "done"
    end
  rescue Net::ReadTimeout => error
    if times_retried < max_retries
      times_retried += 1
      puts "Failed to <do the thing>, retry #{times_retried}/#{max_retries}"
      retry
    else
      puts "Exiting script. <explanation of why this is unlikely to recover>"
      exit(1)
    end
  end

这会导致Net :: ReadTimeout

邮递员请求中的响应标头如下:

Server →daiquiri/3.0.0
Date →Thu, 13 Dec 2018 06:07:58 GMT
Content-Type →application/a-gzip
Transfer-Encoding →chunked
Connection →keep-alive
requestId →d2abe752-303f-4bad-917c-6e3366219ada
Content-Encoding →agzip
Content-Disposition →attachment;filename=S_M_85484293_201811.txt.gz
x-reports-filename →S_M_85484293_201811.txt.gz
x-reports-download-version →1_0
Strict-Transport-Security →max-age=31536000; includeSubDomains
X-XSS-Protection →1; mode=block
X-Content-Type-Options →nosniff
Host →reportingitc-reporter-internal.corp.apple.com
X-Frame-Options →SAMEORIGIN
X-Request-ID →UHNUNKBJS5KAEN7BZGZKY52L
X-Rate-Limit →user-hour-lim:3600;user-hour-rem:3596;
x-daiquiri-instance →daiquiri:38493001:pv50p00it-hyhk12043901:7987:18Q34

0 个答案:

没有答案