Iterating over a list doesn't work when rescuing 302

34 views Asked by At

When I iterate over a list, sending a get request with every element, block in rescue executes for the first iteration only. Here's the code:

def download_doc(gem_id)
  url = @base_url + ATTACHMENT_ENDPOINT + gem_id + "/attachment"
  headers = {
    'Authorization' => @token
    }
  attempts = 0
  begin
response = RestClient::Request.execute(
      method: :get,
      url: url,
      headers: { Authorization: "Bearer #{@token}" },
      max_redirects: 0 
    )
  rescue RestClient::Found => found
    puts "FOUND #{gem_id}"
    exec "wget \"#{found.response.headers[:location]}\" -O \"#{gem_id}.pdf\" -q --show-progress"
  rescue => ex
    puts ex.response
  end
end

ids = File.read(@input_file).split(",")
arra.each do |id|
  download_doc(id)
end

The context is that the GET request I do, redirects to S3 and throws Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified error. So I stopped the redirection and tried to wget the location from the initial GET request.

I tried multiple different apporaches, adjusting rescue blocks etc. but could not figure out the solution.

Preferably I'd get around the S3 error (I suppose I should somehow remove the authorization token for the redirected RQ?)

1

There are 1 answers

0
boberczus On

The problem was that exec stops the script. Use backticks ``` instead. More info: https://rubyquicktips.com/post/5862861056/execute-shell-commands