Skip to content

Commit 37887fd

Browse files
Merge pull request #417 from nofrixion/MOOV-3586-Load-Tribe-Account-When-Payin-Complete
Support for receiving data in case of non-success response
2 parents e197c18 + 46c3539 commit 37887fd

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/NoFrixion.MoneyMoov/RestClient/RestApiClient.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public class RestApiClient : IRestApiClient, IDisposable
6161
private bool _disposed;
6262

6363
public HttpClient HttpClient { get; set; }
64+
65+
private readonly bool _dataExpectedOnErrorResponse;
6466

6567
public RestApiClient(string baseUri)
6668
{
@@ -77,6 +79,12 @@ public RestApiClient(IHttpClientFactory httpClientFactory, string httpClientName
7779
{
7880
HttpClient = httpClientFactory.CreateClient(httpClientName);
7981
}
82+
83+
public RestApiClient(HttpClient httpClient, bool dataExpectedOnErrorResponse)
84+
{
85+
HttpClient = httpClient;
86+
_dataExpectedOnErrorResponse = dataExpectedOnErrorResponse;
87+
}
8088

8189
public Uri GetBaseUri()
8290
=> HttpClient.BaseAddress ?? new Uri(MoneyMoovUrlBuilder.DEFAULT_MONEYMOOV_BASE_URL);
@@ -200,8 +208,18 @@ private async Task<RestApiResponse<T>> ToApiResponse<T>(HttpResponseMessage resp
200208
}
201209
else if (response.Content.Headers.ContentLength > 0)
202210
{
203-
string contentStr = await response.Content.ReadAsStringAsync();
211+
var contentStr = await response.Content.ReadAsStringAsync();
204212
var problem = DeserialiseProblem(response.StatusCode, contentStr);
213+
214+
if (_dataExpectedOnErrorResponse)
215+
{
216+
var result = TryDeserialiseDataOnErrorResponse<T>(contentStr);
217+
218+
if (result != null)
219+
{
220+
return new RestApiResponse<T>(response.StatusCode, requestUri, response.Headers, problem, result);
221+
}
222+
}
205223
return new RestApiResponse<T>(response.StatusCode, requestUri, response.Headers, problem);
206224
}
207225
else
@@ -210,6 +228,18 @@ private async Task<RestApiResponse<T>> ToApiResponse<T>(HttpResponseMessage resp
210228
new NoFrixionProblem(response.StatusCode, "Response content was expected."));
211229
}
212230
}
231+
232+
private T? TryDeserialiseDataOnErrorResponse<T>(string contentStr)
233+
{
234+
try
235+
{
236+
return contentStr.FromJson<T>();
237+
}
238+
catch (Newtonsoft.Json.JsonException)
239+
{
240+
return default;
241+
}
242+
}
213243

214244
/// <summary>
215245
/// Attempts to deserialise a problem object from the contents of a failure response.

src/NoFrixion.MoneyMoov/RestClient/RestApiResponse.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ public RestApiResponse(HttpStatusCode statusCode, Uri? requestUri, NoFrixionProb
104104
public RestApiResponse(HttpStatusCode statusCode, Uri? requestUri, Option<HttpResponseHeaders> headers, NoFrixionProblem problem)
105105
: base(statusCode, requestUri, headers, problem)
106106
{ }
107+
108+
public RestApiResponse(HttpStatusCode statusCode, Uri? requestUri, Option<HttpResponseHeaders> headers,
109+
NoFrixionProblem problem, T data)
110+
: base(statusCode, requestUri, headers, problem)
111+
{
112+
Data = data;
113+
}
107114
}
108115

109116
/// <summary>

0 commit comments

Comments
 (0)