I have a .net core 3.1 api project and I need to get a single vehicle back by using the id. I have made the models to match the data from the Api.
This is what I have so far...
public class SamsaraGpsVehicleInfo
{
[JsonPropertyName("externalIds")]
public ExternalIds ExternalIds { get; set; }
[JsonPropertyName("gateway")]
public Gateway Gateway { get; set; }
[JsonPropertyName("harshAccelerationSettingType")]
public string HarshAccelerationSettingType { get; set; }
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("licensePlate")]
public string LicensePlate { get; set; }
[JsonPropertyName("make")]
public string Make { get; set; }
[JsonPropertyName("model")]
public string Model { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("notes")]
public string Notes { get; set; }
[JsonPropertyName("serial")]
public string Serial { get; set; }
[JsonPropertyName("vin")]
public string Vin { get; set; }
[JsonPropertyName("year")]
public string Year { get; set; }
[JsonPropertyName("vehicleRegulationMode")]
public string VehicleRegulationMode { get; set; }
[JsonPropertyName("createdAtTime")]
public DateTime CreatedAtTime { get; set; }
[JsonPropertyName("updatedAtTime")]
public DateTime UpdatedAtTime { get; set; }
}
public class ExternalIds
{
[JsonPropertyName("samsara.serial")]
public string SamsaraSerial { get; set; }
[JsonPropertyName("samsara.vin")]
public string SamsaraVin { get; set; }
}
public class Gateway
{
[JsonPropertyName("serial")]
public string Serial { get; set; }
[JsonPropertyName("model")]
public string Model { get; set; }
}
public class SamsaraGpsVehicleList
{
[JsonPropertyName("data")]
public ICollection<SamsaraGpsVehicleInfo> Data { get; set; }
}
Which I make a call with my client like so:
public async Task<ICollection<SamsaraGpsVehicleInfo>> GetVehicles()
{
try
{
var result = await _client.GetAsync("fleet/vehicles");
if (result.IsSuccessStatusCode)
{
var list = await result.Content.ReadAsAsync<SamsaraGpsVehicleList>();
return list.Data;
}
else
{
return null;
}
}
catch
{
return null;
}
}
This returns a nice formatted response when I call it from my controller code:
[HttpGet("samsara")]
public async Task<IActionResult> GetVehicles(
[FromServices] SamsaraGpsService samsaraGps)
{
var results = await samsaraGps.GetVehicles();
return Ok(results);
and my result is this JSON:
[
{
"externalIds": {
"samsaraSerial": null,
"samsaraVin": null
},
"gateway": {
"serial": "G2PV-5GN-98C",
"model": "VG54NA"
},
"harshAccelerationSettingType": "automatic",
"id": "28111111111117",
"licensePlate": "UOG 18",
"make": "CHRYSLER",
"model": "Pacifica",
"name": "UOG 18",
"notes": "",
"serial": "G2123123C",
"vin": "2C4123213213515",
"year": "2019",
"vehicleRegulationMode": "regulated",
"createdAtTime": "2021-10-13T17:10:56Z",
"updatedAtTime": "2021-10-13T17:10:56Z"
},
{
"externalIds": {
"samsaraSerial": null,
"samsaraVin": null
},
"gateway": {
"serial": "G111-111-11M",
"model": "V1111A"
},
"harshAccelerationSettingType": "automatic",
"id": "2811111111118",
"licensePlate": "UOG 25",
"make": "CHRYSLER",
"model": "Pacifica",
"name": "UOG 25",
"notes": "",
"serial": "GJJJJJJPM",
"vin": "211111111122",
"year": "2019",
"vehicleRegulationMode": "regulated",
"createdAtTime": "2021-10-13T17:10:56Z",
"updatedAtTime": "2022-08-08T20:55:04Z"
},
{
"externalIds": {
"samsaraSerial": null,
"samsaraVin": null
},
"gateway": {
"serial": "GXGB-111-111",
"model": "VG54NA"
},
"harshAccelerationSettingType": "automatic",
"id": "2819873793099",
"licensePlate": "TEST",
"make": "CHRYSLER",
"model": "Town and Country",
"name": "New Van",
"notes": "",
"serial": "GHGHGHGHGH",
"vin": "2C4G4G4G1",
"year": "2014",
"vehicleRegulationMode": "regulated",
"createdAtTime": "2021-10-13T17:10:56Z",
"updatedAtTime": "2021-10-13T17:10:56Z"
}
]
which is perfect.
However I need to get a single vehicle. So I have this code:
public async Task<SamsaraVehicle> GetVehicleById(string id)
{
try
{
var result = await _client.GetAsync($"fleet/vehicles/{id}");
if (result.IsSuccessStatusCode)
{
var vehicle = await result.Content.ReadAsAsync<SamsaraVehicle>();
return vehicle;
}
else
{
return null;
}
}
catch
{
return null;
}
}
and this model for the vehicle:
public class SamsaraVehicle
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("vin")]
public string Vin { get; set; }
[JsonPropertyName("serial")]
public string Serial { get; set; }
[JsonPropertyName("make")]
public string Make { get; set; }
[JsonPropertyName("model")]
public string Model { get; set; }
[JsonPropertyName("year")]
public string Year { get; set; }
[JsonPropertyName("harshAccelerationSettingType")]
public string HarshAccelerationSettingType { get; set; }
[JsonPropertyName("notes")]
public string Notes { get; set; }
[JsonPropertyName("licensePlate")]
public string LicensePlate { get; set; }
[JsonPropertyName("externalIds")]
public ExternalIds ExternalIds { get; set; }
[JsonPropertyName("gateway")]
public Gateway Gateway { get; set; }
[JsonPropertyName("vehicleRegulationMode")]
public string VehicleRegulationMode { get; set; }
}
public class SamsaraSingleVehicle
{
[JsonPropertyName("data")]
public ICollection<SamsaraVehicle> Data { get; set; }
}
However I can not figure out how to get this vehicle back by id. If anyone could help I would greatly appreciate it!
CodePudding user response:
I recommand you modify your codes:
var vehicle = await result.Content.ReadAsAsync<SamsaraVehicle>();
to
var jsonstring = await result.Content.ReadAsStringAsync();
var obj = JsonSerializer.Deserialize<SamsaraVehicle>(jsonstring);
When you debug, you would check the jsonstring :
then you could modify the propery of the model accroding to the string