C# Sample

This sample loads last 1000 messages for a specified device.

using System;
using System.Collections.Generic;
using System.Net;
// use NuGET to add Json.NET library
using Newtonsoft.Json;

namespace ApiDemo
{
    class Program
    {
        private const string serverUri = "https://api.globalalerting.com";
        private const string username = "user@domain.com";
        private const string password = "mypassword";
        private const string tenantCode = "1234";
        private const string deviceId = "my-device";

        static void Main(string[] args)
        {
            string resourceUrl = String.Format("/{0}/devices/{1}/momessages?format=json", tenantCode, deviceId);
            List<MoMessage> moMessages = Get<List<MoMessage>>(resourceUrl);

            foreach (var moMessage in moMessages)
                Console.WriteLine("{0} {1}", moMessage.SentFromDevice, moMessage.Message);
        }

        private static T Get<T>(string resourceUrl)
        {
            WebClient webClient = new WebClient();
            webClient.Credentials = new NetworkCredential(username, password);
            webClient.BaseAddress = serverUri;
            webClient.Headers["Accept"] = "application/json";
            webClient.Headers["Content-Type"] = "application/json";

            string json = webClient.DownloadString(resourceUrl);

            T data = JsonConvert.DeserializeObject<T>(json);

            return data;
        }

        #region Models

        public class MoMessage
        {
            public byte[] Attachment { get; set; }
            public EmergencyState EmergencyState { get; set; }
            public string Message { get; set; }
            public string MessageId { get; set; }
            public string MessageType { get; set; }
            public Position Position { get; set; }
            public DateTime ReceivedAtServer { get; set; }
            public string Recipients { get; set; }
            public DateTime SentFromDevice { get; set; }
            public int SequenceNumber { get; set; }
            public List<Property> PropertyBag { get; set; }
        }

        public class Position
        {
            public double? Latitude { get; set; }
            public double? Longitude { get; set; }
            public DateTime? GpsTimestamp { get; set; }
            public double? Speed { get; set; }
            public double? Heading { get; set; }
            public double? Altitude { get; set; }
            public LocationSourceType LocationSource { get; set; }
            public string Accuracy { get; set; }
        }

        public class Property
        {
            public string PropertyName { get; set; }
            public string PropertyValue { get; set; }
        }

        public enum LocationSourceType
        {
            Gps = 0,
            Glonass = 1,
            Doppler = 2,
            Unknown = 3
        }

        public enum EmergencyState
        {
            None = 0,
            Emergency = 1,
            EmergencyCancel = 2,
        }

        #endregion
    }
}

PHP Sample

This sample loads last 1000 messages for a specified device.

#!/usr/bin/php
<?php 
    $auth="user@domain.com:mypassword";
    $device="my-device";
    $tenant="1234";
    $url = sprintf("https://api.globalalerting.com/%s/devices/%s/momessages", $tenant, $device);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($curl, CURLOPT_USERPWD,$auth); 
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response= curl_exec($curl);
    
    print $response;
?>

Python Sample

This sample compatible with Python 2.7+ loads last 1000 messages for a specified device.

# compatible with Python 2.7+
import urllib2
import base64
import json

username = 'user@domain.com'
password = 'mypassword'
tenant_id = '1234'
device_id = 'my-device'

url = 'https://api.globalalerting.com/{}/devices/{}/momessages'.format(tenant_id, device_id)
auth = base64.b64encode('{}:{}'.format(username, password))
headers = {'Content-Type': 'application/json', 'Authorization': 'Basic {}'.format(auth)}

request = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(request)

mo_messages = json.loads(response.read())
print(mo_messages)

This sample compatible with Python 3.4+ loads last 1000 messages for a specified device.

# compatible with Python 3.4+
import urllib.request
import base64
import json

username = 'user@domain.com'
password = 'mypassword'
tenant_id = '1234'
device_id = 'my-device'

url = 'https://api.globalalerting.com/{}/devices/{}/momessages'.format(tenant_id, device_id)
auth = base64.b64encode(bytes('{}:{}'.format(username, password), 'ascii'))
headers = {'Content-Type': 'application/json', 'Authorization': 'Basic {}'.format(auth.decode('utf-8'))}

request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)

mo_messages = json.loads(response.read().decode('utf-8', errors='ignore'))
print(mo_messages)