Class: GlobusClient

Inherits:
Object
  • Object
show all
Includes:
Errors, Singleton
Defined in:
lib/globus_client.rb,
lib/globus_client/errors.rb,
lib/globus_client/version.rb,
lib/globus_client/endpoint.rb,
lib/globus_client/identity.rb,
lib/globus_client/authenticator.rb,
lib/globus_client/endpoint_manager.rb,
lib/globus_client/unexpected_response.rb

Overview

Client for interacting with the Globus API

Defined Under Namespace

Modules: Errors Classes: Authenticator, Config, Endpoint, EndpointManager, Identity, UnexpectedResponse

Constant Summary collapse

VERSION =
'0.18.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



65
66
67
# File 'lib/globus_client.rb', line 65

def config
  @config
end

Class Method Details

.configure(client_id:, client_secret:, uploads_directory:, transfer_endpoint_id:, transfer_url: default_transfer_url, auth_url: default_auth_url) ⇒ Object

rubocop:disable Metrics/ParameterLists

Parameters:

  • client_id (String)

    the client identifier registered with Globus

  • client_secret (String)

    the client secret to authenticate with Globus

  • uploads_directory (String)

    where to upload files

  • transfer_endpoint_id (String)

    the transfer API endpoint ID supplied by Globus

  • transfer_url (String) (defaults to: default_transfer_url)

    the transfer API URL

  • auth_url (String) (defaults to: default_auth_url)

    the authentication API URL



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/globus_client.rb', line 26

def configure(client_id:, client_secret:, uploads_directory:, transfer_endpoint_id:,
              transfer_url: default_transfer_url, auth_url: default_auth_url)
  instance.config = Config.new(
    # For the initial token, use a dummy value to avoid hitting any APIs
    # during configuration, allowing `with_token_refresh_when_unauthorized` to handle
    # auto-magic token refreshing. Why not immediately get a valid token? Our apps
    # commonly invoke client `.configure` methods in the initializer in all
    # application environments, even those that are never expected to
    # connect to production APIs, such as local development machines.
    #
    # NOTE: `nil` and blank string cannot be used as dummy values here as
    # they lead to a malformed request to be sent, which triggers an
    # exception not rescued by `with_token_refresh_when_unauthorized`
    token: 'a temporary dummy token to avoid hitting the API before it is needed',
    client_id:,
    client_secret:,
    uploads_directory:,
    transfer_endpoint_id:,
    transfer_url:,
    auth_url:
  )

  self
end

.default_auth_urlObject



60
61
62
# File 'lib/globus_client.rb', line 60

def default_auth_url
  'https://auth.globus.org'
end

.default_transfer_urlObject



56
57
58
# File 'lib/globus_client.rb', line 56

def default_transfer_url
  'https://transfer.api.globusonline.org'
end

Instance Method Details

#allow_writesObject



157
158
159
160
161
# File 'lib/globus_client.rb', line 157

def allow_writes(...)
  Endpoint
    .new(...)
    .allow_writes
end

#delete(base_url:, path:) ⇒ Object

Send an authenticated DELETE request

Parameters:

  • base_url (String)

    the base URL of the Globus API

  • path (String)

    the path to the Globus API request



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/globus_client.rb', line 130

def delete(base_url:, path:)
  response = with_token_refresh_when_unauthorized do
    connection(base_url).delete(path) do |request|
      request.headers['Authorization'] = "Bearer #{config.token}"
    end
  end

  UnexpectedResponse.call(response) unless response.success?

  return nil if response.body.blank?

  JSON.parse(response.body)
end

#delete_access_ruleObject



163
164
165
166
167
# File 'lib/globus_client.rb', line 163

def delete_access_rule(...)
  Endpoint
    .new(...)
    .delete_access_rule
end

#disallow_writesObject



151
152
153
154
155
# File 'lib/globus_client.rb', line 151

def disallow_writes(...)
  Endpoint
    .new(...)
    .disallow_writes
end

#exists?Boolean

Returns:

  • (Boolean)


208
209
210
211
212
# File 'lib/globus_client.rb', line 208

def exists?(...)
  Endpoint
    .new(...)
    .exists?
end

#file_countObject



184
185
186
187
188
# File 'lib/globus_client.rb', line 184

def file_count(...)
  Endpoint
    .new(...)
    .list_files { |files| return files.count }
end

#get(base_url:, path:, params: {}, content_type: nil) ⇒ Object

