Compilers API changes (#2170)

* dont return all compilerproperties always in /api/compilers

* fields=all ipv ?all

* use fields=all with remote instances

* add to documentation

* cover more paths with tests
This commit is contained in:
Patrick Quist
2020-09-02 21:36:02 +02:00
committed by GitHub
parent 0f71fc0146
commit ab2f8e209a
4 changed files with 82 additions and 2 deletions

View File

@@ -24,6 +24,16 @@ Returns a list of compilers. In text form, there's a simple formatting of the
information is returned as an array of compilers, with the `id` key being the
primary identifier of each compiler.
Due to the amount of compilers and information available through this api call,
by default you will only get these fields per compiler:
`['id', 'name', 'lang', 'compilerType', 'semver', 'extensions', 'monaco']`
If you require different fields, you can specify them by adding `?fields=field1,field2,field3`
to your query.
To see all the available fields, you can use `?fields=all`. It is not recommended
to use this by default.
### `GET /api/compilers/<language-id>` - return a list of compilers with matching language
Returns a list of compilers for the provided language id. In text form,
@@ -31,6 +41,8 @@ Returns a list of compilers for the provided language id. In text form,
language ID. In JSON, all the information is returned as an array of compilers,
with the `id` key being the primary identifier of each compiler.
The same field restrictions apply as with `GET /api/compilers`
### `GET /api/libraries/<language-id>` - return a list of libraries available with for a language
Returns a list of libraries and library versions available for the provided language id.

View File

@@ -63,7 +63,7 @@ class CompilerFinder {
const requestLib = port === 443 ? https : http;
const uriSchema = port === 443 ? 'https' : 'http';
const uri = urljoin(`${uriSchema}://${host}:${port}`, uriBase);
const apiPath = urljoin('/', uriBase || '', 'api/compilers', langId || '');
const apiPath = urljoin('/', uriBase || '', 'api/compilers', langId || '', '?fields=all');
logger.info(`Fetching compilers from remote source ${uri}`);
return this.retryPromise(
() => {

View File

@@ -111,11 +111,38 @@ class ApiHandler {
this.outputList(availableLanguages, 'Id', req, res);
}
filterCompilerProperties(list, selectedFields) {
return _.map(list, (compiler) => {
const filteredProps = {};
const allKeys = Object.keys(compiler);
_.each(selectedFields, (field) => {
if (allKeys.includes(field)) {
filteredProps[field] = compiler[field];
}
});
return filteredProps;
});
}
outputList(list, title, req, res) {
if (req.accepts(['text', 'json']) === 'json') {
res.send(list);
if (req.query.fields === 'all') {
res.send(list);
} else {
const defaultfields = ['id', 'name', 'lang', 'compilerType', 'semver', 'extensions', 'monaco'];
if (req.query.fields) {
const filteredList = this.filterCompilerProperties(list, req.query.fields.split(','));
res.send(filteredList);
} else {
const filteredList = this.filterCompilerProperties(list, defaultfields);
res.send(filteredList);
}
}
return;
}
const maxLength = _.max(_.pluck(_.pluck(list, 'id').concat([title]), 'length'));
res.set('Content-Type', 'text/plain');
res.send(utils.padRight(title, maxLength) + ' | Name\n'

View File

@@ -64,6 +64,21 @@ const compilers = [
},
];
const compilersLimitedFields = [
{
id: 'gcc900',
name: 'GCC 9.0.0',
},
{
id: 'fpc302',
name: 'FPC 3.0.2',
},
{
id: 'clangtrunk',
name: 'Clang trunk',
},
];
chai.use(require('chai-http'));
chai.should();
@@ -128,6 +143,32 @@ describe('API handling', () => {
throw err;
});
});
it('should respond to JSON compiler requests with all fields', () => {
return chai.request(app)
.get('/api/compilers?fields=all')
.set('Accept', 'application/json')
.then(res => {
res.should.have.status(200);
res.should.be.json;
res.body.should.deep.equals(compilers);
})
.catch(err => {
throw err;
});
});
it('should respond to JSON compiler requests with limited fields', () => {
return chai.request(app)
.get('/api/compilers?fields=id,name')
.set('Accept', 'application/json')
.then(res => {
res.should.have.status(200);
res.should.be.json;
res.body.should.deep.equals(compilersLimitedFields);
})
.catch(err => {
throw err;
});
});
it('should respond to ASM doc requests', () => {
return chai.request(app)
.get('/api/asm/MOVQ')