forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request open-runtimes#61 from camperjett/get-price-function
getPrice() Function using Deno
- Loading branch information
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Get Price! | ||
|
||
A Deno Appwrite Function to get the price of crypto, stocks, and commodities. Supported types include Bitcoin, Ethereum, Google, Amazon, gold, and silver. | ||
|
||
_Example input:_ | ||
|
||
```json | ||
{ | ||
"type": "gold" | ||
} | ||
``` | ||
|
||
> Other allowed types are: `google`, `amazon`, `ethereum`, `bitcoin`, `gold`, `silver`. | ||
_Example output:_ | ||
|
||
|
||
```json | ||
{ | ||
"success":true, | ||
"price":1666.81 | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"success":false, | ||
"message":"GOLDAPIkey key is required" | ||
} | ||
``` | ||
|
||
## 📝 Environment Variables | ||
|
||
List of environment variables used by this cloud function: | ||
|
||
- **COIN_API_KEY** - Your [Coin API key](https://docs.coinapi.io/#md-docs) | ||
- **ALPHAVANTAGE_API_KEY** - Your [Alphavantage API key](https://www.alphavantage.co/) | ||
- **GOLD_API_KEY** - Your [Gold API key](https://www.goldapi.io/) | ||
|
||
## 🚀 Deployment | ||
|
||
1. Clone this repository, and enter this function folder: | ||
|
||
``` | ||
$ git clone https://github.com/open-runtimes/examples.git && cd examples | ||
$ cd deno/get_price | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
``` | ||
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=src/mod.ts --rm --interactive --tty --volume $PWD:/usr/code openruntimes/deno:v2-1.21 sh /usr/local/src/build.sh | ||
``` | ||
As a result, a `code.tar.gz` file will be generated. | ||
|
||
3. Start the Open Runtime: | ||
``` | ||
docker run -p 3000:3000 -e INTERNAL_RUNTIME_ENTRYPOINT=src/mod.ts -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/deno:v2-1.21 sh /usr/local/src/start.sh | ||
``` | ||
|
||
4. Execute function: | ||
|
||
``` | ||
curl http:https://localhost:3000/ -d '{"variables":{"GOLD_API_KEY":"[YOUR_API_KEY]"},"payload": "{\"type\":\"gold\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" | ||
``` | ||
|
||
Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Deno runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/deno-1.21). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
export default async function (req: any, res: any) { | ||
const CoinAPIkey = req.variables["COIN_API_KEY"]; | ||
const AlphavantageAPIkey = req.variables["ALPHAVANTAGE_API_KEY"]; | ||
const GOLDAPIkey = req.variables["GOLD_API_KEY"]; | ||
|
||
const payload = JSON.parse(req.payload); | ||
|
||
if (!payload.type) { | ||
res.json({ success: false, message: 'type is required' }); | ||
return; | ||
} | ||
|
||
const type = payload.type.toLowerCase(); | ||
|
||
let price; | ||
if (type === "google" || type === "amazon") { | ||
if (!AlphavantageAPIkey || AlphavantageAPIkey === "") { | ||
res.json({ success: false, message: 'AlphavantageAPI key is required' }); | ||
return; | ||
} | ||
|
||
let code = type === "google" ? "GOOGL" : "AMZN"; | ||
const str = "Time Series (5min)"; | ||
let response: { [str: string]: any } = await fetch( | ||
`https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${code}&interval=5min&apikey=${AlphavantageAPIkey}`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
"User-Agent": "request", | ||
}, | ||
}, | ||
); | ||
|
||
if (response.status >= 400) { | ||
res.json({ success: false, message: `${await response.text()}` }); | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
let price = data[str][Object.keys(data[str])[0]]["1. open"]; | ||
|
||
res.send({ "success": true, "price": price }); | ||
} else if ( | ||
type === "bitcoin" || type === "ethereum" | ||
) { | ||
if (!CoinAPIkey || CoinAPIkey === "") { | ||
res.json({ success: false, message: 'CoinAPI key is required' }); | ||
return; | ||
} | ||
|
||
let code = type === "bitcoin" ? "BTC" : "ETH"; | ||
let response: { [rate: string]: any } = await fetch( | ||
`https://rest.coinapi.io/v1/exchangerate/${code}/USD`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
"X-CoinAPI-Key": CoinAPIkey, | ||
}, | ||
}, | ||
); | ||
|
||
if (response.status >= 400) { | ||
res.json({ success: false, message: `${await response.text()}` }); | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
price = data["rate"]; | ||
res.send({ "success": true, "price": price }); | ||
} else if (type === "gold" || type === "silver") { | ||
if (!GOLDAPIkey || GOLDAPIkey === "") { | ||
res.json({ success: false, message: 'GOLDAPIkey key is required' }); | ||
return; | ||
} | ||
|
||
let code = type === "gold" ? "XAU" : "XAG"; | ||
let response: { [index: string]: any } = await fetch( | ||
`https://www.goldapi.io/api/${code}/USD/`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
"x-access-token": GOLDAPIkey, | ||
}, | ||
}, | ||
); | ||
|
||
if (response.status >= 400) { | ||
res.json({ success: false, message: `${await response.text()}` }); | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
let price = data["price"]; | ||
|
||
res.send({ "success": true, "price": price }); | ||
} else { | ||
res.send({ "success": false, "message": "Type is not supported." }); | ||
} | ||
} |