2024-01-26 08:39:03 +00:00
|
|
|
// import { Client } from 'guzzle';
|
|
|
|
// import { Log } from '@adonisjs/core/build/standalone';
|
|
|
|
// import { DoiInterface } from './interfaces/DoiInterface';
|
2024-03-14 19:25:27 +00:00
|
|
|
import DoiClientContract from '#app/Library/Doi/DoiClientContract';
|
2024-04-29 09:25:50 +00:00
|
|
|
import DoiClientException from '#app/exceptions/DoiClientException';
|
2024-01-26 08:39:03 +00:00
|
|
|
import { StatusCodes } from 'http-status-codes';
|
2024-03-14 19:25:27 +00:00
|
|
|
import logger from '@adonisjs/core/services/logger';
|
|
|
|
import { AxiosResponse } from 'axios';
|
|
|
|
import axios from 'axios';
|
2024-01-26 08:39:03 +00:00
|
|
|
|
|
|
|
export class DoiClient implements DoiClientContract {
|
2024-01-31 12:19:46 +00:00
|
|
|
public username: string;
|
|
|
|
public password: string;
|
|
|
|
public serviceUrl: string;
|
2024-01-26 08:39:03 +00:00
|
|
|
|
|
|
|
constructor() {
|
2024-01-31 12:19:46 +00:00
|
|
|
// const datacite_environment = process.env.DATACITE_ENVIRONMENT || 'debug';
|
|
|
|
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 || '';
|
|
|
|
|
2024-01-26 08:39:03 +00:00
|
|
|
if (this.username === '' || this.password === '' || this.serviceUrl === '') {
|
|
|
|
const message = 'issing configuration settings to properly initialize DOI client';
|
2024-03-14 19:25:27 +00:00
|
|
|
logger.error(message);
|
2024-01-26 08:39:03 +00:00
|
|
|
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
|
|
|
|
*
|
2024-01-31 12:19:46 +00:00
|
|
|
* @return Promise<AxiosResponse<any>> The http response in the form of a axios response
|
2024-01-26 08:39:03 +00:00
|
|
|
*/
|
|
|
|
public async registerDoi(doiValue: string, xmlMeta: string, landingPageUrl: string): Promise<AxiosResponse<any>> {
|
|
|
|
//step 1: register metadata via xml upload
|
|
|
|
// state draft
|
2024-01-31 12:19:46 +00:00
|
|
|
// let response;
|
|
|
|
// let url = `${this.serviceUrl}/metadata/${doiValue}`; //https://mds.test.datacite.org/metadata/10.21388/tethys.213
|
2024-01-26 08:39:03 +00:00
|
|
|
const auth = {
|
|
|
|
username: this.username,
|
|
|
|
password: this.password,
|
|
|
|
};
|
|
|
|
let headers = {
|
|
|
|
'Content-Type': 'application/xml;charset=UTF-8',
|
|
|
|
};
|
|
|
|
try {
|
2024-03-14 19:25:27 +00:00
|
|
|
const metadataResponse = await axios.default.put(`${this.serviceUrl}/metadata/${doiValue}`, xmlMeta, { auth, headers });
|
2024-01-26 08:39:03 +00:00
|
|
|
|
2024-01-31 12:19:46 +00:00
|
|
|
// 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
|
|
|
|
// let test = metadataResponse.data; // 'OK (10.21388/TETHYS.213)'
|
|
|
|
if (metadataResponse.status !== 201) {
|
|
|
|
const message = `Unexpected DataCite MDS response code ${metadataResponse.status}`;
|
2024-03-14 19:25:27 +00:00
|
|
|
logger.error(message);
|
2024-01-31 12:19:46 +00:00
|
|
|
throw new DoiClientException(metadataResponse.status, message);
|
|
|
|
}
|
2024-01-26 08:39:03 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
const doiResponse = await axios.default.put(`${this.serviceUrl}/doi/${doiValue}`, `doi=${doiValue}\nurl=${landingPageUrl}`, {
|
2024-01-26 08:39:03 +00:00
|
|
|
auth,
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
|
2024-01-31 12:19:46 +00:00
|
|
|
// 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 (doiResponse.status !== 201) {
|
|
|
|
const message = `Unexpected DataCite MDS response code ${doiResponse.status}`;
|
2024-03-14 19:25:27 +00:00
|
|
|
logger.error(message);
|
2024-01-31 12:19:46 +00:00
|
|
|
throw new DoiClientException(doiResponse.status, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return doiResponse;
|
2024-01-26 08:39:03 +00:00
|
|
|
} catch (error) {
|
2024-01-31 12:19:46 +00:00
|
|
|
// const message = `request for registering DOI failed with ${error.message}`;
|
2024-01-26 08:39:03 +00:00
|
|
|
// Handle the error, log it, or rethrow as needed
|
2024-03-14 19:25:27 +00:00
|
|
|
logger.error(error.message);
|
2024-01-31 12:19:46 +00:00
|
|
|
throw new DoiClientException(error.response.status, error.response.data);
|
2024-01-26 08:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|