Skip to content

Commit

Permalink
Merge 4.4.0 release to main (#156)
Browse files Browse the repository at this point in the history
* When polling, raise if an error is returned (#142)

Previously, if an error from the API was returned while polling for updates on a file, it would continue polling. I updated it so that it raises a RequestError if the API returns an error status. If not, it raises a RetryError, which is swallowed by the retry gem's `with_retries` block.

Example of an error that led to continued polling:

```
{"status":"error","error":"File validation error: Uploading of these file types is not allowed.","error_code":"DownloadFileValidationFailedError"}
```

* Sign URL uploads if configured (#139)

Fix namespace for service

Add spec for upload_from_url

Fix requiring of SignatureGenerator and location of tests

* Merge with main and add changelog

* Bump version to 4.4.0

* Apply suggestions from code review

Co-authored-by: Roman Sedykh <[email protected]>

---------

Co-authored-by: Josh Schwartzman <[email protected]>
Co-authored-by: Roman Sedykh <[email protected]>
  • Loading branch information
3 people authored Mar 9, 2024
1 parent fc50f5d commit 635f0cd
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 101 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 4.4.0 — 2024-03-09

### Breaking

* Drop support of unmaintainable Ruby versions < 3.x.

### Fixed

* Update locations where Dry::Monads structure has changed.
* Sign URL uploads if configured (#139).
* Start returning proper error message when raising RequestError in poll_upload_response, to hint to users what is going on. Fixes #141.
* When polling, raise if an error is returned (#142).
* Fix documentation about original file url on simple file upload.

### Changed
* Support params in Rest client and in file info method, to allow passing custom params like "include=appdata" in `Uploadcare::File.file` calls. Closes #132.


## 4.3.6 — 2023-11-18

### Fixed
Expand Down
1 change: 1 addition & 0 deletions lib/uploadcare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Exceptions
require 'exception/throttle_error'
require 'exception/request_error'
require 'exception/retry_error'

# Entities
require 'entity/entity'
Expand Down
34 changes: 21 additions & 13 deletions lib/uploadcare/client/uploader_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative 'upload_client'
require 'retries'
require 'param/upload/upload_params_generator'
require 'param/upload/signature_generator'

module Uploadcare
module Client
Expand Down Expand Up @@ -75,15 +76,22 @@ def post(args = {})
def poll_upload_response(token)
with_retries(max_tries: Uploadcare.config.max_request_tries,
base_sleep_seconds: Uploadcare.config.base_request_sleep,
max_sleep_seconds: Uploadcare.config.max_request_sleep) do
max_sleep_seconds: Uploadcare.config.max_request_sleep,
rescue: RetryError) do
response = get_upload_from_url_status(token)
handle_polling_response(response)
end
end

if %w[progress waiting unknown].include?(response.success[:status])
raise RequestError, 'Upload is taking longer than expected. Try increasing the max_request_tries config if you know your file uploads will take more time.' # rubocop:disable Layout/LineLength
end

response
def handle_polling_response(response)
case response.success[:status]
when 'error'
raise RequestError, response.success[:error]
when 'progress', 'waiting', 'unknown'
raise RetryError, response.success[:error] || 'Upload is taking longer than expected. Try increasing the max_request_tries config if you know your file uploads will take more time.' # rubocop:disable Layout/LineLength
end

response
end

# Prepares body for upload_many method
Expand All @@ -99,13 +107,13 @@ def upload_many_body(arr, options = {})

# Prepare upload_from_url initial request body
def upload_from_url_body(url, options = {})
HTTP::FormData::Multipart.new(
options.merge(
'pub_key' => Uploadcare.config.public_key,
'source_url' => url,
'store' => store_value(options[:store])
)
)
opts = {
'pub_key' => Uploadcare.config.public_key,
'source_url' => url,
'store' => store_value(options[:store])
}
opts.merge!(Param::Upload::SignatureGenerator.call) if Uploadcare.config.sign_uploads
HTTP::FormData::Multipart.new(options.merge(opts))
end

def store_value(store)
Expand Down
3 changes: 1 addition & 2 deletions lib/uploadcare/exception/request_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Uploadcare
module Exception
# Standard error for invalid API responses
class RequestError < StandardError
end
class RequestError < StandardError; end
end
end
8 changes: 8 additions & 0 deletions lib/uploadcare/exception/retry_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Uploadcare
module Exception
# Standard error to raise when needing to retry a request
class RetryError < StandardError; end
end
end
2 changes: 1 addition & 1 deletion lib/uploadcare/ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Uploadcare
VERSION = '4.3.6'
VERSION = '4.4.0'
end
116 changes: 116 additions & 0 deletions spec/fixtures/vcr_cassettes/upload_upload_from_url_with_signature.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions spec/uploadcare/client/uploader_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
module Uploadcare
module Client
RSpec.describe UploaderClient do
subject { UploaderClient.new }
let!(:file) { ::File.open('spec/fixtures/kitten.jpeg') }
let!(:another_file) { ::File.open('spec/fixtures/another_kitten.jpeg') }
subject { described_class.new }

describe 'upload' do
let(:file) { ::File.open('spec/fixtures/kitten.jpeg') }
let(:another_file) { ::File.open('spec/fixtures/another_kitten.jpeg') }

it 'uploads a file' do
VCR.use_cassette('upload_upload') do
response = subject.upload(file, metadata: { subsystem: 'test' })
Expand Down
Loading

0 comments on commit 635f0cd

Please sign in to comment.