Using Custom Parameters with Cloud Code for Data

Using Custom Parameters with Cloud Code for Data

You can pass custom parameters from the client and then define logic around them, changing the behavior of your service. For example if you have an "Activities" content type that stores the check-in information for your users, and you want to see how far each check-in is compared to a specific location, you can use custom parameters to achieve that.

First you have to execute the following request that can contain either a header parameter X-Everlive-Custom-Parameters or a query string customParams with your current location as follows:

//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));
    }
})
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

Once you define your custom parameters they will become part of your request.customParameters object. You can then iterate over all returned items by using the response.result object and set two 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;
}

Once you execute a get request that contains a custom parameter with currentLocation object it in, you will receive all activities containing DistanceInKilometers and DistanceInMiles similar to the following result:

{
"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
        }
    ]
}
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.