Skip to content

Latest commit

 

History

History
119 lines (91 loc) · 3.09 KB

File metadata and controls

119 lines (91 loc) · 3.09 KB

Calling an external API from an Async action

This exercise challenges you to create a new action that takes a price and currency and returns a message that contains the equivalent amount of Bitcoin. The actual conversion will be done by calling an external API asynchronously from your function.

Input

The action should take two parameters, amount and currency. The amount parameter is a number and currency is a three letter currency code.

Output

Return the following JSON template with the input parameters and bitcoin amounts.

{
  "amount": 1.5,
  "message": "10000 USD is worth 1.5 bitcoins."
}

If either the amount or currency parameters are missing, return an error with details.

Resources

This Coindesk Version 1 API returns real-time bitcoin prices. This includes rates for the USD, GBP and EUR currencies.

This Currency Converter API returns exchanges rates between traditional currencies.

Use the request-promise module to make external API requests. It comes pre-installed in the runtime.

Tests

Test with currencies in the Coindesk API response

ibmcloud fn action invoke bitcoin -r -p amount 1000 -p currency USD
{
    "amount": "0.160814",
    "label": "1000 USD is worth 0.160814 bitcoins."
}
ibmcloud fn action invoke bitcoin -r -p amount 1000 -p currency EUR
{
    "amount": "0.187235",
    "label": "1000 EUR is worth 0.187235 bitcoins."
}
ibmcloud fn action invoke bitcoin -r -p amount 1000 -p currency GBP
{
    "amount": "0.213012",
    "label": "1000 GBP is worth 0.213012 bitcoins."
}

Test with currencies not in the Coindesk API response.

ibmcloud fn action invoke bitcoin -r -p amount 1000 -p currency AUD
{
    "amount": "0.10814",
    "label": "1000 AUD is worth 0.10814 bitcoins."
}

Test with missing parameters

ibmcloud fn action invoke bitcoin -r -p amount 1000
{
    "error": "Missing mandatory argument: currency"
}
ibmcloud fn action invoke bitcoin -r  -p currency GBP
{
    "error": "Missing mandatory argument: amount"
}