Skip to content

Commit

Permalink
New: metadataAPI.js (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradJChan authored Jun 6, 2019
1 parent 54b0eb9 commit 5e4de5f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/lib/__tests__/metadataAPI-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable no-unused-expressions */
import metadataAPI from '../metadataAPI';
import api from '../api';

const sandbox = sinon.sandbox.create();
let stubs = {};

describe('metadataAPI', () => {
beforeEach(() => {
stubs = {};
stubs.get = sandbox.stub(api, 'get');
});

afterEach(() => {
sandbox.verifyAndRestore();
});

describe('getXrefsMetadata', () => {
it('Should reject the promise if id is not provided on the file', () => {
return metadataAPI.getXrefsMetadata(null, 'autocad').catch((err) => {
expect(stubs.get).not.to.have.been.called;
expect(err instanceof Error).to.be.true;
});
});

it('Should reject the promise if template is not provided on the file', () => {
return metadataAPI.getXrefsMetadata('123').catch((err) => {
expect(stubs.get).not.to.have.been.called;
expect(err instanceof Error).to.be.true;
});
});

it('Should return global template on api success', () => {
const expResponse = { hasxrefs: 'true' };
stubs.get.resolves(expResponse);

return metadataAPI.getXrefsMetadata('123', 'autocad').then((response) => {
expect(stubs.get).to.have.been.called;
expect(response).to.eql(expResponse);
});
});

it('Should return manufactured global template on api 404', () => {
const expResponse = { hasxrefs: 'false' };
stubs.get.rejects({ response: { status: 404 } });

return metadataAPI.getXrefsMetadata('123', 'autocad').then((response) => {
expect(stubs.get).to.have.been.called;
expect(response).to.eql(expResponse);
});
});

it('Should return an error for any other http 4xx', () => {
const expResponse = { response: { status: 400 } };
stubs.get.rejects(expResponse);

return metadataAPI.getXrefsMetadata('123', 'autocad').catch((err) => {
expect(stubs.get).to.have.been.called;
expect(err).to.eql(expResponse);
});
});
});
});
57 changes: 57 additions & 0 deletions src/lib/metadataAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import api from './api';
import { getHeaders } from './util';

const metadataAPI = {
/**
* Gets the global metadata xrefs template for the specified file
* @param {string} id - The file id
* @param {string} template - The global metadata template
* @param {Object} [options] - options object
* @return {Promise} Promise is resolved or rejected based on response
*/
getXrefsMetadata(id, template, options = {}) {
if (!id || !template) {
return Promise.reject(new Error('id and template are required parameters'));
}

return metadataAPI.getMetadata(id, 'global', template, options).catch((err) => {
const { response } = err;
// If the http response is 404, this is a valid case because the metadata template
// may not be initialized on the requested file. Resolve the promise with
// a constructed hasxrefs value
if (response && response.status === 404) {
return Promise.resolve({ hasxrefs: 'false' });
}

throw err;
});
},

/**
* Makes network request to fetch metadata
* @param {string} id - File id
* @param {string} scope - Metadata template scope
* @param {string} template - Metadata template
* @param {Object} [options] - options object
* @return {Promise} XHR promise
*/
getMetadata(id, scope, template, options = {}) {
const { apiHost, token } = options;

return api.get(metadataAPI.getMetadataURL(id, scope, template, apiHost), { headers: getHeaders({}, token) });
},

/**
* Gets the metadata URL given the file and template
* @param {string} fileId - File id
* @param {string} scope - Metadata scope
* @param {string} template - Metadata template
* @param {string} apiHost - API host
* @return {string} The metadata url
*/
getMetadataURL(fileId, scope = 'global', template, apiHost) {
return `${apiHost}/2.0/files/${fileId}/metadata/${scope}/${template}`;
}
};

export default metadataAPI;

0 comments on commit 5e4de5f

Please sign in to comment.