Skip to content
This repository has been archived by the owner on Jul 11, 2021. It is now read-only.

Commit

Permalink
Using ReadAsStreamAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
ayiemba committed Mar 18, 2019
1 parent 5d6af61 commit 053f4fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
30 changes: 21 additions & 9 deletions src/MpesaLib/MpesaClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MpesaLib.Helpers.Exceptions;
using MpesaLib.Helpers.Serialization;
using MpesaLib.Responses;
using Newtonsoft.Json;
using System;
Expand Down Expand Up @@ -360,21 +361,27 @@ private async Task<string> RequestAccessToken(string consumerKey, string consume
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestEndPoint);

request.Headers.Authorization = new AuthenticationHeaderValue("Basic", keyBytes);


cancellationToken.ThrowIfCancellationRequested();

var response = await _httpclient.SendAsync(request, cancellationToken);

var content = await response.Content.ReadAsStringAsync();
var content = await response.Content.ReadAsStreamAsync();

var data = JSONStreamHelper.DeserializeFromStream(content);

var strData = JsonConvert.SerializeObject(data);

if (response.IsSuccessStatusCode == false)
{
throw new MpesaApiException
{
StatusCode = (int)response.StatusCode,
Content = content
};
Content = strData
};
}

return JsonConvert.DeserializeObject<TokenResponse>(content).AccessToken;
return JsonConvert.DeserializeObject<TokenResponse>(strData).AccessToken;
}

/// <summary>
Expand All @@ -396,20 +403,25 @@ private async Task<string> MpesaHttpRequest(object Dto,string token, string Endp

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

HttpResponseMessage response = await _httpclient.SendAsync(request, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();

HttpResponseMessage response = await _httpclient.SendAsync(request, cancellationToken);
var content = await response.Content.ReadAsStreamAsync();

var data = JSONStreamHelper.DeserializeFromStream(content);

var content = await response.Content.ReadAsStringAsync();
var strData = JsonConvert.SerializeObject(data);

if (response.IsSuccessStatusCode == false)
{
throw new MpesaApiException
{
StatusCode = (int)response.StatusCode,
Content = content
Content = strData
};
}

return content;
return strData;
}


Expand Down
3 changes: 2 additions & 1 deletion src/MpesaLib/MpesaLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<PackageLicenseUrl>https://ayiemba.github.io/MpesaLib/articles/license.html</PackageLicenseUrl>
<PackageProjectUrl>https://ayiemba.github.io/MpesaLib</PackageProjectUrl>
<PackageReleaseNotes>
- Moved Headers from httclient object to the request object
- Moved Default Headers from HttClient object to the HttpRequestMessage object
- Using ReadAsStreamAsync instead of ReadAsStringAsync to avoid unnecessary allocations
</PackageReleaseNotes>
<IncludeBuildOutput>true</IncludeBuildOutput>
<Version>3.2.4</Version>
Expand Down

0 comments on commit 053f4fe

Please sign in to comment.