-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress.ts
More file actions
118 lines (104 loc) · 2.89 KB
/
express.ts
File metadata and controls
118 lines (104 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Express.js Integration Example
*
* Shows how to use the SDK in an Express API server
*
* Install dependencies:
* npm install express
* npm install -D @types/express
*/
import express from 'express';
import { OilPriceAPI } from 'oilpriceapi';
const app = express();
const port = 3000;
// Initialize SDK client (reuse across requests)
const oilPriceClient = new OilPriceAPI({
apiKey: process.env.OIL_PRICE_API_KEY || 'your_api_key_here',
retries: 3,
timeout: 10000
});
// Endpoint: Get latest prices
app.get('/api/prices/latest', async (req, res) => {
try {
const commodity = req.query.commodity as string | undefined;
const prices = await oilPriceClient.getLatestPrices({ commodity });
res.json({
success: true,
data: prices,
count: prices.length
});
} catch (error: any) {
res.status(error.statusCode || 500).json({
success: false,
error: error.message
});
}
});
// Endpoint: Get historical prices
app.get('/api/prices/historical', async (req, res) => {
try {
const commodity = req.query.commodity as string | undefined;
const period = req.query.period as 'past_week' | 'past_month' | 'past_year' | undefined;
const startDate = req.query.startDate as string | undefined;
const endDate = req.query.endDate as string | undefined;
const prices = await oilPriceClient.getHistoricalPrices({
commodity,
period,
startDate,
endDate
});
res.json({
success: true,
data: prices,
count: prices.length
});
} catch (error: any) {
res.status(error.statusCode || 500).json({
success: false,
error: error.message
});
}
});
// Endpoint: Get all commodities
app.get('/api/commodities', async (req, res) => {
try {
const response = await oilPriceClient.getCommodities();
res.json({
success: true,
data: response.commodities,
count: response.commodities.length
});
} catch (error: any) {
res.status(error.statusCode || 500).json({
success: false,
error: error.message
});
}
});
// Endpoint: Get commodity details
app.get('/api/commodities/:code', async (req, res) => {
try {
const commodity = await oilPriceClient.getCommodity(req.params.code);
res.json({
success: true,
data: commodity
});
} catch (error: any) {
res.status(error.statusCode || 500).json({
success: false,
error: error.message
});
}
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
app.listen(port, () => {
console.log(`Oil Price API server running on http://localhost:${port}`);
console.log('\nAvailable endpoints:');
console.log(' GET /api/prices/latest?commodity=WTI_USD');
console.log(' GET /api/prices/historical?commodity=WTI_USD&period=past_week');
console.log(' GET /api/commodities');
console.log(' GET /api/commodities/WTI_USD');
});