Using Custom Parameters with Cloud Code for Data

You can pass custom parameters from the client to the backend and then build logic around them, changing the behavior of your service. Assume you have an Activities content type that stores check-in information about your users. You can use custom parameters to find out how far each check-in is compared to a given location.

Start by passing your current location as a custom parameter. You can use either the X-Everlive-Custom-Parameters header or the customParams query string parameter to do that. After you define a custom parameter, it becomes a part of the request.customParameters object, accessible inside the content type's Cloud Code.

Request:
    GET https://api.everlive.com/v1/your-app-id/Activities
Header:
    Authorization  Bearer your-access-token-here
    X-Everlive-Custom-Parameters {
        "currentLocation" : {
            "latitude" : 42.650601,
            "longitude" : 23.379068
        }
    }
Response:
    Status: 200 OK
    Content-Type: application/json
//Ajax request using jQuery
var customParam = JSON.stringify({
    "currentLocation" :{
        "latitude" : 42.650601,
        "longitude" : 23.379068
    }
});
$.ajax({
    url: "https://api.everlive.com/v1/your-app-id/Activities",
    type: "GET",
    headers: {
        "Authorization" : "Bearer your-access-token-here",
        "X-Everlive-Custom-Parameters" : customParam
    },
    success: function(data){
        alert(JSON.stringify(data));
    },
    error: function(error){
        alert(JSON.stringify(error));
    }
})

With the current location in hand, iterate over all returned items (response.result) and calculate how far the location is from each check-in location. Then append the calculated information to the standard result as a couple of dynamic properties: DistanceInKilometers and DistanceInMiles.

Everlive.Events.afterRead(function(request, response, context, done) {
    if(!response.error) {
        if(request.customParameters.currentLocation !== null) {
            for(var i = 0; i < response.result.length; i++) {
                var p1 = request.customParameters.currentLocation;
                var p2 = response.result[i].Location;

                if(p2) {
                    var distance = distanceInKilometers(p1,p2);
                    response.result[i].DistanceInKilometers = distance;
                    response.result[i].DistanceInMiles = distance * 0.621371;
                }
            }
        }
    }
    done();
});

function toRadians(degrees)
{
    return degrees * Math.PI / 180;
}

function distanceInKilometers(point1, point2)
{
    var R = 6371;
    var dLat = toRadians(point1.latitude - point2.latitude);
    var dLon = toRadians(point1.longitude - point2.longitude);
    var lat1 = toRadians(point1.latitude);
    var lat2 = toRadians(point2.latitude);

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = R * c;
    return d;
}

function distanceInMiles(point1,point2)
{
    return distanceInKilometers(point1,point2) * 0.621371;
}

Every time you execute a GET request that contains a custom parameter encompassing a currentLocation object, you receive the full list of activities, with additional data in DistanceInKilometers and DistanceInMiles.

{
"Count": 2,
"Result": [
        {
            "CreatedAt": "2012-12-01T14:09:07.000Z",
            "CreatedBy": "00000000-0000-0000-0000-000000000000",
            "Location": {
                "longitude": -0.1318359375,
                "latitude": 51.39920565355378
            },
            "ModifiedAt": "2012-12-01T14:37:36.000Z",
            "ModifiedBy": "00000000-0000-0000-0000-000000000000",
            "Picture": "e36119e7-1770-49ce-af01-ad26501f00e5",
            "Text": "The most amazing sunset I have ever seen at Phuket, Thailand",
            "UserId": "136b5368-2ef4-40fa-a62d-5f409fbc173c",
            "Id": "d36f287a-9572-4b54-9b40-273995d9c1b3",
            "DistanceInKilometers": 2017.626995257996,
            "DistanceInMiles": 1253.6949036704561
        },
        {
            "CreatedAt": "2012-12-01T14:09:07.000Z",
            "CreatedBy": "00000000-0000-0000-0000-000000000000",
            "Location": {
                "longitude": 22.9888916015625,
                "latitude": 42.736926481692684
            },
            "ModifiedAt": "2012-12-01T14:10:53.000Z",
            "ModifiedBy": "00000000-0000-0000-0000-000000000000",
            "Picture": "f29264ce-1e9f-4843-8174-6eaaa33f9dee",
            "Text": "It is finally time for graduation! Good job everyone, we made it.",
            "UserId": "cd94813a-893d-4463-8281-7b4439c62a2d",
            "Id": "f14ddc47-a12e-46a3-a70b-86ae5fe5201e",
            "DistanceInKilometers": 33.30132332007302,
            "DistanceInMiles": 20.69247657271709
        }
    ]
}

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.