Only deserialize response payload in ApiRequest when StatusCode == 200. Will a default return value in other cases cause other issues?

pull/1199/head
smcpeck 8 years ago
parent a5651775a6
commit 231a525109

@ -56,9 +56,8 @@ namespace Ombi.Api
public T Execute<T>(IRestRequest request, Uri baseUri) where T : new() public T Execute<T>(IRestRequest request, Uri baseUri) where T : new()
{ {
var client = new RestClient { BaseUrl = baseUri }; var client = new RestClient { BaseUrl = baseUri };
var response = client.Execute<T>(request); var response = client.Execute<T>(request);
Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}");
if (response.ErrorException != null) if (response.ErrorException != null)
{ {
@ -67,15 +66,16 @@ namespace Ombi.Api
throw new ApiRequestException(message, response.ErrorException); throw new ApiRequestException(message, response.ErrorException);
} }
return response.Data; if (response.StatusCode != HttpStatusCode.OK)
return default(T);
else
return response.Data;
} }
public IRestResponse Execute(IRestRequest request, Uri baseUri) public IRestResponse Execute(IRestRequest request, Uri baseUri)
{ {
var client = new RestClient { BaseUrl = baseUri }; var client = new RestClient { BaseUrl = baseUri };
var response = client.Execute(request); var response = client.Execute(request);
return response; return response;
} }
@ -83,7 +83,7 @@ namespace Ombi.Api
{ {
var client = new RestClient { BaseUrl = baseUri }; var client = new RestClient { BaseUrl = baseUri };
var response = client.Execute(request); var response = client.Execute(request);
Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}");
if (response.ErrorException != null) if (response.ErrorException != null)
{ {
@ -92,7 +92,9 @@ namespace Ombi.Api
throw new ApiRequestException(message, response.ErrorException); throw new ApiRequestException(message, response.ErrorException);
} }
var result = DeserializeXml<T>(response.Content); T result = default(T);
if (response.StatusCode == HttpStatusCode.OK)
result = DeserializeXml<T>(response.Content);
return result; return result;
} }
@ -100,7 +102,8 @@ namespace Ombi.Api
{ {
var client = new RestClient { BaseUrl = baseUri }; var client = new RestClient { BaseUrl = baseUri };
var response = client.Execute(request); var response = client.Execute(request);
Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}");
if (response.ErrorException != null) if (response.ErrorException != null)
{ {
Log.Error(response.ErrorException); Log.Error(response.ErrorException);
@ -108,8 +111,10 @@ namespace Ombi.Api
throw new ApiRequestException(message, response.ErrorException); throw new ApiRequestException(message, response.ErrorException);
} }
var json = JsonConvert.DeserializeObject<T>(response.Content, _settings); T result = default(T);
return json; if (response.StatusCode == HttpStatusCode.OK)
result = JsonConvert.DeserializeObject<T>(response.Content, _settings);
return result;
} }
private T DeserializeXml<T>(string input) private T DeserializeXml<T>(string input)

@ -79,7 +79,7 @@ namespace Ombi.Core
request.Approved = true; request.Approved = true;
// Update the record // Update the record
var updated = RequestService.UpdateRequest(request); bool updated = RequestService.UpdateRequest(request);
return updated; return updated;
} }
@ -90,7 +90,7 @@ namespace Ombi.Core
var artistExists = index.Any(x => x.ArtistID == request.ArtistId); var artistExists = index.Any(x => x.ArtistID == request.ArtistId);
if (!artistExists) if (!artistExists)
{ {
var artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); bool artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId);
Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName);
} }

Loading…
Cancel
Save