POST Update Fence – Client examples

/**
* @author Bluedot Innovation
* Copyright (c) 2016 Bluedot Innovation. All rights reserved.
* Update fence client demonstrates updating existing geofences within a zone using 'request' node module.
* Circular fence
* Bounding Box
* Polygonal
* Geoline
*/
var request = require('request');
var fenceData =
{
    "security": {
        /* The apiKey is generated when you create an application. */
        "apiKey" : "c2c8211f-796f-4eda-b6ce-05467b5263a9",
        /* This key is generated by Bluedot Point Access UI when your account is created. It is also available
         * on the PointAccess interface in the Edit Profile section. */                
        "customerApiKey": "86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            /* The zoneId is the id of the zone being updated. This can be fetched by calling GET zones API */
            "zoneId": 'b80c81af-7bb8-4c85-9340-b816da5a0ca5',
            "fences": {
                "rectangles": [
                    {
                        /* This is the id of the rectangular fence being updated. This can be fetched by calling the GET zones API*/
                        "fenceId": 'b9875caf-5d33-4bbd-bea3-2144e60eb41a',
                        "name": "A bounding box with north east and south west",
                        "color": "#8b4dff",
                        "northEast": {
                            "latitude": -37.81544591805361,
                            "longitude": 144.9786114692688
                        },
                        "southWest": {
                            "latitude": -37.81758175613945,
                            "longitude": 144.9731397628784
                        }
                    }
                ],
                "circles": [
                    {
                        /* This is the id of the circular fence being updated. This can be fetched by calling the GET zones API */
                        "fenceId": '00f3f2af-bcdf-494e-a81c-481aff88f992',
                        "name": "Test Circular fence with 8M radius",
                        "color": "#000ffff",
                        "radius" : 200.8360056433831,
                        "center": {
                            "latitude" : -37.81566628950337,
                            "longitude" : 144.9764871544965
                        }
                    }
                ],
                "polygons": [
                    {
                        /* This is the id of the polygonal fence being updated. This can be fetched by calling the GET zones API*/
                        "fenceId": '36c9521e-3e8b-437a-a27d-9527c46a0b55',
                        "name": "A Polygonal Fence",
                        "color": "#836bff",
                        "vertices": [
                            {
                                "latitude" : -37.818717,
                                "longitude" : 144.983085
                            },
                            {
                                "latitude" : -37.819540,
                                "longitude" : 144.982125
                            },
                            {
                                "latitude" : -37.820298,
                                "longitude" : 144.985178
                            },
                            {
                                "latitude" : -37.820468,
                                "longitude" : 144.984228
                            },
                            {
                                "latitude" : -37.818768,
                                "longitude" : 144.984330
                            },
                            {
                                "latitude" : -37.819476,
                                "longitude" : 144.985033
                            },
                            {
                                "latitude" : -37.820527,
                                "longitude" : 144.982978
                            },
                            {
                                "latitude" : -37.818887,
                                "longitude" : 144.982587
                            }
                        ]
                    }
                ]
            }
        }
    }
};
var options = {
    uri: 'https://api.bluedotinnovation.com/1/fences',
    method: 'POST',
    json: fenceData
};
request(options,
    function (error, response, body) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.fence;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.bluedotinnovation.common.BDCommon;
/**
* @author Bluedot Innovation
* Copyright (c) 2016 Bluedot Innovation. All rights reserved.
* Update fence client demonstrates updating existing geofences within a zone using Apache HTTP client and JSON Simple libraries.
* Circular fence
* Bounding Box
* Polygonal
* Geoline
*/
public class UpdateFence extends BDCommon {
    private static String bdCustomerApiKey      = "ca4c8d11-6942-11e4-ba4b-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created
    private static String bdApplicationApiKey   = "f5223f4c-a0f3-47be-ba59-080b6290a9d4"; //This apiKey is generated when you create an application
    private static String bdZoneId              = "dfafb7fd-d2f7-42ec-afd5-d5c7851c0396"; //This is the id of the zone being updated.  This can be fetched by calling the GET zones API
    private static String bdCircularFenceId     = "21b6874b-032d-4002-b6e0-baf4ac52ab9c"; //This is the id of the circular fence being updated. This can be fetched by calling the GET zones API
    private static String bdBoundingBoxFenceId  = "c24379c7-2268-406b-9262-bc58ba61caac"; //This is the id of the rectangular fence being updated. This can be fetched by calling the GET zones API
    private static String bdPolygonalFenceId    = "176c84a3-80bc-4594-a858-ce9fe3489d22"; //This is the id of the polygonal fence being updated. This can be fetched by calling the GET zones API
    private static String bdGeolineId           = "276c84a3-80bc-4594-a858-ce9fe3489d42"; //This is the id of the Geoline being updated. This can be fetched by calling the GET zones API
    private static String bdRestUrl             = "https://api.bluedotinnovation.com/1/fences";
     
    public static void main(String[] args) throws IOException, ParseException, KeyManagementException, NoSuchAlgorithmException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
   
        JSONParser parser    = new JSONParser();
        JSONObject bdPolygonalFenceJSONObject = (JSONObject) parser.parse(getJsonPolygonalFence()); //update a polygonal fence
                     
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdPolygonalFenceJSONObject.toJSONString(), Charset.defaultCharset()));
      
        HttpResponse response = httpRestClient.execute(postRequest);
                         
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Fence was successfully updated");
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json result
            JSONObject jsonResult   = (JSONObject)  parser.parse(resultString);
            System.out.println(jsonResult);
        } else {
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json error result
            System.out.println(resultString);
        }          
    }
     
    /*Circular fence requires a centerpoint and radius*/
    private static String getJsonCircularFence() {
        String circularFenceJson =
            "{" +
                "\"security\": {" +
                 "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                 "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
             "}," +
             "\"content\": {" +
                 "\"zone\": {" +
                     "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                     "\"fences\": {" +
                         "\"circles\": [" +
                             "{" +
                                 "\"fenceId\":" + "\"" + bdCircularFenceId + "\"," +
                                 "\"name\": \"Circular fence-Updated through public api\"," +
                                 "\"color\": \"#000ffff\"," +
                                 "\"radius\": \"200.330384237225\"," +
                                 "\"center\": {" +
                                            "\"latitude\": -37.818780,"+
                                            "\"longitude\": 144.980734"+
                                 "}" +
                             "}" +
                         "]" +
                     "}" +
                 "}" +
             "}" +
          "}";
        return circularFenceJson;
    }
    /*Bounding box requires north east and south west points*/
    private static String getJsonBoundingBox() {
        String boundingBoxFenceJson =
                "{" +
                   "\"security\": {" +
                        "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                        "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                     "\"fences\": {" +
                        "\"rectangles\": [" +
                            "{" +
                               "\"fenceId\":" + "\"" + bdBoundingBoxFenceId + "\"," +
                                "\"name\": \"Bounding Box-1-Updated\"," +
                                "\"color\": \"#3559e\"," +
                                "\"northEast\": {" +
                                    "\"latitude\": -37.81544591805361," +
                                    "\"longitude\": 144.9786114692688" +
                                "}," +
                                "\"southWest\": {" +
                                    "\"latitude\": -37.81758175613945," +
                                    "\"longitude\": 144.9731397628784" +
                                "}" +
                            "}" +
                        "]" +
                    "}" +
                "}" +
             "}" +
           "}";
        return boundingBoxFenceJson;
    }
     
    /*Polygonal fence requires a series points in lat/long*/
    private static String getJsonPolygonalFence() {
        String polygonalFenceJson =
                   "{" +
                       "\"security\": {" +
                           "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                           "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                            "\"fences\": {" +
                            "\"polygons\": [" +
                                "{" +
                                    "\"fenceId\":" + "\"" + bdPolygonalFenceId + "\"," +
                                    "\"name\": \"Polygon-1-Updated\"," +
                                    "\"color\": \"#000ffff\"," +
                                    "\"vertices\": [" +
                                         "{" +
                                             "\"latitude\": -37.818717," +
                                             "\"longitude\": 144.983085" +
                                         "}," +
                                         "{" +
                                            "\"latitude\": -37.819540," +
                                            "\"longitude\": 144.982125" +
                                         "}," +
                                         "{" +
                                            "\"latitude\": -37.820298," +
                                            "\"longitude\": 144.985178" +
                                         "}," +
                                         "{" +
                                         "\"latitude\": -37.818780,"+
                                         "\"longitude\": 144.980734"+
                                         "}," +
                                         "{" +
                                            "\"latitude\": -37.818768," +
                                            "\"longitude\": 144.984330" +
                                         "}," +
                                         "{" +
                                            "\"latitude\": -37.819476," +
                                            "\"longitude\": 144.985033" +
                                         "}," +
                                         "{" +
                                            "\"latitude\": -37.820527," +
                                            "\"longitude\": 144.982978" +
                                         "}," +
                                         "{" +
                                            "\"latitude\": -37.818887," +
                                            "\"longitude\": 144.982587" +
                                         "}" +
                                    "]" +
                                "}" +
                            "]" +
                        "}" +
                    "}" +
                 "}" +
            "}";
        return polygonalFenceJson;
    }
     
    /*Geoline requires a series points in lat/long*/
    private static String getJsonGeoline() {
        String geolioneJson =
                   "{" +
                       "\"security\": {" +
                           "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                           "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                            "\"fences\": {" +
                            "\"Plylines\": [" +
                                "{" +
                                    "\"fenceId\":" + "\"" + bdGeolineId + "\"," +
                                    "\"name\": \"Geoline-1-Updated\"," +
                                    "\"color\": \"#000ffff\"," +
                                    "\"vertices\": [" +
                                         "{" +
                                             "\"latitude\": -37.818717," +
                                             "\"longitude\": 144.983085" +
                                         "}," +
                                         "{" +
                                            "\"latitude\": -37.818887," +
                                            "\"longitude\": 144.982587" +
                                         "}" +
                                    "]" +
                                "}" +
                            "]" +
                        "}" +
                    "}" +
                 "}" +
            "}";
        return geolioneJson;
    }  
}
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
/**
* @author Bluedot Innovation
* Copyright (c) 2016 Bluedot Innovation. All rights reserved.
* Add fence client demonstrates updating existing geo fences within a zone using net http web api library
* Circular fence
* Bounding Box
* Polygonal
*/
namespace BluedotPublicApiClient.fenceclient
{
    public class BDUpdateFenceClient
    {
        private static String bdCustomerApiKey     = "bc199c80-5441-11e4-b7bb-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created
        private static String bdApplicationApiKey  = "d3161e80-38d1-11e4-b039-bc305bf60831"; //This apiKey is generated when you create an application
        private static String bdZoneId             = "24d9a245-2087-421b-9972-2af2ee0970f1"; //This is the id of the zone being updated. This can be fetched by calling zones/getAll API
        private static String bdCircularFenceId    = "b4024ed6-7a28-4335-ba51-45df58459e17"; //This is the id of the fence being updated. This can be fetched by calling zones/get API
        private static String bdBoundingBoxFenceId = "b4024ed6-7a28-4335-ba51-45df58459e17"; //This is the id of the fence being updated. This can be fetched by calling zones/get API
        private static String bdPolygonalFenceId   = "b4024ed6-7a28-4335-ba51-45df58459e17"; //This is the id of the fence being updated. This can be fetched by calling zones/get API
        private static String bdGeolineFenceId     = "b4024ed6-7a28-4335-ba51-45df58459e17"; //This is the id of the fence being updated. This can be fetched by calling zones/get API
        private static String bdRestUrl = "https://api.bluedotinnovation.com/1/fences";
         
