Skip to content

Commit

Permalink
PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Jan 22, 2023
1 parent c09c873 commit ee195a5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
21 changes: 18 additions & 3 deletions deno/get_price/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ _Example input:_
}
```

> Other allowed types are: `google`, `amazon`, `ethereum`, `bitcoin`, `gold`, `silver`.
_Example output:_


Expand All @@ -20,6 +22,13 @@ _Example output:_
}
```

```json
{
"success":false,
"message":"GOLDAPIkey key is required"
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:
Expand All @@ -39,13 +48,19 @@ $ 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:1.14 sh /usr/local/src/build.sh
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:1.14 sh /usr/local/src/start.sh
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.14).
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).
71 changes: 47 additions & 24 deletions deno/get_price/src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
export default async function (req: any, res: any) {
const CoinAPIkey = req.env["COIN_API_KEY"];
const AlphavantageAPIkey = req.env["ALPHAVANTAGE_API_KEY"];
const GOLDAPIkey = req.env["GOLD_API_KEY"];
const CoinAPIkey = req.variables["COIN_API_KEY"];
const AlphavantageAPIkey = req.variables["ALPHAVANTAGE_API_KEY"];
const GOLDAPIkey = req.variables["GOLD_API_KEY"];

const { type } = req.payload;
const payload = JSON.parse(req.payload);

if (!type) {
throw new Error("type is required");
if (!payload.type) {
res.json({ success: false, message: 'type is required' });
return;
}

if (!CoinAPIkey || CoinAPIkey === "") {
throw new Error("CoinAPI key is required");
}

if (!AlphavantageAPIkey || AlphavantageAPIkey === "") {
throw new Error("AlphavantageAPI key is required");
}

if (!GOLDAPIkey || GOLDAPIkey === "") {
throw new Error("GOLDAPIkey key is required");
}
const type = payload.type.toLowerCase();

let price;
if (type.toLowerCase() === "google" || type.toLowerCase() === "amazon") {
let code = type.toLowerCase() === "google" ? "GOOGL" : "AMZN";
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}`,
Expand All @@ -34,14 +30,25 @@ export default async function (req: any, res: any) {
},
},
);

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.toLowerCase() === "bitcoin" || type.toLowerCase() === "ethereum"
type === "bitcoin" || type === "ethereum"
) {
let code = type.toLowerCase() === "bitcoin" ? "BTC" : "ETH";
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`,
{
Expand All @@ -51,12 +58,22 @@ export default async function (req: any, res: any) {
},
},
);

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.toLowerCase() === "gold" || type.toLowerCase() === "silver") {

let code = type.toLowerCase() === "gold" ? "XAU" : "XAG";
} 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/`,
{
Expand All @@ -66,6 +83,12 @@ export default async function (req: any, res: any) {
},
},
);

if (response.status >= 400) {
res.json({ success: false, message: `${await response.text()}` });
return;
}

const data = await response.json();

let price = data["price"];
Expand Down

0 comments on commit ee195a5

Please sign in to comment.