POST Add Beacons to Zone – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Add Beacon to Zone client demonstrates adding a Beacon to an existing Zone of your Bluedot backend
 * using 'request' node module.
 */
var request = require('request');
    var zoneData =
    {
        "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": "d69781dc-3cb1-4303-9888-1800060fabb6",
                "beacons": [
                    {
                        /*This is the ID of a Beacon registered to a customer's account,
                         * which has to be associated with a Zone.  */
                        "beaconId" : "f4d3c2f3-b2ba-4529-adac-18af1598ee4f",
                        "proximity" : 1,
                        "order": 1
                    }
                ]
            }
        }
};
 
var options = {
    uri: 'https://api.bluedotinnovation.com/1/zones/beacons',
    method: 'POST',
    json: zoneData
};
 
request(options,
    function (error, response, body) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.beacon;
 
import java.io.IOException;
import java.io.InputStream;
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 Beacon to Zone client demonstrates adding a Beacon to an existing Zone of your Bluedot backend using Apache HTTP client and JSON Simple libraries.
 */
 
public class AddBeaconToZone extends BDCommon {
     
    private static String bdCustomerApiKey    = "7cd1ea80-d40e-11e4-84cb-b8ca3a6b869d"; //This key is generated by Bluedot Point Access UI when your account is created.
    private static String bdApplicationApiKey = "afc346a0-de5e-11e4-af33-b8ca3a7b879d"; //This apiKey is generated when you create an application
    private static String bdZoneId            = "bbef1027-9d47-4e5b-95f2-a9208715348a"; //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/zones/beacons";
         
    /**
     * @param args
     * @throws IOException
     * @throws IllegalStateException
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public static void main(String[] args) throws IllegalStateException, IOException, ParseException, KeyManagementException, NoSuchAlgorithmException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
         
        JSONParser parser    = new JSONParser();
        JSONObject bdAddBeaconToZoneJSONObject = (JSONObject) parser.parse(getJsonBeaconToZone()); //add a Beacon to a Zone
         
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdAddBeaconToZoneJSONObject.toJSONString()));
         
        HttpResponse response = httpRestClient.execute(postRequest);
         
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Beacon 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 {
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json error result
            System.out.println(resultString);
        }
    }
     
    private static String getJsonBeaconToZone() {
         
        String addBeaconToZone =
                "{" +
                   "\"security\": {" +
                        "\"apiKey\":"+ "\"" + bdApplicationApiKey +"\","+
                        "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" "+
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {"+
                            "\"zoneId\":" + "\"" + bdZoneId + "\","+
                            "\"beacons\": [" +
                                "{" +
                                   "\"beaconId\":  \"b22ea927-f757-4a8f-8ba7-2960a4b688b2\"," +
                                   "\"proximity\": 1," +
                                   "\"order\": 1" +
                                "}"+
                            "]"+
                        "}"+
                    "}"+
             "}";
        return addBeaconToZone;
    }
}
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.Web.Script.Serialization;
using System.IO;
using System.Security.Cryptography.X509Certificates;
 
/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Add Beacon to Zone client demonstrates adding a Beacon to an existing Zone to an existing application
 */
 
namespace BluedotPublicApiClient.beaconclient
{
    public class  BDAddBeacontoZone
    {
        private static String bdApplicationApiKey = "a251fbd2-5035-4887-896d-0b56abb4102d"; // This apiKey is generated when you create an application a
        private static String bdCustomerApiKey    = "dc67d0e0-397b-11e5-9931-0646bf53d23f"; // This key is generated by Bluedot Point Access UI when your account is created
        private static String bdZoneId            = "9bf7ff72-cb3e-4ef2-b1b6-b8aa5a119256"; // This is the id of the Zone being updated. This can be fetched by calling GET Zones API
        private static String bdBeaconId          = "b7db6f18-6557-4903-b0a7-b35b96d54fec"; // This is the id of the Beacon being updated and retrived from Bluedot UI.
        private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/zones/beacons";
          
         public void create()
        {
            postToService(getjsonAddBeacontoZone());
        }
         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 jsonAddBeacontoZoneContent = new StringContent(json);
            jsonAddBeacontoZoneContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
            HttpResponseMessage serverResponse = httpRestClient.PostAsync(new Uri(bdRestUrl), jsonAddBeacontoZoneContent).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);
            }
        }
         
        private static String getjsonAddBeacontoZone()
        {
            return "{" +
                 "\"security\": {" +
                            "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                            "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
                "}," +
               "\"content\": {" +
                     "\"zone\": {" +
                        "\"zoneId\":" + "\"" + bdZoneId + "\" "+
                        "\"beacons\": [" +
                            "{" +
                                "\"beaconId\": " +" \"" + bdBeaconId + "\" " +
                                "\"proximity\": 1," +
                                "\"order\": 1" +
                            "}"+
                        "]"+
                    "}"+
                "}"+
            "}";
        }
    }
}
Created by Bluedot DevOps on January 22, 2018

Start the discussion