Class: SdrClient::RedesignedClient

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sdr_client/redesigned_client.rb,
lib/sdr_client/redesigned_client/cli.rb,
lib/sdr_client/redesigned_client/file.rb,
lib/sdr_client/redesigned_client/find.rb,
lib/sdr_client/redesigned_client/deposit.rb,
lib/sdr_client/redesigned_client/file_set.rb,
lib/sdr_client/redesigned_client/metadata.rb,
lib/sdr_client/redesigned_client/cli/update.rb,
lib/sdr_client/redesigned_client/job_status.rb,
lib/sdr_client/redesigned_client/upload_files.rb,
lib/sdr_client/redesigned_client/authenticator.rb,
lib/sdr_client/redesigned_client/operations/md5.rb,
lib/sdr_client/redesigned_client/cli/credentials.rb,
lib/sdr_client/redesigned_client/create_resource.rb,
lib/sdr_client/redesigned_client/operations/sha1.rb,
lib/sdr_client/redesigned_client/request_builder.rb,
lib/sdr_client/redesigned_client/update_resource.rb,
lib/sdr_client/redesigned_client/structural_grouper.rb,
lib/sdr_client/redesigned_client/unexpected_response.rb,
lib/sdr_client/redesigned_client/operations/mime_type.rb,
lib/sdr_client/redesigned_client/direct_upload_request.rb,
lib/sdr_client/redesigned_client/direct_upload_response.rb,
lib/sdr_client/redesigned_client/image_file_set_strategy.rb,
lib/sdr_client/redesigned_client/file_type_file_set_strategy.rb,
lib/sdr_client/redesigned_client/structural_metadata_builder.rb,
lib/sdr_client/redesigned_client/single_file_grouping_strategy.rb,
lib/sdr_client/redesigned_client/upload_files_metadata_builder.rb,
lib/sdr_client/redesigned_client/matching_file_grouping_strategy.rb,
lib/sdr_client/redesigned_client/update_dro_with_file_identifiers.rb

Overview

The SDR client reimagined, built using patterns successfully used in other client gems we maintain

Defined Under Namespace

Modules: Operations Classes: Authenticator, CLI, Config, CreateResource, Deposit, DirectUploadRequest, DirectUploadResponse, File, FileSet, FileTypeFileSetStrategy, Find, ImageFileSetStrategy, JobStatus, MatchingFileGroupingStrategy, Metadata, RequestBuilder, SingleFileGroupingStrategy, StructuralGrouper, StructuralMetadataBuilder, UnexpectedResponse, UpdateDroWithFileIdentifiers, UpdateResource, UploadFiles, UploadFilesMetadataBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



71
72
73
# File 'lib/sdr_client/redesigned_client.rb', line 71

def config
  @config
end

Class Method Details

.configure(url:, email: nil, password: nil, token_refresher: nil, token: default_token, request_options: default_request_options, logger: default_logger) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/ParameterLists



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sdr_client/redesigned_client.rb', line 16

def configure(url:, email: nil, password: nil, token_refresher: nil, token: default_token,
              request_options: default_request_options, logger: default_logger)
  if email.blank? && password.blank? && !token_refresher.respond_to?(:call)
    raise ArgumentError, 'email and password cannot be blank without a custom token refresher callable'
  end

  instance.config = Config.new(
    token: token,
    url: url,
    email: email,
    password: password,
    request_options: request_options,
    logger: logger,
    token_refresher: token_refresher
  )

  instance
end

.default_loggerObject



50
51
52
# File 'lib/sdr_client/redesigned_client.rb', line 50

def default_logger
  Logger.new($stdout)
end

.default_request_optionsObject



54
55
56
57
58
59
# File 'lib/sdr_client/redesigned_client.rb', line 54

def default_request_options
  {
    read_timeout: default_timeout,
    timeout: default_timeout
  }
end

.default_timeoutObject

NOTE: This is the number of seconds it roughly takes for H2 to successfully shunt ~10GB files over to SDR API



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

def default_timeout
  900
end

.default_tokenObject

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



46
47
48
# File 'lib/sdr_client/redesigned_client.rb', line 46

def default_token
  'a temporary dummy token to avoid hitting the API before it is needed'
end

Instance Method Details

#build_and_depositObject



89
90
91
# File 'lib/sdr_client/redesigned_client.rb', line 89

def build_and_deposit(...)
  Metadata.deposit(...)
end

#deposit_modelObject



73
74
75
# File 'lib/sdr_client/redesigned_client.rb', line 73

def deposit_model(...)
  Deposit.deposit_model(...)
end

#findObject



81
82
83
# File 'lib/sdr_client/redesigned_client.rb', line 81

def find(...)
  Find.run(...)
end

#get(path:) ⇒ Object

Send an authenticated GET request

Parameters:

  • path (String)

    the path to the SDR API request



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sdr_client/redesigned_client.rb', line 95

def get(path:)
  response = with_token_refresh_when_unauthorized do
    connection.get(path)
  end

  UnexpectedResponse.call(response) unless response.success?

  return nil if response.body.blank?

  JSON.parse(response.body).with_indifferent_access
end

#job_statusObject



77
78
79
# File 'lib/sdr_client/redesigned_client.rb', line 77

def job_status(...)
  JobStatus.new(...)
end

#post(path:, body:, headers: {}, expected_status: nil) ⇒ Object

Send an authenticated POST request

Parameters:

  • path (String)

    the path to the SDR API request

  • body (String)

    the body of the SDR API request

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

    extra headers to add to the SDR API request

  • expected_status (Integer) (defaults to: nil)

    override if all 2xx statuses aren't success conditions



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sdr_client/redesigned_client.rb', line 112

def post(path:, body:, headers: {}, expected_status: nil) # rubocop:disable Metrics/MethodLength
  response = with_token_refresh_when_unauthorized do
    connection.post(path) do |request|
      request.body = body
      request.headers = default_headers.merge(headers)
    end
  end

  if expected_status
    UnexpectedResponse.call(response) if response.status != expected_status
  elsif !response.success?
    UnexpectedResponse.call(response)
  end

  return nil if response.body.blank?

  JSON.parse(response.body).with_indifferent_access
end

#put(path:, body:, headers: {}, params: {}, expected_status: nil) ⇒ Object

Send an authenticated PUT request

Parameters:

  • path (String)

    the path to the SDR API request

  • body (String)

    the body of the SDR API request

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

    extra headers to add to the SDR API request

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

    query parameters to add to the SDR API request

  • expected_status (Integer) (defaults to: nil)

    override if all 2xx statuses aren't success conditions



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sdr_client/redesigned_client.rb', line 137

def put(path:, body:, headers: {}, params: {}, expected_status: nil) # rubocop:disable Metrics/MethodLength
  response = with_token_refresh_when_unauthorized do
    connection.put(path) do |request|
      request.body = body
      request.headers = default_headers.merge(headers)
      request.params = params if params.present?
    end
  end

  if expected_status
    UnexpectedResponse.call(response) if response.status != expected_status
  elsif !response.success?
    UnexpectedResponse.call(response)
  end

  return nil if response.body.blank?

  JSON.parse(response.body).with_indifferent_access
end

#update_modelObject



85
86
87
# File 'lib/sdr_client/redesigned_client.rb', line 85

def update_model(...)
  UpdateResource.run(...)
end