dev: add cost calculation for NewsDataService

This commit is contained in:
KernelDeimos
2025-04-25 14:14:58 -04:00
parent 78ae6f6bf2
commit c5c17bae3a
@@ -27,6 +27,8 @@ class NewsDataService extends BaseService {
static IMPLEMENTS = {
newsdata: {
async newsdata (parameters) {
const cost_per_article =
this.config.cost_per_article ?? 13000;
// doing this makes vscode recognize what's being required
const require = this.require;
@@ -47,6 +49,20 @@ class NewsDataService extends BaseService {
url: 'https://newsdata.io/api/1/latest?' + qstr,
});
const amount_articles = resp.data.results.length;
{
const cost = amount_articles * cost_per_article;
const svc_cost = this.services.get('cost');
const usageAllowed = await svc_cost.get_funding_allowed({
minimum: cost,
});
if ( ! usageAllowed ) {
throw APIError.create('insufficient_funds');
}
await svc_cost.record_cost({ cost });
}
return resp.data;
}
}