Skip to content

Commit

Permalink
feat: Support params in Rest client and in file info method.
Browse files Browse the repository at this point in the history
- Fix Uploadcare::Param::SecureAuthHeader to allow accepting query params that can be passed down to http requests
- Add make_uri method to support the same, with or without query params options being passed down
- Group and move call method to class << self instead
- In RestClient start passing down params when calling api struct request
 - Start passing down params from info method to get
 - Cover with new specs
  • Loading branch information
vipulnsward committed Feb 24, 2024
1 parent 27899c5 commit 28730ce
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 18 deletions.
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
8 changes: 6 additions & 2 deletions lib/uploadcare/client/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ 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
39 changes: 26 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,17 @@ 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.

12 changes: 12 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,18 @@ module Client
end
end

it 'supports extra params like include' do
Uploadcare.config.public_key = "5d5bb5639e3f2df33674"
Uploadcare.config.secret_key = "159845b6ca81012c106c"

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

0 comments on commit 28730ce

Please sign in to comment.