PUT Vehicle - C#

The PUT Vehicle method within the Customer Meta Data – Vehicles API enables integrated users to edit the vehicle object details within the referenced REVEAL account.

Before attempting to retrieve vehicle location information it is important to know if the REVEAL account has been updated with valid VEHICLE NUMBERS for each vehicle to be pulled via the API. The VEHICLE NUMBER and VEHICLE NAME fields are different fields within REVEAL and the VEHICLE NUMBER field is not automatically populated upon account creation.

Main Method

namespace Rest_Put_Vehicle_Example
{
class Program
{
static void Main()
{
// Token call goes here, see Rest_Token_Example

// App Id from Fleetmatics Integration Manager (FIM)
string appId = "companyname-p-us-4654sdf4fa351af65dsf1d";

// Put Vehicle URI
string putVehicleUri = "https://fim.api.ENV.fleetmatics.com/cmd/v1/vehicles/";

// Local variables that correspond to put body fields
string vehicleNum = "0112358";
string vehicleName = "TESTER";
string registrationNumber = "GA23423";
string vin = "1GNDX03E5YD117167";
string make = "Ford";
string model = "F150";
int? year = 2012;
double? tankCapacity = 20.5;
double? highwayMPG = 14.5;
double? cityMPG = 11.5;
int? vehicleSize = 1;

// Create new PUT_Vehicle object
var rest_put = new PUT_Vehicle();

// Pass in local variables to create a vehicle object
rest_put.CreateNewPutVehicle(vehicleName, vehicleNum, registrationNumber, vin, make, model, year, tankCapacity, highwayMPG, cityMPG, vehicleSize);

// Call method to execute the API call
rest_put.PutVehicle(/* token object or auth string passed here */, putVehicleUri, appId);
}
}
}

PUT_Vehicle Class

using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Script.Serialization;

namespace Rest_Put_Vehicle_Example
{
public class PUT_Vehicle
{
private Vehicle _vehicle { get; set; }

public void PutVehicle(/* token object or auth string passed here */, string putVehicleUri, string appId)
{
// Construct authentication header for FIM
string authHeader = string.Format("Atmosphere atmosphere_app_id={0}, Bearer {1}", appId, /* token authentication string goes here */);

// Serialize vehicle object into JSON for PUT body
string vehicleContent = new JavaScriptSerializer().Serialize(_vehicle);

// Create the body, pass in serialized data and set content type
string bodyContent = new StringContent(vehicleContent, Encoding.UTF8, "application/json");

// Begin new HttpClient. Use "using" to ensure the resource is released
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authHeader); // Pass in FIM auth header

// Pass in URI and append the vehicle number, supply bodyContent, and execute REST PUT
using(HttpResponseMessage response = client.PutAsync(putVehicleUri + _vehicle.VehicleNumber, bodyContent).Result)
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); // Logging

// True if HttpStatusCode was in the successful range (200-299); otherwise false
if (response.IsSuccessStatusCode)
{
// Clear vehicle object and return
_vehicle = null;
return;
}

// If this is reached, the call has failed
switch (response.StatusCode)
{
case HttpStatusCode.Unauthorized:
// Optional token renewal here, see Rest_Token_Example
break;
/*
... Other cases here
*/
default:
// Take another action
break;
}
}
}
}

// Public method used to create the vehicle object which will be used in the body for the PUT command
public void CreateNewPutVehicle(string name, string number, string registrationNumber, string vin, string make, string model,
int? year, double? tankCapacity, double? highwayMpg, double? cityMpg, int? vehicleSize)
{
_vehicle = new Vehicle
{
Name = name,
VehicleNumber = number,
RegistrationNumber = registrationNumber,
VIN = vin,
Make = make,
Model = model,
Year = year,
TankCapacity = tankCapacity,
HighwayMPG = highwayMpg,
CityMPG = cityMpg,
VehicleSize = vehicleSize
};
}
}

public class Vehicle
{
public string VehicleNumber { get; set; }
public string Name { get; set; }
public string RegistrationNumber { get; set; }
public string VIN { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int? Year { get; set; }
public double? TankCapacity { get; set; }
public double? HighwayMPG { get; set; }
public double? CityMPG { get; set; }
public int? VehicleSize { get; set; }
}
}

Downloadable source code for this example is available as a Microsoft Visual Studio solution in a .zip file.
To download source code for this example, click HERE.

DISCLAIMER

All sample code is provided by Fleetmatics, A Verizon Company, for illustrative purposes only. These examples have not been thoroughly tested under all conditions. Fleetmatics, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All programs contained herein are provided to you “AS IS” without any warranties of any kind.