Skip to content

Commit

Permalink
fix: fixes issue with concurrent requests causing malformed responses
Browse files Browse the repository at this point in the history
  • Loading branch information
aknous committed Apr 26, 2024
1 parent 372760d commit beca9c4
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 58 deletions.
61 changes: 46 additions & 15 deletions src/EpiphanPearl/EpiphanPearlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using PepperDash.Core;
using PepperDash.Essentials.EpiphanPearl.Interfaces;
using PepperDash.Essentials.EpiphanPearl.Utilities;
using System.Text;

namespace PepperDash.Essentials.EpiphanPearl
{
Expand All @@ -31,23 +32,25 @@ public T Get<T>(string path) where T:class

var response = SendRequest(request);

if (string.IsNullOrEmpty(response))
if (response == null || response.Length <= 0)
{
Debug.Console(2, "[T Get<T>] Response to {0} is null", request.Url, response);
return null;
}

try
{
Debug.Console(2, "[T Get<T>] Response to {0}: {1}", request.Url, response);
return JsonConvert.DeserializeObject<T>(response);
}
catch (Exception ex)
{
Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(0, "[T Get<T>] Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(2, "Stack Trace: {0}", ex.StackTrace);

if (ex.InnerException == null) return null;

Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.InnerException.Message);
Debug.Console(0, "[T Get<T>] Exception sending to {0}: {1}", request.Url, ex.InnerException.Message);
Debug.Console(2, "Stack Trace: {0}", ex.InnerException.StackTrace);

return null;
Expand All @@ -65,7 +68,7 @@ public TResponse Post<TBody, TResponse> (string path, TBody body) where TBody: c

var response = SendRequest(request);

if (string.IsNullOrEmpty(response))
if (response == null)
{
return null;
}
Expand All @@ -76,12 +79,12 @@ public TResponse Post<TBody, TResponse> (string path, TBody body) where TBody: c
}
catch (Exception ex)
{
Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(0, "[TResponse Post<TBody, TResponse>] Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(2, "Stack Trace: {0}", ex.StackTrace);

if (ex.InnerException == null) return null;

Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.InnerException.Message);
Debug.Console(0, "[TResponse Post<TBody, TResponse>] Exception sending to {0}: {1}", request.Url, ex.InnerException.Message);
Debug.Console(2, "Stack Trace: {0}", ex.InnerException.StackTrace);

return null;
Expand All @@ -97,7 +100,7 @@ public TResponse Post<TResponse>(string path)

var response = SendRequest(request);

if (string.IsNullOrEmpty(response))
if (response == null)
{
return null;
}
Expand All @@ -108,12 +111,12 @@ public TResponse Post<TResponse>(string path)
}
catch (Exception ex)
{
Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(0, "[TResponse Post<TResponse>] Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(2, "Stack Trace: {0}", ex.StackTrace);

if (ex.InnerException == null) return null;

Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.InnerException.Message);
Debug.Console(0, "[TResponse Post<TResponse>] Exception sending to {0}: {1}", request.Url, ex.InnerException.Message);
Debug.Console(2, "Stack Trace: {0}", ex.InnerException.StackTrace);

return null;
Expand All @@ -134,24 +137,52 @@ public void setHost(string host)

private string SendRequest(HttpClientRequest request)
{
if (request == null)
{
Debug.Console(2, "[SendRequest] Request is null");
return null;
}

if (_client == null)
{
Debug.Console(2, "[SendRequest] HttpClient is null");
return null;
}

try
{
Debug.Console(2, "[SendRequest] Dispatching request to {0}", request.Url); // Log before dispatch
var response = _client.Dispatch(request);

Debug.Console(2, "Response from request to {0}: {1} {2}", request.Url, response.Code,
response.ContentString);
if (response == null)
{
Debug.Console(2, "[SendRequest] Response is null after dispatching request to {0}", request.Url);
return null;
}

return response.ContentString;
//Debug.Console(0, "Raw response bytes: {0}", BitConverter.ToString(response.ContentBytes));

try
{
// Attempt to parse the response content as a string
var contentString = response.ContentString;
return contentString;
}
catch (Exception ex)
{
Debug.Console(2, "[SendRequest] Error converting response to string for URL {0}: {1}", request.Url, ex.Message);
return null;
}
}
catch (Exception ex)
{
Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(0, "[SendRequest] Exception sending to {0}: {1}", request.Url, ex.Message);
Debug.Console(2, "Stack Trace: {0}", ex.StackTrace);

if (ex.InnerException != null)
{
Debug.Console(0, "Exception sending to {0}: {1}", request.Url, ex.InnerException.Message);
Debug.Console(2, "Stack Trace: {0}", ex.InnerException.StackTrace);
Debug.Console(0, "[SendRequest] Inner Exception: {1}", request.Url, ex.InnerException.Message);
Debug.Console(2, "Inner Stack Trace: {0}", ex.InnerException.StackTrace);
}

return null;
Expand Down
Loading

0 comments on commit beca9c4

Please sign in to comment.