module Ecoportal module API class V2 # @attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. class S3 class MissingLocalFile < ArgumentError; end class CredentialsGetFailed < StandardError; end extend Common::BaseClass class_resolver :data_class, "Ecoportal::API::V2::S3::Data" class_resolver :files_class, "Ecoportal::API::V2::S3::Files" attr_reader :client # @param client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. # @return [Schemas] an instance object ready to make schema api requests. def initialize(client) @client = client end # Gets the S3 credentials to upload files # @return [Ecoportal::API::V2::S3::Data] def data response = client.get("/s3/data") body = body_data(response.body) return data_class.new(body) if response.success? msg = "Could not get S3 credentials - Error #{response.status}: #{body}" raise CredentialsGetFailed, msg end # @param file [String] full path to existing local file # @param credentials [Ecoportal::API::V2::S3::Data, NilClass] # @return [Hash, NilClass] with the s3_file_reference on success, and `nil` otherwise def upload_file(file, credentials: data, &block) msg = "The file '#{file}' does not exist" raise MissingLocalFile, msg unless File.exist?(file) credentials ||= data Upload.new(credentials, file: file).upload!(&block) end # Obtain specific object for eP file api requests. # @return [Files] an instance object ready to make eP file api requests. def files files_class.new(client, s3_api: self) end private def body_data(body) return body unless body.is_a?(Hash) return body unless body.key?("data") body["data"] end end end end end require 'ecoportal/api/v2/s3/data' require 'ecoportal/api/v2/s3/upload' require 'ecoportal/api/v2/s3/files'