// import { Client } from 'guzzle'; // import { Log } from '@adonisjs/core/build/standalone'; // import { DoiInterface } from './interfaces/DoiInterface'; import DoiClientContract from 'App/Library/Doi/DoiClientContract'; import DoiClientException from 'App/Exceptions/DoiClientException'; import { StatusCodes } from 'http-status-codes'; import Logger from '@ioc:Adonis/Core/Logger'; import axios, {AxiosResponse} from 'axios'; export class DoiClient implements DoiClientContract { username: string; password: string; serviceUrl: string; // prefix: string; // base_domain: string; constructor() { const datacite_environment = process.env.DATACITE_ENVIRONMENT || 'debug'; if (datacite_environment === 'debug') { this.username = process.env.DATACITE_TEST_USERNAME || ''; this.password = process.env.DATACITE_TEST_PASSWORD || ''; this.serviceUrl = process.env.DATACITE_TEST_SERVICE_URL || ''; // this.prefix = process.env.DATACITE_TEST_PREFIX || ''; // this.base_domain = process.env.TEST_BASE_DOMAIN || ''; } else if (datacite_environment === 'production') { this.username = process.env.DATACITE_USERNAME || ''; this.password = process.env.DATACITE_PASSWORD || ''; this.serviceUrl = process.env.DATACITE_SERVICE_URL || ''; // this.prefix = process.env.DATACITE_PREFIX || ''; // this.base_domain = process.env.BASE_DOMAIN || ''; } if (this.username === '' || this.password === '' || this.serviceUrl === '') { const message = 'issing configuration settings to properly initialize DOI client'; Logger.error(message); throw new DoiClientException(StatusCodes.BAD_REQUEST, message); } } /** * Creates a DOI with the given identifier * * @param doiValue The desired DOI identifier e.g. '10.5072/tethys.999', * @param xmlMeta * @param landingPageUrl e.g. https://www.tethys.at/dataset/1 * * @return Promise The http response in the form of a axios response */ public async registerDoi(doiValue: string, xmlMeta: string, landingPageUrl: string): Promise> { //step 1: register metadata via xml upload // state draft let response; let url = `${this.serviceUrl}/metadata/${doiValue}`; //https://mds.test.datacite.org/metadata/10.21388/tethys.213 const auth = { username: this.username, password: this.password, }; let headers = { 'Content-Type': 'application/xml;charset=UTF-8', }; try { response = await axios.put(url, xmlMeta, { auth, headers, }); } catch (error) { const message = `request to ${url} failed with ${error.message}`; // Handle the error, log it, or rethrow as needed Logger.error(message); throw new DoiClientException(StatusCodes.SERVICE_UNAVAILABLE, message); } // let test = response.data; // 'OK (10.21388/TETHYS.213)' // Response Codes // 201 Created: operation successful // 401 Unauthorised: no login // 403 Forbidden: login problem, quota exceeded // 415 Wrong Content Type : Not including content type in the header. // 422 Unprocessable Entity : invalid XML if (response.status !== 201) { const message = 'unexpected DataCite MDS response code ' + response.status; // $this->log($message, 'err'); throw new DoiClientException(response.status, message); } // step 2: Register the DOI name // // DOI und URL der Frontdoor des zugehörigen Dokuments übergeben: state findable // const url2 = this.serviceUrl + "/doi/" + doiValue; url = `${this.serviceUrl}/doi/${doiValue}`; //'https://mds.test.datacite.org/doi/10.21388/tethys.213' headers = { 'Content-Type': 'text/plain;charset=UTF-8', }; const data = `doi=${doiValue}\nurl=${landingPageUrl}`; try { response = await axios.put(url, data, { auth, headers, }); // Access the response data using response.data // Do something with the response.data } catch (error) { const message = `request to ${url} failed with ${error.message}`; // Handle the error, log it, or rethrow as needed throw new DoiClientException(response.status, message); } // Response Codes // 201 Created: operation successful // 400 Bad Request: request body must be exactly two lines: DOI and URL; wrong domain, wrong prefix; // 401 Unauthorised: no login // 403 Forbidden: login problem, quota exceeded // 412 Precondition failed: metadata must be uploaded first. if (response.status != 201) { const message = 'unexpected DataCite MDS response code ' + response.status; Logger.error(message); throw new DoiClientException(response.status, message); } return response; } }