-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54b0eb9
commit 5e4de5f
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |