POST Sequence Geofeatures – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Post Geofeature Sequence client demonstrates zones/sequence endpoint can be used to sequence/order the geofeatures in a Zone
 * to be used for triggering the Sequential condition using 'request' node module.
 */
var request = require('request');
 
var geofeatureSequence =
    {
        "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": "a6375661-07e5-4596-800e-eef37d3cd6a6",
                /*An ordered list of fence and beacon identifiers to be used for the Sequence condition.*/
                "sequence": [
                    "fdf1a69b-090a-48f7-99d0-7eb631f22eac",
                    "fbe38318-9b71-410e-98cc-10c64cdb7be4"
                ]
            }
        }
    };
 
var options = {
    uri: 'https://api.bluedotinnovation.com/1/zones/sequence',
    method: 'POST',
    json: geofeatureSequence
};
 
request(options,
    function (error, response, body) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.zone;
 
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.
 * Post Geofeature Sequence client demonstrates zones/sequence endpoint can be used to sequence/order the geofeatures in a Zone
 * to be used for triggering the Sequential condition using Apache HTTP client and JSON Simple libraries.
 */
 
public class PostGeofeatureSequence extends BDCommon {
     
    //This key is generated by Bluedot Point Access UI when your account is created.
    private static String bdCustomerApiKey    = "86577370-7b91-11e4-bcb7-a0481cdc3311";
     
    //This apiKey is generated when you create an application
    private static String bdApplicationApiKey = "9beebb18-ec2e-4f30-8e1c-2286c77007d4";
     
    //This is the ID of the zone being updated.
    private static String bdZoneId            = "d6c3b688-cf2e-4aac-b76b-b29f371f448e";
     
    //An ordered list of fence and beacon identifiers to be used for the Sequence condition.
    private static String[] bdSequence        = { "fdf1a69b-090a-48f7-99d0-7eb631f22eac", "fbe38318-9b71-410e-98cc-10c64cdb7be4"}; 
     
    private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/zones/sequence";
 
    public static void main(String[] args) throws ParseException, IOException, KeyManagementException, NoSuchAlgorithmException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
         
        JSONParser parser    = new JSONParser();
        JSONObject bdSequenceJSONObject = (JSONObject) parser.parse(getJsonSequenceGeofeatures()); //JSON of zone's geofeature sequence.
 
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdSequenceJSONObject.toJSONString(), Charset.defaultCharset()));
      
        HttpResponse response = httpRestClient.execute(postRequest);
                         
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("The Geofeatures of the Zone have been ordered successfully.");
            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);
        }          
 
    }
 
     
    /**/
    private static String getJsonSequenceGeofeatures() {
        return "{" +
            "\"security\": {" +
                   "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                   "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
           "}," +
           "\"content\": {" +
               "\"zone\": {" +
                   "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                   "\"sequence\": [ \"" + bdSequence[0] + "\",\"" + bdSequence[1] + "\"]"+
               "}" +
           "}" +
       "}";
    }
}
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.Web.Script.Serialization;
using System.Security.Cryptography.X509Certificates;
 
/**
* @author Bluedot Innovation
* Copyright (c) 2016 Bluedot Innovation. All rights reserved.
* Post Geofeature Sequence client demonstrates zones/sequence endpoint can be used to sequence/order the geofeatures in a Zone
* to be used for triggering the Sequential condition.
*/
namespace BluedotPublicApiClient.zoneclient
{
 
    public class PostGeofeaturesSequence
    {
         
        private void sequence()
        {
            String bdCustomerApiKey = "76e1ae30-c616-11e5-a7c0-b8ca3a6b879d"; //This key is generated by Bluedot Point Access UI when your account is created
            String bdApplicationApiKey = "dee11930-ebff-11e5-8e27-bc305bf60831"; //This key is generated by Bluedot Access UI when you register
            String bdZoneId = "d6c3b688-cf2e-4aac-b76b-b29f371f448e"; //This is the ID of the zone being updated.
            String[] bdSequence = { "fdf1a69b-090a-48f7-99d0-7eb631f22eac", "fbe38318-9b71-410e-98cc-10c64cdb7be4" }; //An ordered list of fence and beacon identifiers to be used for the Sequence condition.
            String bdRestUrl = "https://api.bluedotinnovation.com/1/zones/sequence";
 
            String zonesSequenceJson = "{" +
                "\"security\": {" +
                       "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                       "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
               "}," +
               "\"content\": {" +
                   "\"zone\": {" +
                       "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                       "\"sequence\": [ \"" + bdSequence[0] + "\",\"" + bdSequence[1] + "\"]" +
                   "}" +
               "}" +
           "}";
 
            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 jsonContent = new StringContent(zonesSequenceJson);
            jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
            HttpResponseMessage serverResponse = httpRestClient.PostAsync(new Uri(bdRestUrl), jsonContent).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);
            }
        }
    }
}
Created by Bluedot DevOps on January 31, 2018

Start the discussion