        public void updateFence()
        {
            WebRequestHandler handler = new WebRequestHandler();
            X509Certificate2 certificate = new X509Certificate2();
            handler.ClientCertificates.Add(certificate);
            HttpClient httpRestClient = new HttpClient(handler);
            //specify to use TLS 1.2 as default connection
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            httpRestClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpContent jsonFenceContent         = new StringContent(getJsonCircularFence());
            jsonFenceContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage serverResponse   = httpRestClient.PostAsync(new Uri(bdRestUrl), jsonFenceContent).Result;
            if (serverResponse.IsSuccessStatusCode)
            {
                var result = serverResponse.Content.ReadAsStringAsync().Result;
                Console.WriteLine("{0}", result);
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int) serverResponse.StatusCode, serverResponse.Content.ReadAsStringAsync().Result);
            }
        }
        /*Circular fence requires a centerpoint and radius*/
        private static String getJsonCircularFence()
        {
            String circularFenceJson =
            "{" +
                "\"security\": {" +
                 "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                 "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
             "}," +
             "\"content\": {" +
                 "\"zone\": {" +
                     "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                     "\"fences\": {" +
                         "\"circles\": [" +
                             "{" +
                                 "\"fenceId\":" + "\"" + bdCircularFenceId + "\"," +
                                 "\"name\": \"Circle-1-Updated\"," +
                                 "\"color\": \"#000ffff\"," +
                                 "\"radius\": \"10.330384237225\"," +
                                 "\"center\": {" +
                                     "\"latitude\": \"-37.818326\"," +
                                     "\"longitude\": \"144.979318\" " +
                                 "}" +
                             "}" +
                         "]" +
                     "}" +
                 "}" +
             "}" +
          "}";
            return circularFenceJson;
        }
        /*Bounding box requires north east and south west points*/
        private static String getJsonBoundingBox()
        {
            String boundingBoxFenceJson =
                    "{" +
                       "\"security\": {" +
                            "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                            "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                         "\"fences\": {" +
                            "\"rectangles\": [" +
                                "{" +
                                   "\"fenceId\":" + "\"" + bdBoundingBoxFenceId + "\"," +
                                    "\"name\": \"Bounding Box-1-Updated\"," +
                                    "\"color\": \"#3559e\"," +
                                    "\"northEast\": {" +
                                        "\"latitude\": -37.81544591805361," +
                                        "\"longitude\": 144.9786114692688" +
                                    "}," +
                                    "\"southWest\": {" +
                                        "\"latitude\": -37.81758175613945," +
                                        "\"longitude\": 144.9731397628784" +
                                    "}" +
                                "}" +
                            "]" +
                        "}" +
                    "}" +
                 "}" +
               "}";
            return boundingBoxFenceJson;
        }
        /*Polygonal fence requires a series points in lat/long*/
        private static String getJsonPolygonalFence()
        {
            String polygonalFenceJson =
                           "{" +
                               "\"security\": {" +
                                   "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                                   "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                            "}," +
                            "\"content\": {" +
                                "\"zone\": {" +
                                    "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                                    "\"fences\": {" +
                                    "\"polygons\": [" +
                                        "{" +
                                            "\"fenceId\":" + "\"" + bdPolygonalFenceId + "\"," +
                                            "\"name\": \"Polygon-2\"," +
                                            "\"color\": \"#000ffff\"," +
                                            "\"vertices\": [" +
                                                 "{" +
                                                     "\"latitude\": -37.818717," +
                                                     "\"longitude\": 144.983085" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.819540," +
                                                    "\"longitude\": 144.982125" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.820298," +
                                                    "\"longitude\": 144.985178" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.820468," +
                                                    "\"longitude\": 144.984228" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.818768," +
                                                    "\"longitude\": 144.984330" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.819476," +
                                                    "\"longitude\": 144.985033" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.820527," +
                                                    "\"longitude\": 144.982978" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.818887," +
                                                    "\"longitude\": 144.982587" +
                                                 "}" +
                                            "]" +
                                        "}" +
                                    "]" +
                                "}" +
                            "}" +
                         "}" +
                    "}";
            return polygonalFenceJson;
        }
        /*Geoline fence requires a series points in lat/long*/
        private static String getJsonGeolineFence()
        {
            String geolineFenceJson =
                           "{" +
                               "\"security\": {" +
                                   "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                                   "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                            "}," +
                            "\"content\": {" +
                                "\"zone\": {" +
                                    "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                                    "\"fences\": {" +
                                        "\"polylines\": [" +
                                        "{" +
                                            "\"fenceId\":" + "\"" + bdGeolineFenceId + "\"," +
                                            "\"name\": \"Geoline-1\"," +
                                            "\"color\": \"#a0d7be\"," +
                                            "\"vertices\": [" +
                                                 "{" +
                                                     "\"latitude\": -37.81527640444762," +
                                                     "\"longitude\": 144.975049495697" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.80735968553275," +
                                                    "\"longitude\": 144.9712514877319" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.80581692842606," +
                                                    "\"longitude\": 144.9556946754456" +
                                                 "}," +
                                                  "{" +
                                                    "\"latitude\": -37.82136169906169," +
                                                    "\"longitude\": 144.9547076225281" +
                                                 "}" +
                                            "]" +
                                        "}" +
                                    "]" +
                                "}" +
                            "}" +
                         "}" +
                    "}";
            return geolineFenceJson;
        }
    }
}
Created by Bluedot DevOps on March 5, 2018

Start the discussion