POST Create Multiple Fences – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Create Multiple fence client demonstrates adding fences of various shapes in a single JSON request to an existing zone
 * using 'request' node module.
 */
var request = require('request');   
var multipleFenceData =
    {
        "security": {
            /* The apiKey is generated when you create an application. */
            "apiKey": "386dc374-0a57-47f3-beb7-5862cade8a31",
            /* 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": "7cd1ea80-d40e-11e4-84cb-b8ca3a6b879d"
        },
        "content": {
            "zone": {
                /* The zoneId is the id of the zone being updated. This can be fetched by calling GET zones API */
                "zoneId": "95b8285d-f85d-48d1-9c4d-b2e7f12ec6bf",
                "fences": {
                    "circles": [
                        {
                            "order": 1,
                            "name": "Test Circular fence with 8M radius",
                            "color": "#000fff",
                            "radius": 8,
                            "center": {
                                "latitude": "-37.8159544565362",
                                "longitude": "144.9723565578461"
                            }
                        },
                        {
                            "order": 2,
                            "name": "Test Circular fence with 16M radius",
                            "color": "#fff234",
                            "radius": 16,
                            "center": {
                                "latitude": "-37.8164587537386",
                                "longitude": "144.978997707366"
                            }
                        }
                    ],
                    "rectangles": [
                        {
                            "order": 3,
                            "name": "A bounding box with north east and south west",
                            "color": "#3559ef",
                            "northEast": {
                                "latitude": "-37.81544591805361",
                                "longitude": "144.9786114692688"
                            },
                            "southWest": {
                                "latitude": "-37.81758175613945",
                                "longitude": "144.9731397628784"
                            }
                        }
                    ],
                    "polygons": [
                        {
                            "order": 4,
                            "name": "A Polygonal Fence",
                            "color": "#000fff",
                            "vertices": [
                                {
                                    "latitude": "-37.818717",
                                    "longitude": "144.983085"
                                },
                                {
                                    "latitude": "-37.81954",
                                    "longitude": "144.982125"
                                },
                                {
                                    "latitude": "-37.820298",
                                    "longitude": "144.985178"
                                },
                                {
                                    "latitude": "-37.820468",
                                    "longitude": "144.984228"
                                },
                                {
                                    "latitude": "-37.818887",
                                    "longitude": "144.982587"
                                }
                            ]
                        }
                    ],
                    "polylines": [
                        {
                            "order": 5,
                            "name": "PolyLine Around MCG",
                            "color": "#000fff",
                            "vertices": [
                                {
                                    "latitude": -37.818717,
                                    "longitude": 144.983085
                                },
                                {
                                    "latitude": -37.81954,
                                    "longitude": 144.982125
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
  
var options = {
    uri: 'https://api.bluedotinnovation.com/1/fences',
    method: 'POST',
    json: multipleFenceData
};
  
var beforeDate = new Date();
request(options,
    function (error, response) {
        var afterDate = new Date();
        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.ClientProtocolException;
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.
 * Add Multiple fence client demonstrates adding fences of various shapes in a single JSON request to an existing zone
 * using Apache HTTP client and JSON Simple libraries.
 */
public class AddMultipleFences extends BDCommon {
     
   private static String bdCustomerApiKey    = "7cd1ea80-d40e-22e4-84cb-b8ca3a6b879d"; //This key is generated by Bluedot Point Access UI when your account is created.
   private static String bdApplicationApiKey = "c019116b-3723-4773-bbca-e399f1cad738"; //This apiKey is generated when you create an application
   private static String bdZoneId            = "cb5b2c75-d2bb-471e-9ff6-a4d59b31422d"; //This is the id of the zone being updated. This can be fetched by calling GET Zones API
   private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/fences";
      
   /**
    * @param args
    * @throws ClientProtocolException
    * @throws IOException
    * @throws NoSuchAlgorithmException
    * @throws KeyManagementException
    * @throws ParseException
    */
   public static void main(String[] args) throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException, ParseException {
        
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
           
        JSONParser parser    = new JSONParser();
        JSONObject bdMultipleFenceJSONObject = (JSONObject) parser.parse(getJsonMultipleFence());
                            
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdMultipleFenceJSONObject.toJSONString(), Charset.defaultCharset()));
          
        HttpResponse response = httpRestClient.execute(postRequest);
                           
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Fence was successfully created");
            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 {
            System.out.println("Error creating new fence");
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json error result
            System.out.println(resultString);
        }  
   }
         
   /*This Json contains an example of all the different fence geometry types*/      
    private static String getJsonMultipleFence() {
        String multipleFenceJson =
        "{" +
            "\"security\": {" +
             "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
             "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
         "}," +
         "\"content\": {" +
             "\"zone\": {" +
                 "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                 "\"fences\": {" +
                     "\"circles\": [" +
                         "{" +
                             "\"order\": 1," +
                             "\"name\": \"Circular fence-added through API 01\"," +
                             "\"color\": \"#000fff\"," +
                             "\"radius\": 20.235," +
                             "\"center\": {" +
                                 "\"latitude\": -37.818326," +
                                 "\"longitude\": 144.979318" +
                             "}" +
                         "}," +
                         "{" +
                             "\"order\": 2," +
                             "\"name\": \"Circular fence-added through API 02\"," +
                             "\"color\": \"#000fff\"," +
                             "\"radius\": 30," +
                             "\"center\": {" +
                                 "\"latitude\": 37.8164587537386," +
                                 "\"longitude\": 144.978997707366" +
                             "}" +
                         "}" +
                     "]," +
                     "\"rectangles\": [" +
                        "{" +
                            "\"order\": 3," +
                            "\"name\": \"Bounding Box-1\"," +
                            "\"color\": \"#3559ef\"," +
                            "\"northEast\": {" +
                                "\"latitude\": -37.81544591805361," +
                                "\"longitude\": 144.9786114692688" +
                            "}," +
                            "\"southWest\": {" +
                                "\"latitude\": -37.81758175613945," +
                                "\"longitude\": 144.9731397628784" +
                            "}" +
                        "}" +
                    "]," +
                    "\"polygons\": [" +
                        "{" +
                            "\"order\": 3," +
                            "\"name\": \"Polygon-2\"," +
                            "\"color\": \"#000fff\"," +
                            "\"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" +
                                 "}" +
                            "]" +
                        "}" +
                    "]," +
                    "\"polylines\": [" +
                        "{" +
                            "\"order\": 4," +
                            "\"name\": \"Geoline-1\"," +
                            "\"color\": \"#000fff\"," +
                            "\"vertices\": [" +
                                 "{" +
                                    "\"latitude\": -37.819540," +
                                    "\"longitude\": 144.982125" +
                                 "}," +
                                 "{" +
                                    "\"latitude\": -37.820298," +
                                    "\"longitude\": 144.985178" +
                                 "}" +
                            "]" +
                        "}" +
                    "]" +
                 "}" +
             "}" +
         "}" +
        "}";
        return multipleFenceJson;
    }
}
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 Public API client demonstrates adding fences of various shapes in a single JSON request to an existing zone using net http web api library
*/
 
namespace BluedotPublicApiClient.fenceclient
{
    public class BDAddMultipleFencesClient
    {
        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 bdRestUrl = "https://api.bluedotinnovation.com/1/fences";
 
        public void addFence()
        {
            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(getJsonMultipleFence());
            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.ToString());
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)serverResponse.StatusCode, serverResponse.Content.ReadAsStringAsync().Result);
            }
        }
 
 
        /*This Json contains an example of all the different fence geometry types*/
        private static String getJsonMultipleFence()
        {
            String multipleFenceJson =
            "{" +
                "\"security\": {" +
                 "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                 "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
             "}," +
             "\"content\": {" +
                 "\"zone\": {" +
                     "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                     "\"fences\": {" +
                         "\"circles\": [" +
                             "{" +
                                 "\"order\": 1," +
                                 "\"name\": \"Circular fence-added through API 01\"," +
                                 "\"color\": \"#000fff\"," +
                                 "\"radius\": 20.235," +
                                 "\"center\": {" +
                                     "\"latitude\": \"-37.818326\"," +
                                     "\"longitude\": \"144.979318\" " +
                                 "}" +
                             "}," +
                             "{" +
                                 "\"order\": 2," +
                                 "\"name\": \"Circular fence-added through API 02\"," +
                                 "\"color\": \"#000fff\"," +
                                 "\"radius\": 30," +
                                 "\"center\": {" +
                                     "\"latitude\": \"37.8164587537386\"," +
                                     "\"longitude\": \"144.978997707366\" " +
                                 "}" +
                             "}" +
                         "]," +
                         "\"rectangles\": [" +
                            "{" +
                                "\"order\": 3," +
                                "\"name\": \"Bounding Box-1\"," +
                                "\"color\": \"#3559ef\"," +
                                "\"northEast\": {" +
                                    "\"latitude\": -37.81544591805361," +
                                    "\"longitude\": 144.9786114692688" +
                                "}," +
                                "\"southWest\": {" +
                                    "\"latitude\": -37.81758175613945," +
                                    "\"longitude\": 144.9731397628784" +
                                "}" +
                            "}" +
                        "]," +
                        "\"polygons\": [" +
                            "{" +
                                "\"order\": 4," +
                                "\"name\": \"Polygon-2\"," +
                                "\"color\": \"#000fff\"," +
                                "\"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" +
                                     "}" +
                                "]" +
                            "}" +
                        "]," +
                        "\"polylines\": [" +
                        "{" +
                            "\"order\": 5," +
                            "\"name\": \"Geoline-1\"," +
                            "\"color\": \"#000fff\"," +
                            "\"vertices\": [" +
                                 "{" +
                                    "\"latitude\": -37.819540," +
                                    "\"longitude\": 144.982125" +
                                 "}," +
                                 "{" +
                                    "\"latitude\": -37.820298," +
                                    "\"longitude\": 144.985178" +
                                 "}" +
                            "]" +
                        "}" +
                    "]" +
                     "}" +
                 "}" +
             "}" +
            "}";
            return multipleFenceJson;
        }
    }
}
Created by Bluedot DevOps on January 31, 2018

Start the discussion