Getting Started with Cloud Functions

Getting Started with Cloud Functions

Using a Currency Exchange Rate Calculator example, this article shows you how to define a simple Cloud Function that accepts parameters and returns a result that varies depending on their values. The sample function utilizes the Cloud Code API to communicate with a third-party web service.

The function accepts three parameters: source currency, amount, and target currency. It uses an external web service to get the exchange rate. Then it calculates the amount in the target currency and returns it in plain text.

Note that this is a very simplistic scenario that is only used to get you started with Cloud Functions. Because Cloud Functions actually execute as HTTP requests, you can use them to integrate with much more complex services.

  1. Define the Cloud Function
  2. Set Permissions for the Cloud Function
  3. Execute the Cloud Function
  4. Summary

Prerequisites

  • A Telerik Platform account.
  • The App ID for your Telerik Platform app. You can access it from Settings.
  • The Business Logic service enabled.

Define the Cloud Function

Start by creating your first Cloud Function:

  1. Open you app.
  2. Navigate to Business Logic > Explorer to view the Business Logic Explorer.

    In the tree on the right you will see a list of all available Cloud Functions, listed under Cloud Functions.

  3. Click the + button next to Cloud Functions to start a new function.

  4. Enter ConvertCurrency in the name field and then click OK to create the new function.

    This will automatically open the code editor where you can enter code.

  5. Paste the following code in the editor:

    Everlive.CloudFunction.onRequest(function(request, response, done) {
        //Get parameters from the request
        var fromCurrency = request.queryString.from;
        var toCurrency = request.queryString.to;
        var amount = request.queryString.amount;
    
        //Get the exchange rate from an external service
            var url = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("' + fromCurrency + toCurrency + '")&env=store://datatables.org/alltableswithkeys&format=json';
    
        Everlive.Http.get(url, {}, function(error, httpResponse) {
            if (error) {
                //Write the error in the response
                response.body = "Error while executing third-party service: " + error.message;
            } else {
                //Calculate and write the response of the Cloud Function
                var rate = httpResponse.data.query.results.rate.Rate;
                var amountInNewCurrency = amount * rate;
                response.body = amount + " " + fromCurrency + " are equal to " + amountInNewCurrency + " " + toCurrency + ".";
            }
    
            //Call done to end the execution of the Cloud Function
            done();
            });
    });
    
  6. Click the Save button to store the new code.

The code has four logical steps:

  1. Read the parameters from the query string.
  2. Call a service to get the exchange rate.
  3. Calculate the result.
  4. Write the result to the HTTP response.

Set Permissions for the Cloud Function

Depending on your app, Cloud Functions may be responsible for highly important or even critical or sensitive tasks. They may be handling private data or doing operations that should only be accessible to a small subset of users. This is why you need permissions to secure your Cloud Functions.

In this particular example you are building a tool that needs to be run by everyone. To do that, you will allow everyone to execute the Cloud Function. Take these steps:

  1. Go to Business Logic > Permissions in the left-hand navigation pane.
  2. Find your Cloud Function and make sure that the box next to Anonymous is checked.
  3. Click Save.

Execute the Cloud Function

Now that your function is defined, you can execute it and see how it works.

  1. Go back to Business Logic > Explorer.
  2. Click the Cloud Function in the tree on the left to open it.
  3. Click the Execute at the top of the code editor and fill in the following fields:

  • Url—This is filled in for you; you can select and copy the URL to execute it in another tool if you need to.
  • Request—Keep the default GET request.
  • Query parameters—Set the following parameters (click Add another to add a new parameter):
    • amount to value 50
    • from to value USD
    • to to value EUR

Services page

After you click the Execute button at the bottom you see the result of the request in the Response text box.

You can also execute Cloud Functions using the Backend Services RESTful API. See Executing Cloud Functions for more information.

Summary

Cloud Functions provide you with a way to implement your server-side logic without the burden of supporting complex cloud infrastructure. Add the integrated security, the APIs, and the debugging capabilities, and Cloud Functions become a powerful framework for complementing your mobile applications with server-side logic.

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.