POST Update Zone – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Update Zone client demonstrates updating fields of an existing Zone using 'request' node module.
 * 100 Zones are returned per call, increment the page number parameter to get records corresponding to the page.
 */
var request = require('request');
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": {
                /* The zoneId is the id of the Zone being updated. This can be fetched by calling GET Zones API */
                "zoneId":"cc6f9dd1-3d69-454d-abdd-58176cbf67dc",
                "zoneName": "starbucks",
                /* Time in Hour:Minute format.*/
                "minimumRetriggerTime": "00:01",
                "enableCheckOut": true,
                "timeActive": {
                    "from": {
                        /* Time in Hour:Minute format.*/
                        "time": "0:00",
                        "period": "am"
                    },
                    "to": {
                        /* Time in Hour:Minute format.*/
                        "time": "12:00",
                        "period": "pm"
                    }
                }
            }
        }
    };
var zoneActiveAllDayData =
{
    "security": {
        /* The apiKey is generated when you create an application. */
        "apiKey" : 'c2c8211f-796f-4eda-b6ce-05467b5263a9',
        /* The 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": {
            /* The zoneId is the id of the Zone being updated. This can be fetched by calling GET Zones API */
            "zoneId":"cc6f9dd1-3d69-454d-abdd-58176cbf67dc",
            "zoneName": "starbucks",
            /* Time in Hour:Minute format.*/
            "minimumRetriggerTime": "00:01",
            "enableCheckOut": true,
            "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.
 * Update Zone client demonstrates updating fields of an existing Zone using Apache HTTP client and JSON Simple libraries.
 */
public class UpdateZone extends BDCommon {
     
    private static String bdApplicationApiKey = "5a9b1b78-3dd7-4f4d-8608-82408f3baf4c"; //This apiKey is generated when you create an application.
    private static String bdCustomerApiKey    = "a6598740-75f5-11e4-86ca-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created
    private static String bdZoneId            = "d6c3b688-cf2e-4aac-b76b-b29f371f448e"; //This is the ID of the Zone being updated.
    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 updated 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\": {" +
                   "\"zoneId\":" + "\"" + bdZoneId + "\"" +
                   "\"zoneName\": \"First Test Bluedot Zone-Updated\"," +
                   "\"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\": {" +
                   "\"zoneId\":" + "\"" + bdZoneId + "\"" +
                   "\"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.
 * Update Zone client demonstrates updating fields of a Zone
 */
namespace BluedotPublicApiClient.zoneclient
{
    public class UpdateZone
    {
        private static String bdApplicationApiKey   = "5a9b1b78-3dd7-4f4d-8608-82408f3baf4c"; //This apiKey is generated when you create an application
        private static String bdCustomerApiKey      = "a6598740-75f5-11e4-86ca-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created
        private static String bdZoneId              = "093189c3-9827-44c0-b11e-318c8c985772"; //This is the ID of the Zone being updated.
        private static String bdRestUrl             = "https://api.bluedotinnovation.com/1/zones";
        public void update()
        {
            postToService(getJsonZoneTimeActive());
        }
        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\": {" +
                        "\"zoneId\":" + "\"" + bdZoneId + "\"" +
                        "\"zoneName\": \"First Test Bluedot Zone-Updated\"," +
                        "\"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\": {" +
                        "\"zoneId\":" + "\"" + bdZoneId + "\"" +
                        "\"zoneName\": \"First Test Bluedot Zone-Updated\"," +
                        "\"minimumRetriggerTime\": \"11:11\"," +
                        "\"enableCheckout\": true," +
                        "\"activeAllDay\": true" +
                    "}" +
                "}" +
            "}";
        }
    }
}
Created by Bluedot DevOps on March 5, 2018

Start the discussion