POST Update Beacon – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Update Beacon client demonstrates updating a Beacon to your Bluedot backend using 'request' node module
 */
var request = require('request');
var beaconData = {
    security: {
        /* The apiKey is generated when you create an application. */
        apiKey : "8e766280-0b3a-11e5-89f3-1289088180cb",
        /* 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: "203830a0-e272-11e4-a1e0-aa465fbb5c67"
    },
    content: {
        beacon : {
            /* This is the Id of the becon to be updated and can be retrieved through the GET Beacon call. */
            beaconId: "ad1a5c15-d3c7-4903-a067-d1cd5d11be1b",
            name: "Westfield",
            proximityUuid: "f7826da6-4fa2-4e98-8024-bc5b71e0893e",
            type: "Both",
            longitude: "123.34455",
            latitude: "47.777888",
            major: 12,
            minor: 13,
            macAddress: "00-15-E9-2B-99-3C",
            txPower: -77,
            description: "Westfield sample Beacon"
        }
    }
};
var options = {
    uri: 'https://api.bluedotinnovation.com/1/beacons',
    method: 'POST',
    json: beaconData
};
request ( options,
    function (error, response, body) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.beacon;
import com.bluedotinnovation.common.BDCommon;
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 java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Update Beacon client demonstrates updating a Beacon to your Bluedot backend using Apache HTTP client and JSON Simple libraries.
 */
public class UpdateBeacon extends BDCommon {
     
    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 bdApplicationApiKey = "13a9d8e0-b255-11e4-a423-00155d53317c"; //This apiKey is generated when you create an application
    private static String bdBeaconId          = "ad1a5c15-d3c7-4903-a067-d1cd5d11be1b"; //This is the id of the Beacon being updated. This can be fetched by calling GET Beacons API.
    private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/beacons";
    /**
     * @param args
     * @throws IOException
     * @throws ClientProtocolException
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public static void main(String[] args) throws ClientProtocolException, IOException, ParseException, KeyManagementException, NoSuchAlgorithmException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
         
        JSONParser parser    = new JSONParser();
        JSONObject bdUpdateBeaconJSONObject = (JSONObject) parser.parse(getJsonUpdateBeacon()); //update the Beacon value
         
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdUpdateBeaconJSONObject.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 getJsonUpdateBeacon() {
         
        String updateBeaconJson = 
                "{" +
                   "\"security\": {" +
                    "\"apiKey\":"+ "\"" + bdApplicationApiKey +"\","+
                    "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" "+
                "}," +
                "\"content\": {" +
                    "\"beacon\": {" +
                       "\"beaconId\":" + "\"" + bdBeaconId +"\"," +
                       "\"name\": \"Bluedot building\"," +
                       "\"proximityUUID\": \"f7826da6-4fa2-4e98-8024-bc4b71e0894e\"," +
                       "\"longitude\": \"123.34455\"," +
                       "\"latitude\": \"47.777888\"," +
                       "\"type\":  \"Both\"," +
                       "\"major\": 12," +
                       "\"minor\": 13," +
                       "\"macAddress\": \"01:17:C5:31:84:21\"," +
                       "\"txPower\": -77," +
                       "\"description\": \"Sample description\"" +
                    "}"+
                "}"+
             "}";
        return updateBeaconJson;
    }
         
}
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.
* Update Beacon client demonstrates adding a Beacon to to an existing the customer's account using net http web api library
* Pass beaconId of the Beacon to update
*/
namespace BluedotPublicApiClient.beaconclient
{
    class UpdateBeacon
    {
        private static String bdApplicationApiKey = "45f087e0-245c-11e4-a968-bc305bf60831"; //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 bdBeaconId          = "977444dc-968a-4b13-84d8-4b194b794c18"; //This is the ID of the Beacon being updated.
        private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/beacons";
       public void update()
        {
            postToService(getJsonUpdatedBeacon());
        }
          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 jsonUpdateBeaconContent = new StringContent(json);
            jsonUpdateBeaconContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage serverResponse = httpRestClient.PostAsync(new Uri(bdRestUrl), jsonUpdateBeaconContent).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);
            }
        }
        /*JSON to create a Beacon*/
        private static String getJsonUpdatedBeacon()
        {
          return "{" +
                 "\"security\": {" +
                 "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                 "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
             "}," +
             "\"content\": {" +
                   "\"beacon\": {" +
                        "\"beaconId\":" + "\"" + bdBeaconId + "\" " +
                        "\"name\": \"Beacon-15-test2\"," +
                        "\"proximityUUID\": \"f7826da6-4fa2-4e98-8024-bc5b71e0893e\"," +
                        "\"longitude\": \"123.34455\"," +
                        "\"latitude\": \"47.777888\"," +
                        "\"major\": 12," +
                        "\"minor\": 14," +
                        "\"macAddress\":\"01:17:C5:31:84:21\"," +
                        "\"txPower\": -77," +
                        "\"description\": \"Sample description\"" +
                    "}" +
                "}" +
          "}";
        }
    }
}
Created by Bluedot DevOps on March 5, 2018

Start the discussion