mirror of
https://github.com/HeyPuter/puter.git
synced 2026-05-29 21:01:27 +00:00
dev: add cost calculation for NewsDataService
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user