Class: GlobusClient

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

Overview

Client for interacting with the Globus API

Defined Under Namespace

Classes: Authenticator, Config, Endpoint, Identity, UnexpectedResponse

Constant Summary collapse

VERSION =
'0.14.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



63
64
65
# File 'lib/globus_client.rb', line 63

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



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

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



58
59
60
# File 'lib/globus_client.rb', line 58

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

.default_transfer_urlObject



54
55
56
# File 'lib/globus_client.rb', line 54

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

Instance Method Details

#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



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

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



155
156
157
158
159
# File 'lib/globus_client.rb', line 155

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

#disallow_writesObject



149
150
151
152
153
# File 'lib/globus_client.rb', line 149

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

#file_countObject



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

def file_count(...)
  Endpoint
    .new(self, ...)
    .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



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

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



182
183
184
185
186
# File 'lib/globus_client.rb', line 182

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

#has_files?Boolean

Returns:

  • (Boolean)


188
189
190
191
192
# File 'lib/globus_client.rb', line 188

def has_files?(...)
  Endpoint
    .new(self, ...)
    .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`


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

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

#mkdirObject



142
143
144
145
146
147
# File 'lib/globus_client.rb', line 142

def mkdir(...)
  Endpoint.new(self, ...).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



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

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



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

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

#total_sizeObject



176
177
178
179
180
# File 'lib/globus_client.rb', line 176

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

#user_valid?Boolean

Returns:

  • (Boolean)


194
195
196
197
198
# File 'lib/globus_client.rb', line 194

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