Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support params in Rest client and in file info method. #151

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ Entities are representations of objects in Uploadcare cloud.

#### File

File entity contains its metadata.
File entity contains its metadata. It also supports `include` param to include additional fields to the file object, such as: "appdata".

```ruby
@file = Uploadcare::File.file("FILE_ID_IN_YOUR_PROJECT")
@file = Uploadcare::File.file("FILE_ID_IN_YOUR_PROJECT", include: "appdata")
{
"datetime_removed"=>nil,
"datetime_stored"=>"2018-11-26T12:49:10.477888Z",
Expand Down
4 changes: 2 additions & 2 deletions lib/uploadcare/client/file_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def index

# Acquire file info
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/fileInfo
def info(uuid)
get(uri: "/files/#{uuid}/")
def info(uuid, params = {})
get(uri: "/files/#{uuid}/", params: params)
end
alias file info

Expand Down
7 changes: 5 additions & 2 deletions lib/uploadcare/client/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ def request(uri:, method: 'GET', **options)
request_headers = Param::AuthenticationHeader.call(method: method.upcase, uri: uri,
content_type: headers[:'Content-Type'], **options)
handle_throttling do
send("api_struct_#{method.downcase}", path: remove_trailing_slash(uri),
headers: request_headers, body: options[:content])
send("api_struct_#{method.downcase}",
path: remove_trailing_slash(uri),
headers: request_headers,
body: options[:content],
params: options[:params])
end
end

Expand Down
40 changes: 27 additions & 13 deletions lib/uploadcare/param/secure_auth_header.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# frozen_string_literal: true

require 'digest/md5'
require 'addressable/uri'

module Uploadcare
module Param
# This object returns headers needed for authentication
# This authentication method is more secure, but more tedious
class SecureAuthHeader
# @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
def self.call(options = {})
@method = options[:method]
@body = options[:content] || ''
@content_type = options[:content_type]
@uri = options[:uri]
@date_for_header = timestamp
{
Date: @date_for_header,
Authorization: "Uploadcare #{Uploadcare.config.public_key}:#{signature}"
}
end

class << self
# @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
def call(options = {})
@method = options[:method]
@body = options[:content] || ''
@content_type = options[:content_type]
@uri = make_uri(options)

@date_for_header = timestamp
{
Date: @date_for_header,
Authorization: "Uploadcare #{Uploadcare.config.public_key}:#{signature}"
}
end

def signature
content_md5 = Digest::MD5.hexdigest(@body)
sign_string = [@method, content_md5, @content_type, @date_for_header, @uri].join("\n")
Expand All @@ -31,6 +33,18 @@ def signature
def timestamp
Time.now.gmtime.strftime('%a, %d %b %Y %H:%M:%S GMT')
end

private

def make_uri(options)
if options[:params] && !options[:params].empty?
uri = Addressable::URI.parse options[:uri]
uri.query_values = uri.query_values(Array).to_a.concat(options[:params].to_a)
uri.to_s
else
options[:uri]
end
end
end
end
end
Expand Down
58 changes: 57 additions & 1 deletion spec/fixtures/vcr_cassettes/rest_file_info.yml

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

9 changes: 9 additions & 0 deletions spec/uploadcare/client/file_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ module Client
end
end

it 'supports extra params like include' do
VCR.use_cassette('rest_file_info') do
uuid = '640fe4b7-7352-42ca-8d87-0e4387957157'
file = subject.info(uuid, { include: 'appdata' })
expect(file.value![:uuid]).to eq(uuid)
expect(file.value![:appdata]).not_to be_empty
end
end

it 'shows nothing on invalid file' do
VCR.use_cassette('rest_file_info_fail') do
uuid = 'nonexistent'
Expand Down
Loading