# frozen_string_literal: true require "faraday" require "faraday_middleware" require "dry/monads" module Datacite # The connection to DataCite API class Client include Dry::Monads[:result] # @param [String] username # @param [String] password # @param [String] host def initialize(username:, password:, host: "api.test.datacite.org") @conn = Faraday.new( url: "https://#{host}", headers: headers ) do |conn| conn.request :json conn.basic_auth(username, password) conn.response :json end end # @returns [Dry::Monads::Result] def autogenerate_doi(prefix:) request_body = AutogenerateDoi.new(prefix: prefix).to_json response = conn.post("/dois", request_body) response.success? ? Success(Response.new(response)) : Failure(response) end private def headers { "Content-Type" => "application/vnd.api+json", "User-Agent" => "Datacite Ruby client version #{Datacite::VERSION}" } end attr_reader :conn end end