enonic / xp

Enonic XP
https://enonic.com
GNU General Public License v3.0
202 stars 34 forks source link

Document web api's #6672

Closed ComLock closed 5 years ago

ComLock commented 6 years ago

There are many web api's used in the admin apps, that are not documented.

For example its possible to list app statuses, and start and stop apps. If they where documented people would know that they exist and be able to use them for cool stuff.

In addition it would also be useful if those same web api's where available as js api's.

ComLock commented 6 years ago
import {request as httpClientRequest} from '/lib/http-client';
import {toStr} from '/lib/enonic/util';
import {getBaseUri} from '/lib/xp/admin';

export function isAppRunning(applicationKey) {
    const url = `http://localhost:8080${getBaseUri()}/rest/application`; // TODO Remove hardcode
    const reqParams = {
        url,
        params: {
            applicationKey
        },
        auth: {
            user: 'su',
            password: 'password' // TODO Remove hardcode
        }
    }; //log.info(toStr({reqParams}));

    let res;
    try {
        res = httpClientRequest(reqParams); log.info(toStr({res}));
        if (res.status !== 200) {
            const errMsg = `Requesting url:${url} got status:${res.status}`;
            log.error(errMsg);
            throw new Error(errMsg);
        }
    } catch (e) {
        const errMsg = `Something went wrong trying request url:${url}`;
        log.error(errMsg);
        throw new Error(errMsg);
    }

    let body;
    try {
        body = JSON.parse(res.body); log.info(toStr({body}));
    } catch (e) {
        const errMsg = `Something went wrong trying to json parse response.body from url:${url}`;
        log.error(errMsg);
        throw new Error(errMsg);
    }
    return body.state === 'started';
}
alansemenov commented 6 years ago

Our Web API is internal and endpoints require authentication. If we open them outside, we have to implement authentication via token/API key. I suggest we focus on improving/expanding our JS API for now (like https://github.com/enonic/xp/issues/6671 for example).