This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- a Client instance can create a tranfer (and add files on the go) - split Transfers' .new and .create, - new only initializes, - create also sends it to WeTransfers' Public API - need to have a seperate add_file method to make this work - MiniIO introduced to get wrap all things IO, in a very slim interface
- Loading branch information
1 parent
5387a3f
commit dfd22cf
Showing
32 changed files
with
707 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.6.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module WeTransfer | ||
class CommunicationError < StandardError; end | ||
module CommunicationHelper | ||
API_URI_BASE = 'https://dev.wetransfer.com'.freeze | ||
|
||
class << self | ||
attr_accessor :logger, :api_key | ||
end | ||
|
||
def logger | ||
@logger ||= CommunicationHelper.logger | ||
end | ||
|
||
def api_key | ||
@api_key ||= CommunicationHelper.api_key | ||
end | ||
|
||
def request_as | ||
authorize_if_no_bearer_token! | ||
|
||
@request_as ||= Faraday.new(API_URI_BASE) do |c| | ||
c.response :logger, logger | ||
c.adapter Faraday.default_adapter | ||
c.headers = auth_headers.merge( | ||
"User-Agent" => "WetransferRubySdk/#{WeTransfer::VERSION} Ruby #{RUBY_VERSION}", | ||
"Content-Type" => "application/json", | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def auth_headers | ||
authorize_if_no_bearer_token! | ||
|
||
{ | ||
'X-API-Key' => api_key, | ||
'Authorization' => ('Bearer %s' % @bearer_token), | ||
} | ||
end | ||
|
||
def ensure_ok_status!(response) | ||
case response.status | ||
when 200..299 | ||
true | ||
when 400..499 | ||
logger.error response | ||
raise WeTransfer::CommunicationError, JSON.parse(response.body)["message"] | ||
when 500..504 | ||
logger.error response | ||
raise WeTransfer::CommunicationError, "Response had a #{response.status} code, we could retry" | ||
else | ||
logger.error response | ||
raise WeTransfer::CommunicationError, "Response had a #{response.status} code, no idea what to do with that" | ||
end | ||
end | ||
|
||
def authorize_if_no_bearer_token! | ||
return @bearer_token if @bearer_token | ||
|
||
response = Faraday.new(API_URI_BASE) do |c| | ||
c.response :logger, logger | ||
c.adapter Faraday.default_adapter | ||
c.headers = { | ||
"User-Agent" => "WetransferRubySdk/#{WeTransfer::VERSION} Ruby #{RUBY_VERSION}", | ||
"Content-Type" => "application/json", | ||
} | ||
end.post( | ||
'/v2/authorize', | ||
'', | ||
'Content-Type' => 'application/json', | ||
'X-API-Key' => CommunicationHelper.api_key, | ||
) | ||
ensure_ok_status!(response) | ||
bearer_token = JSON.parse(response.body)['token'] | ||
if bearer_token.nil? || bearer_token.empty? | ||
raise WeTransfer::CommunicationError, "The authorization call returned #{response.body} and no usable :token key could be found there" | ||
end | ||
@bearer_token = bearer_token | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module WeTransfer | ||
# Wrapper around an IO object, delegating only methods we need for creating | ||
# and sending a file in chunks. | ||
# | ||
class MiniIO | ||
# Initialize with an io object to wrap | ||
# | ||
# @param io [anything] An object that responds to #read, #rewind, #seek, #size | ||
# | ||
def initialize(io) | ||
@io = io | ||
end | ||
|
||
def read(*args) | ||
@io.read(*args) | ||
end | ||
|
||
def rewind | ||
@io.rewind | ||
end | ||
|
||
def seek(*args) | ||
@io.seek(*args) | ||
end | ||
|
||
# The size delegated to io. | ||
# If io is nil, we return nil. | ||
# | ||
# nil is fine, since this method is used only as the default size for a | ||
# WeTransferFile | ||
# @returns [Integer, nil] the size of the io. See IO#size | ||
# | ||
def size | ||
@io&.size | ||
end | ||
|
||
# The name of the io, guessed using File.basename. If this raises a TypeError | ||
# we swallow the error, since this is used only as the default name for a | ||
# WeTransferFile | ||
# | ||
def name | ||
File.basename(@io) | ||
rescue TypeError | ||
# yeah, what? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module WeTransfer | ||
class Transfer | ||
class DuplicateFileNameError < ArgumentError; end | ||
class NoFilesAddedError < StandardError; end | ||
|
||
include CommunicationHelper | ||
|
||
def self.create(message:, &block) | ||
transfer = new(message: message) | ||
|
||
transfer.persist(&block) | ||
end | ||
|
||
def initialize(message:) | ||
@message = message | ||
@files = [] | ||
@unique_file_names = Set.new | ||
end | ||
|
||
# Add files (if still needed) | ||
def persist | ||
yield(self) if block_given? | ||
|
||
create_remote_transfer | ||
|
||
## files should now be in persisted status | ||
|
||
end | ||
|
||
# Add one or more files to a transfer, so a transfer can be created over the | ||
# WeTransfer public API | ||
# | ||
# @param name [String] (nil) the name of the file | ||
# | ||
# @return [WeTransfer::Client] | ||
def add_file(name: nil, size: nil, io: nil) | ||
file = WeTransferFile.new(name: name, size: size, io: io) | ||
raise DuplicateFileNameError unless @unique_file_names.add?(file.name.downcase) | ||
|
||
@files << file | ||
self | ||
end | ||
|
||
private | ||
|
||
def as_json_request_params | ||
{ | ||
message: @message, | ||
files: @files.map(&:as_json_request_params), | ||
} | ||
end | ||
|
||
def create_remote_transfer | ||
raise NoFilesAddedError if @unique_file_names.empty? | ||
|
||
response = request_as.post( | ||
'/v2/transfers', | ||
as_json_request_params.to_json, | ||
{} | ||
) | ||
ensure_ok_status!(response) | ||
|
||
@remote_transfer = RemoteTransfer.new(JSON.parse(response.body, symbolize_names: true)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module WeTransfer | ||
class WeTransferFile | ||
|
||
attr_reader :name | ||
|
||
def initialize(name: nil, size: nil, io: nil) | ||
@io = io.is_a?(MiniIO) ? io : MiniIO.new(io) | ||
@name = name || @io.name | ||
@size = size || @io.size | ||
|
||
raise ArgumentError, "Need a file name and a size, or io should provide it" unless @name && @size | ||
end | ||
|
||
def as_json_request_params | ||
{ | ||
name: @name, | ||
size: @size, | ||
} | ||
end | ||
|
||
# def persist() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.