The following examples illustrate how third party dispatch systems interact with the Atlas Labs MobileCAD API.
The following C# code illustrates the procedure for logging in and obtaining an authentication token. The received token maybe used in both WebSockets and REST calls.
class Program { [DataContract] public class AuthResponse { [DataMember(Name = "access_token")] public string Token { get; set; } [DataMember(Name = "expires_in")] public int Expires { get; set; } } static void Main(string[] args) { string baseUrl = "https://api.atlaslabs.io"; string username = "test@domain.com"; string password = "honeybadger"; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (LoginAsync(client, username, password).Result) { Console.WriteLine("Login success!"); } else Console.WriteLine("Login failed!"); } } static async TaskLoginAsync(HttpClient client, string username, string password) { // Setup request for token authentication HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "Token"); var keyValues = new List > { new KeyValuePair ("grant_type", "password"), new KeyValuePair ("username", username), new KeyValuePair ("password", password) }; request.Content = new FormUrlEncodedContent(keyValues); HttpResponseMessage response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { if (response.Content == null) return false; // Successful header. Read content and save token for future requests AuthResponse auth = await response.Content.ReadAsAsync (); Console.WriteLine("Token expires in " + auth.Expires + " seconds"); // Save bearer token for future requests client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", auth.Token); return true; } return false; } }
Following successful authentication, clients should initialize their systems by synchronizing the API objects (vehicles, priorities, service levels, etc) with customer dispatch platforms. Typically this is done by retrieving a list of all API objects and comparing against the dispatch system, then forwarding updates, deletions, and additions back to the API.
// ExtRef1 and ExtRef2 are convenience fields for third party use and are recommended for storing mapping values // or import/export codes to customer dispatch systems Listpriorities = new List { new EntityDto(){ Name = "Alpha", ExtRef1 = "A", ExtRef2 = "1" }, new EntityDto(){ Name = "Bravo", ExtRef1 = "B", ExtRef2 = "1" }, new EntityDto(){ Name = "Charlie", ExtRef1 = "C", ExtRef2 = "1" }, new EntityDto(){ Name = "Delta", ExtRef1 = "D", ExtRef2 = "1" }, }; foreach (EntityDto dto in priorities) { using (HttpResponseMessage response = await client.PostAsJsonAsync ("api/Priority", dto)) { if (response.IsSuccessStatusCode) Console.WriteLine("Created priority {0} at {1}", dto.Name, response.Headers.Location); else Console.WriteLine("Error creating priority. Http status: {0}, Message: {1}", response.StatusCode, response.ReasonPhrase); } } List serviceLevels = new List { new EntityDto(){ Name = "BLS", ExtRef1 = "1", ExtRef2 = "2" }, new EntityDto(){ Name = "ALS", ExtRef1 = "2", ExtRef2 = "2" }, }; Foreach (EntityDto dto in serviceLevels) { using (HttpResponseMessage response = await client.PostAsJsonAsync ("api/ServiceLevel", dto)) { if (response.IsSuccessStatusCode) Console.WriteLine("Created service level {0} at {1}", dto.Name, response.Headers.Location); else Console.WriteLine("Error creating priority. Http status: {0}, Message: {1}", response.StatusCode, response.ReasonPhrase); } } // Import list of possible vehicle statuses before loading vehicles List vehicleStatus = new List { new EntityDto(){ Name = "Out of Service", ExtRef1 = "1", ExtRef2 = "0" }, new EntityDto(){ Name = "OOS Training", ExtRef1 = "2", ExtRef2 = "0" }, new EntityDto(){ Name = "Av Radio", ExtRef1 = "1", ExtRef2 = "1" }, new EntityDto(){ Name = "Av Quarters", ExtRef1 = "2", ExtRef2 = "1" }, new EntityDto(){ Name = "Assigned", ExtRef1 = "1", ExtRef2 = "2" }, }; foreach (EntityDto dto in vehicleStatus) { using (HttpResponseMessage response = await client.PostAsJsonAsync ("api/VehicleStatus", dto)) { if (response.IsSuccessStatusCode) Console.WriteLine("Created vehicle status {0} at {1}", dto.Name, response.Headers.Location); else Console.WriteLine("Error creating vehicle status. Http status: {0}, Message: {1}", response.StatusCode, response.ReasonPhrase); } } List vehicles = new List { new VehicleDto(){ Name = "001", Alias = "M-1", Status = "Out of Service", ExtRef1 = "001", ExtRef2 = "" }, new VehicleDto(){ Name = "002", Alias = "E-1", Status = "Out of Service", ExtRef1 = "002", ExtRef2 = "" } }; foreach (VehicleDto dto in vehicles) { using (HttpResponseMessage response = await client.PostAsJsonAsync ("api/Vehicle", dto)) { if (response.IsSuccessStatusCode) Console.WriteLine("Created vehicle {0} at {1}", dto.Name, response.Headers.Location); else Console.WriteLine("Error creating vehicle. Http status: {0}, Message: {1}", response.StatusCode, response.ReasonPhrase); } }