POST Create Zone – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Create Zone  client demonstrates adding a Zone  to an existing application using 'request' node module.
 */
var request = require('request');
//JSON for a Zone  which is active for a certain time period during the day. Time values have a format of HH:MM and the period value has to be one of {am/pm}
var zoneTimeActiveData =
    {
        "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": {
                "zoneName": "starbucks",
                "minimumRetriggerTime": "00:01",
                "enableCheckOut": false,
                "timeActive": {
                    "from": {
                        /* Time in Hour:Minute format.*/
                        "time": "0:00",
                        "period": "am"
                    },
                    "to": {
                        /* Time in Hour:Minute format.*/
                        "time": "12:00",
                        "period": "pm"
                    }
                }
            }
        }
    };
//JSON for a Zone which is active all day.
var zoneActiveAllDayData =
{
    "security": {
        "apiKey" : "c2c8211f-796f-4eda-b6ce-05467b5263a9",
        /*
         customerApiKey is generated when customer registers first time. It is also available
         on the PointAccess interface in the Edit Profile section.
         */       
        "customerApiKey": "86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            "zoneName": "starbucks",
            "minimumRetriggerTime": "00:01",
            "enableCheckOut": false,
            "activeAllDay": true
        }
    }
};
var options = {
    uri: 'https://api.bluedotinnovation.com/1/zones',
    method: 'POST',
    json: zoneTimeActiveData
};
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.
 * Add Zone client demonstrates adding a Zone to an existing application using Apache HTTP client and JSON Simple libraries.
 */
public class AddZone extends BDCommon {
     
    private static String bdApplicationApiKey = "9beebb18-ec2e-4f30-8e1c-2286c77007d4"; //This apiKey is generated when you create an application
    private static String bdCustomerApiKey    = "86577370-7b91-11e4-bcb7-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created
    private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/zones";
    public static void main(String[] args) throws ParseException, IOException, KeyManagementException, NoSuchAlgorithmException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
         
        JSONParser parser    = new JSONParser();
        JSONObject bdZoneJSONObject = (JSONObject) parser.parse(getJsonZoneTimeActive()); //JSON of Zone  which is active during 06:00 AM - 11:00 PM
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdZoneJSONObject.toJSONString(), Charset.defaultCharset()));
      
        HttpResponse response = httpRestClient.execute(postRequest);
                         
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println(Zone was added 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);
        }          
    }
    /*Return JSON for a Zone which is active for a certain time period during the day. Time values have a format of HH:MM and the period value has to be one of {am/pm}*/
    private static String getJsonZoneTimeActive() {
        return "{" +
             "\"security\": {" +
                "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
            "}," +
           "\"content\": {" +
                 "\"zone\": {" +
                    "\"zoneName\": \"First Test Bluedot Zone"\"," +
                    "\"minimumRetriggerTime\": \"11:11\"," +
                    "\"enableCheckOut\": true," +
                    "\"timeActive\" : {" +
                        "\"from\": {" +
                            "\"time\": \"06:00\"," +
                            "\"period\": \"am\"" +
                        "}," +
                        "\"to\": {" +
                            "\"time\": \"11:00\"," +
                            "\"period\": \"pm\"" +
                        "}" +
                    "}" +
                "}" +
            "}" +
        "}";
    }
     
    /*Return JSON for a Zone which is active all day.*/
    private static String getJsonZoneActiveAllDay() {
        return "{" +
            "\"security\": {" +
                       "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                       "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
           "}," +
          "\"content\": {" +
               "\"zone\": {" +
                   "\"zoneName\": \"First Test Bluedot Zone-Updated\"," +
                   "\"minimumRetriggerTime\": \"11:11\"," +
                   "\"enableCheckOut\": true," +
                   "\"activeAllDay\": true" +
               "}" +
           "}" +
       "}";
    }
}
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.
* Add Zone client demonstrates adding a Zone to an existing application
*/
namespace BluedotPublicApiClient.zoneclient
{
    public class CreateZone
    {
        private static String bdCustomerApiKey      = "76e1ae30-c616-11e5-a7c0-b8ca3a6b879d"; //This apiKey is generated when you create an application
        private static String bdApplicationApiKey   = "dee11930-ebff-11e5-8e27-bc305bf60831"; //This key is generated by Bluedot Point Access UI when your account is created
        private static String bdRestUrl             = "https://api.bluedotinnovation.com/1/zones";
        public void createZoneWithTimeActive()
        {
            postToService(getJsonZoneTimeActive());
        }
        public void createZoneActiveAllDay()
        {
            postToService(getJsonZoneActiveAllDay());
        }
        private void postToService(String json)
        {
            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(json);
            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);
            }
        }
        /*Return JSON for a Zone which is active for a certain time period during the day. Time values have a format of HH:MM and the period value has to be one of {am/pm}*/
        private static String getJsonZoneTimeActive()
        {
            return "{" +
                    "\"security\": {" +
                            "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                            "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
                "}," +
                "\"content\": {" +
                        "\"zone\": {" +
                        "\"zoneName\": \"First Test Bluedot Zone"\"," +
                        "\"minimumRetriggerTime\": \"11:11\"," +
                        "\"enableCheckOut\": true," +
                        "\"timeActive\" : {" +
                            "\"from\": {" +
                                "\"time\": \"06:00\"," +
                                "\"period\": \"am\"" +
                            "}," +
                            "\"to\": {" +
                                "\"time\": \"11:00\"," +
                                "\"period\": \"pm\"" +
                            "}" +
                        "}" +
                    "}" +
                "}" +
            "}";
        }
        /*Return JSON for a Zone which is active all day.*/
        private static String getJsonZoneActiveAllDay()
        {
            return "{" +
                "\"security\": {" +
                            "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                            "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneName\": \"First Test Bluedot Zone"\"," +
                        "\"minimumRetriggerTime\": \"11:11\"," +
                        "\"enableCheckOut\": true," +
                        "\"activeAllDay\": true" +
                    "}" +
                "}" +
            "}";
        }
    }
}
Created by Bluedot DevOps on January 17, 2018

Start the discussion