Skip to content

Commit

Permalink
Merge pull request #265 from RobokopU24/feature/cache-sample-queries
Browse files Browse the repository at this point in the history
Feature/cache sample queries
  • Loading branch information
Woozl authored Jul 25, 2023
2 parents 62890ba + 042ae44 commit ed3b3ea
Show file tree
Hide file tree
Showing 2 changed files with 7,696 additions and 6 deletions.
25 changes: 19 additions & 6 deletions api_routes/routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('fs');
const path = require('path');

const router = require('express').Router();
const axios = require('axios');

Expand All @@ -6,12 +9,23 @@ const { handleAxiosError } = require('./utils');
const services = require('./services');
const external_apis = require('./external');

const samples = JSON.parse(fs.readFileSync(path.join(__dirname, './sample-query-cache.json')));

router.use('/', external_apis);

router.use('/robokache', robokache.router);

router.route('/quick_answer')
.post(async (req, res) => {
// if this is a sample query, load the response from the cache JSON:
for (let i = 0; i < samples.length; i += 1) {
const { query, response } = samples[i];
if (JSON.stringify(req.body) === JSON.stringify(query)) {
res.send(response);
return;
}
}

const { ara } = req.query;
const ara_url = services[ara];
const config = {
Expand All @@ -37,12 +51,11 @@ router.route('/quick_answer')
});
}
} catch (err) {
//res.send(handleAxiosError(err));
res.send({
status: 'error',
message: `Error from ${ara_url}`,
});
}
res.send({
status: 'error',
message: `Error from ${ara_url}`,
});
}
});

router.route('/answer')
Expand Down
7,677 changes: 7,677 additions & 0 deletions api_routes/sample-query-cache.json

Large diffs are not rendered by default.

0 comments on commit ed3b3ea

Please sign in to comment.