Send an authenticated GET request

Parameters:

  • base_url (String)

    the base URL of the Globus API

  • path (String)

    the path to the Globus API request

  • params (Hash) (defaults to: {})

    params to get to the API



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/globus_client.rb', line 71

def get(base_url:, path:, params: {}, content_type: nil)
  response = with_token_refresh_when_unauthorized do
    connection(base_url).get(path, params) do |request|
      request.headers['Authorization'] = "Bearer #{config.token}"
      request.headers['Content-Type'] = content_type if content_type
    end
  end

  UnexpectedResponse.call(response) unless response.success?

  return nil if response.body.blank?

  JSON.parse(response.body)
end

#get_filenamesObject



196
197
198
199
200
# File 'lib/globus_client.rb', line 196

def get_filenames(...)
  Endpoint
    .new(...)
    .list_files { |files| return files.map(&:name) }
end

#has_files?Boolean

Returns:

  • (Boolean)


202
203
204
205
206
# File 'lib/globus_client.rb', line 202

def has_files?(...)
  Endpoint
    .new(...)
    .has_files?
end

#list_files(**keywords) ⇒ Object

NOTE: Can’t use the ‘…` (argument forwarding) operator here because we

want to route the keyword args to `Endpoint#new` and the block arg to
`Endpoint#list_files`


178
179
180
181
182
# File 'lib/globus_client.rb', line 178

def list_files(**keywords, &)
  Endpoint
    .new(**keywords)
    .list_files(&)
end

#mkdirObject



144
145
146
147
148
149
# File 'lib/globus_client.rb', line 144

def mkdir(...)
  Endpoint.new(...).tap do |endpoint|
    endpoint.mkdir
    endpoint.allow_writes
  end
end

#post(base_url:, path:, body:, expected_response: ->(_resp) { false }) ⇒ Object

Send an authenticated POST request

Parameters:

  • base_url (String)

    the base URL of the Globus API

  • path (String)

    the path to the Globus API request

  • body (String)

    the body of the Globus API request

  • expected_response (#call) (defaults to: ->(_resp) { false })

    an expected response handler to allow short-circuiting the unexpected response



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/globus_client.rb', line 91

def post(base_url:, path:, body:, expected_response: ->(_resp) { false })
  response = with_token_refresh_when_unauthorized do
    connection(base_url).post(path) do |request|
      request.headers['Authorization'] = "Bearer #{config.token}"
      request.headers['Content-Type'] = 'application/json'
      request.body = body.to_json
    end
  end

  UnexpectedResponse.call(response) unless response.success? || expected_response.call(response)

  return nil if response.body.blank?

  JSON.parse(response.body)
end

#put(base_url:, path:, body:) ⇒ Object

Send an authenticated PUT request

Parameters:

  • base_url (String)

    the base URL of the Globus API

  • path (String)

    the path to the Globus API request

  • body (String)

    the body of the Globus API request



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/globus_client.rb', line 111

def put(base_url:, path:, body:)
  response = with_token_refresh_when_unauthorized do
    connection(base_url).put(path) do |request|
      request.headers['Authorization'] = "Bearer #{config.token}"
      request.headers['Content-Type'] = 'application/json'
      request.body = body.to_json
    end
  end

  UnexpectedResponse.call(response) unless response.success?

  return nil if response.body.blank?

  JSON.parse(response.body)
end

#rename(new_path:, **args) ⇒ Object



169
170
171
172
173
# File 'lib/globus_client.rb', line 169

def rename(new_path:, **args)
  Endpoint
    .new(**args)
    .rename(new_path:)
end

#task_listObject



220
221
222
223
224
# File 'lib/globus_client.rb', line 220

def task_list(...)
  EndpointManager
    .new
    .task_list(...)
end

#tasks_in_progress?Boolean

Returns:

  • (Boolean)


226
227
228
229
230
# File 'lib/globus_client.rb', line 226

def tasks_in_progress?(...)
  EndpointManager
    .new
    .tasks_in_progress?(...)
end

#total_sizeObject



190
191
192
193
194
# File 'lib/globus_client.rb', line 190

def total_size(...)
  Endpoint
    .new(...)
    .list_files { |files| return files.sum(&:size) }
end

#user_valid?Boolean

Returns:

  • (Boolean)


214
215
216
217
218
# File 'lib/globus_client.rb', line 214

def user_valid?(...)
  Identity
    .new
    .valid?(...)
end