Skip to content

Commit

Permalink
Added deserialize from stream support
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldwight committed Jan 29, 2021
1 parent 9c58bd5 commit dbb2087
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions MpesaSdk/MpesaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Polly;
using Polly.Extensions.Http;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -25,6 +26,7 @@ public class MpesaClient : IMpesaClient
private static readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
private static CancellationToken _token = new CancellationToken();
Random jitterer = new Random();
private JsonSerializer _serializer = new JsonSerializer();

/// <summary>
/// MpesaClient that creates a client using httpclientfactory
Expand Down Expand Up @@ -369,18 +371,22 @@ public async Task<MpesaResponse> ReverseMpesaTransactionAsync(MpesaReversal mpes

if (response.IsSuccessStatusCode)
{
await response.Content.ReadAsStringAsync().ContinueWith((Task<string> x) =>
await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
{
result = JsonConvert.DeserializeObject<T>(x.Result);
}, _token);
using var reader = new StreamReader(stream.Result);
using var json = new JsonTextReader(reader);
result = _serializer.Deserialize<T>(json);
}, _tokenSource.Token);
}
else
{
MpesaErrorResponse mpesaErrorResponse = new MpesaErrorResponse();
await response.Content.ReadAsStringAsync().ContinueWith((Task<string> x) =>
await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
{
mpesaErrorResponse = JsonConvert.DeserializeObject<MpesaErrorResponse>(x.Result);
}, _token);
using var reader = new StreamReader(stream.Result);
using var json = new JsonTextReader(reader);
mpesaErrorResponse = _serializer.Deserialize<MpesaErrorResponse>(json);
}, _tokenSource.Token);
throw new MpesaAPIException(new HttpRequestException(mpesaErrorResponse.ErrorMessage), response.StatusCode, mpesaErrorResponse);
}
return result;
Expand All @@ -402,18 +408,22 @@ private async Task<string> RequestAccessTokenAsync(string consumerKey, string co
var response = await _client.GetAsync(mpesaRequestEndpoint, _token).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
await response.Content.ReadAsStringAsync().ContinueWith((Task<string> x) =>
await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
{
result = JsonConvert.DeserializeObject<MpesaAccessTokenResponse>(x.Result);
}, _token);
using var reader = new StreamReader(stream.Result);
using var json = new JsonTextReader(reader);
result = _serializer.Deserialize<MpesaAccessTokenResponse>(json);
}, _tokenSource.Token);
}
else
{
MpesaErrorResponse mpesaErrorResponse = new MpesaErrorResponse();
await response.Content.ReadAsStringAsync().ContinueWith((Task<string> x) =>
await response.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
{
mpesaErrorResponse = JsonConvert.DeserializeObject<MpesaErrorResponse>(x.Result);
}, _token);
using var reader = new StreamReader(stream.Result);
using var json = new JsonTextReader(reader);
mpesaErrorResponse = _serializer.Deserialize<MpesaErrorResponse>(json);
}, _tokenSource.Token);
throw new MpesaAPIException(new HttpRequestException(mpesaErrorResponse.ErrorMessage), response.StatusCode, mpesaErrorResponse);
}
return result.AccessToken;
Expand Down

0 comments on commit dbb2087

Please sign in to comment.