Skip to content

Commit

Permalink
Added extension method to handle exception message from the API in a …
Browse files Browse the repository at this point in the history
…proper format
  • Loading branch information
jensnylander committed Feb 4, 2017
1 parent c5112b3 commit f077147
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 58 deletions.
4 changes: 3 additions & 1 deletion Automile.Net.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public void TestGetTrips()
[TestMethod]
public void TestGetTrip()
{
TripDetailModel trip = client.GetTripById(31826384);
// TripDetailModel trip = client.GetTripById(31565475);
TripDetailModel trip = client.GetTripById(31826384);

Assert.IsNotNull(trip);
}

Expand Down
1 change: 1 addition & 0 deletions Automile.Net/Automile.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<Compile Include="Client\Contacts.cs" />
<Compile Include="Client\Devices.cs" />
<Compile Include="Client\Extensions.cs" />
<Compile Include="Client\Places.cs" />
<Compile Include="Client\Geofence.cs" />
<Compile Include="Client\Notifications.cs" />
Expand Down
6 changes: 3 additions & 3 deletions Automile.Net/Client/Contacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class AutomileClient
public IEnumerable<Contact2Model> GetContacts()
{
var response = client.GetAsync("/v1/resourceowner/contacts2").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<List<Contact2Model>>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -28,7 +28,7 @@ public IEnumerable<Contact2Model> GetContacts()
public Contact2DetailModel GetContactById(int contactId)
{
var response = client.GetAsync($"/v1/resourceowner/contacts2/{contactId}").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<Contact2DetailModel>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -40,7 +40,7 @@ public Contact2DetailModel GetContactById(int contactId)
public Contact2DetailModel GetMe()
{
var response = client.GetAsync("/v1/resourceowner/contacts2/me").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<Contact2DetailModel>(response.Content.ReadAsStringAsync().Result);
}

Expand Down
12 changes: 6 additions & 6 deletions Automile.Net/Client/Devices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class AutomileClient
public IEnumerable<IMEIConfigModel> GetDevices()
{
var response = client.GetAsync("/v1/resourceowner/imeiconfigs").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<List<IMEIConfigModel>>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -29,7 +29,7 @@ public IEnumerable<IMEIConfigModel> GetDevices()
public IMEIConfigDetailModel GetDeviceById(int deviceId)
{
var response = client.GetAsync($"/v1/resourceowner/imeiconfigs/{deviceId}").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<IMEIConfigDetailModel>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -43,10 +43,10 @@ public IMEIConfigDetailModel CreateDevice(IMEIConfigCreateModel model)
string stringPayload = JsonConvert.SerializeObject(model);
var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = client.PostAsync("/v1/resourceowner/imeiconfigs", content).Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
var urlToCreatedDevice = response.Headers.GetValues("Location").First();
var deviceModel = client.GetAsync(urlToCreatedDevice).Result;
deviceModel.EnsureSuccessStatusCode();
deviceModel.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<IMEIConfigDetailModel>(deviceModel.Content.ReadAsStringAsync().Result);
}

Expand All @@ -60,7 +60,7 @@ public void EditDevice(int deviceId, IMEIConfigEditModel model)
string stringPayload = JsonConvert.SerializeObject(model);
var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = client.PutAsync($"/v1/resourceowner/imeiconfigs/{deviceId}", content).Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
}

/// <summary>
Expand All @@ -70,7 +70,7 @@ public void EditDevice(int deviceId, IMEIConfigEditModel model)
public void DeleteDevice(int deviceId)
{
var response = client.DeleteAsync($"/v1/resourceowner/imeiconfigs/{deviceId}").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
}

}
Expand Down
23 changes: 23 additions & 0 deletions Automile.Net/Client/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Automile.Net
{
public static class Extensions
{
public static void EnsureSuccessStatusCodeWithProperExceptionMessage(this HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
var code = response.StatusCode;
var phrase = response.ReasonPhrase;
string msg = response.Content.ReadAsStringAsync().Result;
throw new Exception($"Code: {code} Phrase: {phrase} Message: {msg}");
}
}
}
}
12 changes: 6 additions & 6 deletions Automile.Net/Client/Geofence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class AutomileClient
public IEnumerable<GeofenceModel> GetGeofences()
{
var response = client.GetAsync("/v1/resourceowner/geofence").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<List<GeofenceModel>>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -29,7 +29,7 @@ public IEnumerable<GeofenceModel> GetGeofences()
public GeofenceModel GetGeofenceById(int geofenceId)
{
var response = client.GetAsync($"/v1/resourceowner/geofence/{geofenceId}").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<GeofenceModel>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -43,10 +43,10 @@ public GeofenceModel CreateGeofence(GeofenceCreateModel model)
string stringPayload = JsonConvert.SerializeObject(model);
var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = client.PostAsync("/v1/resourceowner/geofence", content).Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
var urlToCreatedGeofence = response.Headers.GetValues("Location").First();
var geofenceModelResponse = client.GetAsync(urlToCreatedGeofence).Result;
geofenceModelResponse.EnsureSuccessStatusCode();
geofenceModelResponse.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<GeofenceModel>(geofenceModelResponse.Content.ReadAsStringAsync().Result);
}

Expand All @@ -60,7 +60,7 @@ public void EditGeofence(int geofenceId, GeofenceEditModel model)
string stringPayload = JsonConvert.SerializeObject(model);
var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = client.PutAsync($"/v1/resourceowner/geofence/{geofenceId}", content).Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
}

