Skip to content

Commit 4a3a233

Browse files
simplified Faspex5 specific api call
Signed-off-by: Laurent Martin <laurent.martin.l@gmail.com>
1 parent 099cb06 commit 4a3a233

3 files changed

Lines changed: 24 additions & 36 deletions

File tree

lib/aspera/api/faspex.rb

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ class Faspex < Aspera::Rest
6464
PATH_AUTH = 'auth'
6565
PATH_API_V5 = 'api/v5'
6666
PATH_HEALTH = 'configuration/ping'
67-
private_constant :PATH_AUTH,
68-
:PATH_API_V5,
69-
:PATH_HEALTH
7067
RECIPIENT_TYPES = %w[user workgroup external_user distribution_list shared_inbox].freeze
7168
PACKAGE_TERMINATED = %w[completed failed].freeze
7269
# list of supported mailbox types (to list packages)
@@ -152,6 +149,7 @@ def box_type(box)
152149
def initialize(
153150
url:,
154151
auth:,
152+
root: PATH_API_V5,
155153
password: nil,
156154
client_id: nil,
157155
client_secret: nil,
@@ -165,23 +163,23 @@ def initialize(
165163
super(**
166164
case auth
167165
when :public_link
168-
# Get URL of final redirect of public link
169-
redir_url = Rest.new(base_url: url, redirect_max: 3).call(operation: 'GET', ret: :resp).uri.to_s
170-
Log.dump(:redir_url, redir_url, level: :trace1)
171-
# get context from query
172-
encoded_context = Rest.query_to_h(URI.parse(redir_url).query)['context']
166+
# Get URL of final redirect of provided public link
167+
final_url = Rest.new(base_url: url, redirect_max: 3).call(operation: 'GET', ret: :resp).uri.to_s
168+
Log.dump(:final_url, final_url, level: :trace1)
169+
# Get context from query
170+
encoded_context = Rest.query_to_h(URI.parse(final_url).query)['context']
173171
raise ParameterError, 'Bad faspex5 public link, missing context in query' if encoded_context.nil?
174172
# public link information (contains passcode and allowed usage)
175173
@pub_link_context = JSON.parse(Base64.decode64(encoded_context))
176174
Log.dump(:pub_link_context, @pub_link_context, level: :trace1)
177175
# Get the base url, i.e. .../aspera/faspex
178-
base_url = redir_url.gsub(%r{/public/.*}, '').gsub(/\?.*/, '')
176+
base_url = final_url.gsub(%r{/public/.*}, '').gsub(/\?.*/, '')
179177
# Get web UI client_id and redirect_uri
180178
# TODO: change this for something more reliable
181179
config = JSON.parse(Rest.new(base_url: "#{base_url}/config.js", redirect_max: 3).call(operation: 'GET').sub(/^[^=]+=/, '').gsub(/([a-z_]+):/, '"\1":').delete("\n ").tr("'", '"')).symbolize_keys
182-
Log.dump(:configjs, config)
180+
Log.dump(:config_js, config)
183181
{
184-
base_url: "#{base_url}/#{PATH_API_V5}",
182+
base_url: "#{base_url}/#{root}",
185183
auth: {
186184
type: :oauth2,
187185
base_url: "#{base_url}/#{PATH_AUTH}",
@@ -198,15 +196,15 @@ def initialize(
198196
Aspera.assert(password, type: ParameterError){'Missing password'}
199197
# the password here is the token copied directly from browser in developer mode
200198
{
201-
base_url: "#{url}/#{PATH_API_V5}",
199+
base_url: "#{url}/#{root}",
202200
headers: {'Authorization' => password}
203201
}
204202
when :web
205203
Aspera.assert(client_id, type: ParameterError){'Missing client_id'}
206204
Aspera.assert(redirect_uri, type: ParameterError){'Missing redirect_uri'}
207205
# opens a browser and ask user to auth using web
208206
{
209-
base_url: "#{url}/#{PATH_API_V5}",
207+
base_url: "#{url}/#{root}",
210208
auth: {
211209
type: :oauth2,
212210
base_url: "#{url}/#{PATH_AUTH}",
@@ -221,7 +219,7 @@ def initialize(
221219
Aspera.assert(client_id, type: ParameterError){'Missing client_id'}
222220
Aspera.assert(private_key, type: ParameterError){'Missing private_key'}
223221
{
224-
base_url: "#{url}/#{PATH_API_V5}",
222+
base_url: "#{url}/#{root}",
225223
auth: {
226224
type: :oauth2,
227225
base_url: "#{url}/#{PATH_AUTH}",
@@ -242,10 +240,6 @@ def initialize(
242240
end
243241
)
244242
end
245-
246-
def auth_api
247-
Rest.new(**params, base_url: base_url.sub(PATH_API_V5, PATH_AUTH))
248-
end
249243
end
250244
end
251245
end

lib/aspera/cli/plugins/base.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def do_bulk_operation(command:, descr: nil, values: Hash, id_result: 'id', field
134134
# @param delete_style [String] If set, the delete operation by array in payload
135135
# @param id_as_arg [String] If set, the id is provided as url argument ?<id_as_arg>=<id>
136136
# @param is_singleton [Boolean] If `true`, entity is the full path to the resource
137-
# @param tclo [Boolean] If `true`, :list use paging with total_count, limit, offset
138137
# @param block [Proc] Block to search for identifier based on attribute value
139138
# @return [Hash] Result suitable for CLI result
140139
def entity_execute(
@@ -147,7 +146,6 @@ def entity_execute(
147146
id_as_arg: false,
148147
is_singleton: false,
149148
list_query: nil,
150-
tclo: false,
151149
&block
152150
)
153151
command = options.get_next_command(ALL_OPS) if command.nil?
@@ -185,10 +183,6 @@ def entity_execute(
185183
when :show
186184
return Main.result_single_object(api.read(one_res_path), fields: display_fields)
187185
when :list
188-
if tclo
189-
data, total = api.list_entities_limit_offset_total_count(entity: entity, items_key: items_key, query: query_read_delete(default: list_query))
190-
return Main.result_object_list(data, total: total, fields: display_fields)
191-
end
192186
data, http = api.read(entity, query_read_delete, ret: :both)
193187
return Main.result_empty if http.code == '204'
194188
# TODO: not generic : which application is this for ?

lib/aspera/cli/plugins/faspex5.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,6 @@ def initialize(**_)
100100
options.parse_options!
101101
end
102102

103-
def set_api
104-
# create an API object with the same options, but with a different subpath
105-
@api_v5 = Api::Faspex.new(**Oauth.args_from_options(options))
106-
# in case user wants to use HTTPGW tell transfer agent how to get address
107-
transfer.httpgw_url_cb = lambda{@api_v5.read('account')['gateway_url']}
108-
end
109-
110103
# if recipient is just an email, then convert to expected API hash : name and type
111104
def normalize_recipients(parameters, type)
112105
type = type.to_s
@@ -423,8 +416,7 @@ def package_action
423416
def execute_resource(res_sym)
424417
exec_args = {
425418
api: @api_v5,
426-
entity: res_sym.to_s,
427-
tclo: true
419+
entity: res_sym.to_s
428420
}
429421
res_id_query = :default
430422
available_commands = ALL_OPS
@@ -445,7 +437,7 @@ def execute_resource(res_sym)
445437
available_commands += [:reset_password]
446438
when :oauth_clients
447439
exec_args[:display_fields] = Formatter.all_but('public_key')
448-
exec_args[:api] = @api_v5.auth_api
440+
exec_args[:api] = Api::Faspex.new(root: Api::Faspex::PATH_AUTH, **Oauth.args_from_options(options))
449441
exec_args[:list_query] = {'expand': true, 'no_api_path': true, 'client_types[]': 'public'}
450442
when :shared_inboxes, :workgroups
451443
available_commands += %i[members saml_groups invite_external_collaborator]
@@ -458,10 +450,13 @@ def execute_resource(res_sym)
458450
res_command = options.get_next_command(available_commands)
459451
return Main.result_value_list(Api::Faspex::EMAIL_NOTIF_LIST, name: 'email_id') if res_command.eql?(:list) && res_sym.eql?(:email_notifications)
460452
case res_command
461-
when *ALL_OPS
453+
when :create, :modify, :delete, :show
462454
return entity_execute(command: res_command, **exec_args) do |field, value|
463455
@api_v5.lookup_entity_by_field(entity: exec_args[:entity], value: value, field: field, items_key: exec_args[:items_key], query: res_id_query)['id']
464456
end
457+
when :list
458+
data, total = exec_args[:api].list_entities_limit_offset_total_count(entity: exec_args[:entity], items_key: exec_args[:items_key], query: query_read_delete(default: exec_args[:list_query]))
459+
return Main.result_object_list(data, total: total, fields: exec_args[:display_fields])
465460
when :shared_folders
466461
# nodes
467462
node_id = instance_identifier do |field, value|
@@ -620,7 +615,12 @@ def execute_admin
620615

621616
def execute_action
622617
command = options.get_next_command(ACTIONS)
623-
set_api unless %i{postprocessing health}.include?(command)
618+
unless %i{postprocessing health}.include?(command)
619+
# create an API object with the same options, but with a different subpath
620+
@api_v5 = Api::Faspex.new(**Oauth.args_from_options(options))
621+
# in case user wants to use HTTPGW tell transfer agent how to get address
622+
transfer.httpgw_url_cb = lambda{@api_v5.read('account')['gateway_url']}
623+
end
624624
case command
625625
when :version
626626
return Main.result_single_object(@api_v5.read('version'))

0 commit comments

Comments
 (0)