Class: ResourceSpace::Client
- Inherits:
-
Object
- Object
- ResourceSpace::Client
- Defined in:
- lib/resourcespace/client.rb
Overview
Main client class for interacting with ResourceSpace API
Instance Attribute Summary collapse
-
#collections ⇒ Collection
readonly
Collection management interface.
-
#config ⇒ Configuration
readonly
Client configuration.
-
#metadata ⇒ Metadata
readonly
Metadata management interface.
-
#resources ⇒ Resource
readonly
Resource management interface.
-
#search ⇒ Search
readonly
Search interface.
-
#users ⇒ User
readonly
User management interface.
Instance Method Summary collapse
-
#connection ⇒ Faraday::Connection
private
Get the Faraday connection instance.
-
#download_file(download_url, file_path) ⇒ Boolean
Download a file from ResourceSpace.
-
#generate_signature(query_string) ⇒ String
private
Generate SHA256 signature for API authentication.
-
#get(function, params = {}) ⇒ Hash
Make a GET request to the ResourceSpace API.
-
#handle_error_response(response) ⇒ Object
private
Handle error responses and raise appropriate exceptions.
-
#handle_response(response) ⇒ Hash
private
Handle API response and parse JSON.
-
#initialize(url: nil, user: nil, private_key: nil, config: nil, **options) ⇒ Client
constructor
Initialize a new ResourceSpace client.
-
#mime_type_for_file(file_path) ⇒ String
private
Get MIME type for a file.
-
#parse_json_response(body) ⇒ Hash
private
Parse JSON response body.
-
#post(function, params = {}) ⇒ Hash
Make a POST request to the ResourceSpace API.
-
#request(method, function, params = {}, multipart: false) ⇒ Hash
private
Make an HTTP request to the ResourceSpace API.
-
#test_connection ⇒ Hash
Test the API connection.
-
#upload_file(file, params = {}) ⇒ Hash
Upload a file to ResourceSpace.
Constructor Details
#initialize(url: nil, user: nil, private_key: nil, config: nil, **options) ⇒ Client
Initialize a new ResourceSpace client
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/resourcespace/client.rb', line 44 def initialize(url: nil, user: nil, private_key: nil, config: nil, **) @config = config || ResourceSpace.config.dup # Set configuration from parameters @config.url = url if url @config.user = user if user @config.private_key = private_key if private_key # Apply additional options .each { |key, value| @config.public_send("#{key}=", value) if @config.respond_to?("#{key}=") } # Validate configuration @config.validate! # Initialize API interfaces @resources = Resource.new(self) @collections = Collection.new(self) @search = Search.new(self) @users = User.new(self) = Metadata.new(self) end |
Instance Attribute Details
#collections ⇒ Collection (readonly)
Returns collection management interface.
26 27 28 |
# File 'lib/resourcespace/client.rb', line 26 def collections @collections end |
#config ⇒ Configuration (readonly)
Returns client configuration.
20 21 22 |
# File 'lib/resourcespace/client.rb', line 20 def config @config end |
#metadata ⇒ Metadata (readonly)
Returns metadata management interface.
35 36 37 |
# File 'lib/resourcespace/client.rb', line 35 def end |
#resources ⇒ Resource (readonly)
Returns resource management interface.
23 24 25 |
# File 'lib/resourcespace/client.rb', line 23 def resources @resources end |
#search ⇒ Search (readonly)
Returns search interface.
29 30 31 |
# File 'lib/resourcespace/client.rb', line 29 def search @search end |
#users ⇒ User (readonly)
Returns user management interface.
32 33 34 |
# File 'lib/resourcespace/client.rb', line 32 def users @users end |
Instance Method Details
#connection ⇒ Faraday::Connection (private)
Get the Faraday connection instance
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/resourcespace/client.rb', line 172 def connection @connection ||= Faraday.new(url: config.url) do |conn| conn.request :multipart conn.request :url_encoded conn.adapter Faraday.default_adapter # Set timeout conn..timeout = config.timeout # Set headers conn.headers["User-Agent"] = config.user_agent config.default_headers.each { |key, value| conn.headers[key] = value } # Add response middleware conn.response :logger, config.logger if config.debug && config.logger end end |
#download_file(download_url, file_path) ⇒ Boolean
Download a file from ResourceSpace
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/resourcespace/client.rb', line 105 def download_file(download_url, file_path) response = connection.get(download_url) if response.success? File.write(file_path, response.body) true else handle_error_response(response) end end |
#generate_signature(query_string) ⇒ String (private)
Generate SHA256 signature for API authentication
165 166 167 |
# File 'lib/resourcespace/client.rb', line 165 def generate_signature(query_string) Digest::SHA256.hexdigest("#{config.private_key}#{query_string}") end |
#get(function, params = {}) ⇒ Hash
Make a GET request to the ResourceSpace API
71 72 73 |
# File 'lib/resourcespace/client.rb', line 71 def get(function, params = {}) request(:get, function, params) end |
#handle_error_response(response) ⇒ Object (private)
Handle error responses and raise appropriate exceptions
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/resourcespace/client.rb', line 207 def handle_error_response(response) = "HTTP #{response.status}" # Try to extract error message from response body if response.body && !response.body.empty? begin parsed_body = JSON.parse(response.body) = parsed_body["error"] || parsed_body["message"] || rescue JSON::ParserError = response.body.length > 200 ? "#{response.body[0..200]}..." : response.body end end raise ResourceSpace.from_response(response.status, , response.body) end |
#handle_response(response) ⇒ Hash (private)
Handle API response and parse JSON
195 196 197 198 199 200 201 |
# File 'lib/resourcespace/client.rb', line 195 def handle_response(response) if response.success? parse_json_response(response.body) else handle_error_response(response) end end |
#mime_type_for_file(file_path) ⇒ String (private)
Get MIME type for a file
240 241 242 243 |
# File 'lib/resourcespace/client.rb', line 240 def mime_type_for_file(file_path) require "mime/types" MIME::Types.type_for(file_path).first&.content_type || "application/octet-stream" end |
#parse_json_response(body) ⇒ Hash (private)
Parse JSON response body
228 229 230 231 232 233 234 |
# File 'lib/resourcespace/client.rb', line 228 def parse_json_response(body) return {} if body.nil? || body.empty? JSON.parse(body) rescue JSON::ParserError => e raise ParseError.new("Failed to parse JSON response: #{e.message}", data: { body: body }) end |
#post(function, params = {}) ⇒ Hash
Make a POST request to the ResourceSpace API
80 81 82 |
# File 'lib/resourcespace/client.rb', line 80 def post(function, params = {}) request(:post, function, params) end |
#request(method, function, params = {}, multipart: false) ⇒ Hash (private)
Make an HTTP request to the ResourceSpace API
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/resourcespace/client.rb', line 132 def request(method, function, params = {}, multipart: false) # Prepare base parameters request_params = { user: config.user, function: function }.merge(params) # Build query string for signing query_string = URI.encode_www_form(request_params.reject { |_k, v| v.is_a?(Faraday::UploadIO) }) # Generate signature signature = generate_signature(query_string) request_params[:sign] = signature request_params[:authmode] = config.auth_mode # Make the request response = if method == :get connection.get("", request_params) else if multipart connection.post("", request_params) else connection.post("", URI.encode_www_form(request_params)) end end handle_response(response) end |
#test_connection ⇒ Hash
Test the API connection
119 120 121 |
# File 'lib/resourcespace/client.rb', line 119 def test_connection get("get_system_status") end |
#upload_file(file, params = {}) ⇒ Hash
Upload a file to ResourceSpace
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/resourcespace/client.rb', line 89 def upload_file(file, params = {}) file_param = if file.is_a?(String) Faraday::UploadIO.new(file, mime_type_for_file(file)) else Faraday::UploadIO.new(file, mime_type_for_file(file.path)) end params = params.merge(filedata: file_param) request(:post, "upload_file", params, multipart: true) end |