/// <summary>
Expand All @@ -70,7 +70,7 @@ public void EditGeofence(int geofenceId, GeofenceEditModel model)
public void DeleteGeofence(int geofenceId)
{
var response = client.DeleteAsync($"/v1/resourceowner/geofence/{geofenceId}").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
}

}
Expand Down
6 changes: 3 additions & 3 deletions Automile.Net/Client/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static SignUpResponseModel SignUp(string email)
string stringPayload = JsonConvert.SerializeObject(requestModel);
var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = signupClient.PostAsync($"/signup", content).Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<SignUpResponseModel>(response.Content.ReadAsStringAsync().Result);
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public AutomileClient(string username, string password, string apiClient, string
var content = new FormUrlEncodedContent(formUrlContent);
var response = client.PostAsync("OAuth2/Token/", content).Result;

response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();

TokenPair = JsonConvert.DeserializeObject<TokenPair>(response.Content.ReadAsStringAsync().Result);
TokenPair.Expires = DateTime.UtcNow.AddSeconds(TokenPair.ExpiresIn);
Expand Down Expand Up @@ -181,7 +181,7 @@ private void RefreshAccessToken()
var content = new FormUrlEncodedContent(formUrlContent);
var response = client.PostAsync("OAuth2/Token/", content).Result;

response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();

TokenPair = JsonConvert.DeserializeObject<TokenPair>(response.Content.ReadAsStringAsync().Result);
TokenPair.Expires = DateTime.UtcNow.AddSeconds(TokenPair.ExpiresIn);
Expand Down
12 changes: 6 additions & 6 deletions Automile.Net/Client/Places.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class AutomileClient
public IEnumerable<PlaceModel> GetPlaces()
{
var response = client.GetAsync("/v1/resourceowner/place").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<List<PlaceModel>>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -29,7 +29,7 @@ public IEnumerable<PlaceModel> GetPlaces()
public PlaceModel GetPlaceById(int placeId)
{
var response = client.GetAsync($"/v1/resourceowner/place/{placeId}").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<PlaceModel>(response.Content.ReadAsStringAsync().Result);
}

Expand All @@ -43,10 +43,10 @@ public PlaceModel CreatePlace(PlaceCreateModel model)
string stringPayload = JsonConvert.SerializeObject(model);
var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = client.PostAsync("/v1/resourceowner/place", content).Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
var urlToCreatedPlace = response.Headers.GetValues("Location").First();
var placeModelResult = client.GetAsync(urlToCreatedPlace).Result;
placeModelResult.EnsureSuccessStatusCode();
placeModelResult.EnsureSuccessStatusCodeWithProperExceptionMessage();
return JsonConvert.DeserializeObject<PlaceModel>(placeModelResult.Content.ReadAsStringAsync().Result);
}

Expand All @@ -60,7 +60,7 @@ public void EditPlace(int placeId, PlaceEditModel model)
string stringPayload = JsonConvert.SerializeObject(model);
var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = client.PutAsync($"/v1/resourceowner/place/{placeId}", content).Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
}

/// <summary>
Expand All @@ -70,7 +70,7 @@ public void EditPlace(int placeId, PlaceEditModel model)
public void DeletePlace(int placeId)
{
var response = client.DeleteAsync($"/v1/resourceowner/place/{placeId}").Result;
response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCodeWithProperExceptionMessage();
}


Expand Down
Loading

0 comments on commit f077147

Please sign in to comment.