Skip to content

Commit c032ddb

Browse files
added market metrics tokens endpoint action
1 parent 674f23f commit c032ddb

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import tokenMetrics from "../../token_metrics.app.mjs";
2+
import { ENDPOINTS, FILTER_DEFINITIONS } from "../../common/constants.mjs";
3+
import { buildParams, generateFilterSummary } from "../../common/utils.mjs";
4+
5+
const endpoint = ENDPOINTS.MARKET_METRICS;
6+
7+
export default {
8+
key: "token_metrics-get-market-metrics",
9+
name: "Get Market Metrics",
10+
description: `${endpoint.description}. [See the documentation](https://developers.tokenmetrics.com/reference/market-metrics)`,
11+
version: "0.0.1",
12+
type: "action",
13+
props: {
14+
tokenMetrics,
15+
// Filter props based on endpoint configuration and API documentation
16+
startDate: {
17+
...FILTER_DEFINITIONS.start_date,
18+
description: "Start Date accepts date as a string - YYYY-MM-DD format. Example: 2023-10-01",
19+
},
20+
endDate: {
21+
...FILTER_DEFINITIONS.end_date,
22+
description: "End Date accepts date as a string - YYYY-MM-DD format. Example: 2023-10-10",
23+
},
24+
// Pagination props
25+
limit: {
26+
propDefinition: [
27+
tokenMetrics,
28+
"limit",
29+
],
30+
description: "Limit the number of items in response. Defaults to 50",
31+
default: 50,
32+
},
33+
page: {
34+
propDefinition: [
35+
tokenMetrics,
36+
"page",
37+
],
38+
description: "Enables pagination and data retrieval control by skipping a specified number of items before fetching data. Page should be a non-negative integer, with 1 indicating the beginning of the dataset. Defaults to 1",
39+
default: 1,
40+
},
41+
},
42+
async run({ $ }) {
43+
// Build parameters using utility function
44+
const params = buildParams(this, endpoint.filters);
45+
46+
try {
47+
const response = await this.tokenMetrics.getMarketMetrics({
48+
$,
49+
params,
50+
});
51+
52+
// Generate summary using utility function
53+
const filterSummary = generateFilterSummary(this, endpoint.filters);
54+
55+
// Use $ context for export
56+
if ($ && $.export) {
57+
const dataLength = response.data?.length || 0;
58+
$.export("$summary", `Successfully retrieved market metrics for ${dataLength} records${filterSummary}`);
59+
}
60+
61+
return response;
62+
} catch (error) {
63+
// Enhanced error handling
64+
const errorMessage = error.response?.data?.message || error.message || "An error occurred";
65+
const statusCode = error.response?.status;
66+
67+
if ($ && $.export) {
68+
$.export("$summary", `Error: ${errorMessage}`);
69+
}
70+
71+
// Throw a more descriptive error
72+
throw new Error(`Token Metrics API Error (${statusCode || 'Unknown'}): ${errorMessage}`);
73+
}
74+
},
75+
};

components/token_metrics/common/constants.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ export const ENDPOINTS = {
124124
"end_date",
125125
],
126126
},
127+
MARKET_METRICS: {
128+
path: "/market-metrics",
129+
description: "Get the Market Analytics from Token Metrics. They provide insight into the full Crypto Market, including the Bullish/Bearish Market indicator",
130+
filters: [
131+
"start_date",
132+
"end_date",
133+
],
134+
},
127135
};
128136

129137
// Common filter definitions that can be reused across endpoints

components/token_metrics/token_metrics.app.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,15 @@ export default {
181181
params,
182182
});
183183
},
184+
async getMarketMetrics({
185+
$ = this,
186+
params = {},
187+
}) {
188+
return this.makeApiCall({
189+
$,
190+
endpoint: "/market-metrics",
191+
params,
192+
});
193+
},
184194
},
185195
};

0 commit comments

Comments
 (0)