2020-05-17 11:37:18 +02:00
|
|
|
const ExamplesHandler = function () {
|
|
|
|
const config = {
|
|
|
|
username: process.env.ZULIP_USERNAME,
|
|
|
|
apiKey: process.env.ZULIP_API_KEY,
|
|
|
|
realm: process.env.ZULIP_REALM,
|
|
|
|
};
|
2020-05-19 18:39:17 +02:00
|
|
|
const examples = {};
|
|
|
|
const response_data = [];
|
2020-05-17 11:37:18 +02:00
|
|
|
|
|
|
|
const make_result_object = (example, result, count = false) => {
|
|
|
|
const name = count !== false ? `${example.name}_${count}` : example.name;
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
endpoint: example.endpoint.split(':')[0],
|
|
|
|
method: example.endpoint.split(':')[1],
|
|
|
|
status_code: example.status_code.toString(),
|
|
|
|
result,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-19 18:39:17 +02:00
|
|
|
const generate_validation_data = async (client, example) => {
|
|
|
|
const result = await example.func(client);
|
|
|
|
if (Array.isArray(result)) {
|
|
|
|
// Handle special cases where some examples make
|
|
|
|
// more than 1 API requests.
|
|
|
|
result.forEach((r, index) => {
|
|
|
|
response_data.push(make_result_object(example, r, index));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
response_data.push(make_result_object(example, result));
|
2020-05-17 11:37:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const main = async () => {
|
2020-05-19 18:39:17 +02:00
|
|
|
const Zulip = require('zulip-js');
|
|
|
|
const client = await Zulip(config);
|
|
|
|
|
|
|
|
await generate_validation_data(client, examples.send_message);
|
|
|
|
await generate_validation_data(client, examples.create_user);
|
|
|
|
|
|
|
|
console.log(JSON.stringify(response_data));
|
|
|
|
return;
|
2020-05-17 11:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const add_example = (name, endpoint, status_code, func) => {
|
2020-05-19 18:39:17 +02:00
|
|
|
const example = {
|
2020-05-17 11:37:18 +02:00
|
|
|
name,
|
|
|
|
endpoint,
|
|
|
|
status_code,
|
|
|
|
func,
|
2020-05-19 18:39:17 +02:00
|
|
|
};
|
|
|
|
examples[name] = example;
|
2020-05-17 11:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
main,
|
|
|
|
add_example,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-17 12:26:12 +02:00
|
|
|
const {main, add_example} = ExamplesHandler();
|
2020-05-17 11:37:18 +02:00
|
|
|
|
|
|
|
// Declare all the examples below.
|
|
|
|
|
2020-05-17 12:26:12 +02:00
|
|
|
add_example('create_user', '/users:post', 200, async (client) => {
|
|
|
|
// {code_example|start}
|
|
|
|
const params = {
|
|
|
|
email: 'newbie@zulip.com',
|
|
|
|
password: 'temp',
|
|
|
|
full_name: 'New User',
|
|
|
|
short_name: 'newbie',
|
|
|
|
};
|
|
|
|
|
|
|
|
return await client.users.create(params);
|
|
|
|
// {code_example|end}
|
|
|
|
});
|
|
|
|
|
2020-05-17 11:37:18 +02:00
|
|
|
main();
|