Class: LUSI::API::Core::API
- Inherits:
-
Object
- Object
- LUSI::API::Core::API
- Defined in:
- lib/lusi_api/core/api.rb
Overview
Provides a wrapper for the LUSI web service API
Instance Method Summary collapse
-
#call(path, endpoint, method, headers: nil, options: nil, **params) ⇒ Nokogiri::XML::Node
(also: #post)
Calls a LUSI API method and return the parsed XML response Any undocumented keyword parameters are passed as parameters to the LUSI API call.
-
#get_service_account_details ⇒ LUSI::API::ServiceAccount
Return a ServiceAccount instance representing the API’s service user.
-
#initialize(api_user: nil, api_password: nil, api_root: nil, logger: nil, timeout: nil) ⇒ API
constructor
Initializes a new API instance.
-
#update_service_account_password(password = nil) ⇒ String
Update the service user’s password via the LUSI API.
-
#url(path = nil, endpoint = nil, method = nil) ⇒ String
Construct a full API method URL from its constituents.
Constructor Details
#initialize(api_user: nil, api_password: nil, api_root: nil, logger: nil, timeout: nil) ⇒ API
Initializes a new API instance
22 23 24 25 26 27 28 |
# File 'lib/lusi_api/core/api.rb', line 22 def initialize(api_user: nil, api_password: nil, api_root: nil, logger: nil, timeout: nil) @api_password = api_password @api_root = api_root || 'https://lusiservice.lancs.ac.uk' @api_user = api_user @logger = logger @timeout = timeout.to_i end |
Instance Method Details
#call(path, endpoint, method, headers: nil, options: nil, **params) ⇒ Nokogiri::XML::Node Also known as: post
Calls a LUSI API method and return the parsed XML response Any undocumented keyword parameters are passed as parameters to the LUSI API call. The LUSI ‘Username’ and ‘Password’ parameters are automatically added.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lusi_api/core/api.rb', line 42 def call(path, endpoint, method, headers: nil, options: nil, **params) # Add the username and password to the params params[:Username] ||= @api_user params[:Password] ||= @api_password # Set the REST client headers rest_headers = {}.merge(headers || {}) # Set the REST client options = { method: :post, headers: rest_headers, payload: params, url: url(path, endpoint, method), } [:timeout] = @timeout if @timeout > 0 .merge() if RestClient.log = @log if @log # Make the API call begin response = RestClient::Request.execute(**) rescue RestClient::Exceptions::Timeout => e raise APITimeoutError.new(url: [:url]) rescue RestClient::Exception => e raise APICallError.new(url: [:url]) rescue => e raise APIError.new(url: [:url]) end # Extract and return the XML content xml(response) end |
#get_service_account_details ⇒ LUSI::API::ServiceAccount
Return a ServiceAccount instance representing the API’s service user
81 82 83 84 85 |
# File 'lib/lusi_api/core/api.rb', line 81 def get_service_account_details # There should only be one instance but get_instance returns an array for consistnecy with other classes result = LUSI::API::ServiceAccount.get_instance(self) result.length == 0 ? nil : result[0] end |
#update_service_account_password(password = nil) ⇒ String
Update the service user’s password via the LUSI API
94 95 96 |
# File 'lib/lusi_api/core/api.rb', line 94 def update_service_account_password(password = nil) @api_password = LUSI::API::ServiceAccount.update_service_account_password(self, password) end |
#url(path = nil, endpoint = nil, method = nil) ⇒ String
Construct a full API method URL from its constituents
103 104 105 |
# File 'lib/lusi_api/core/api.rb', line 103 def url(path = nil, endpoint = nil, method = nil) "#{@api_root}/#{path}/#{endpoint}/#{method}" end |