Skip to content

Commit

Permalink
added logging to httpClient Service, optional parameter support in Pu…
Browse files Browse the repository at this point in the history
…bnub constructor for middleware and client library
  • Loading branch information
mohitpubnub committed Oct 30, 2024
1 parent 0055eda commit 5bef325
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Api/PubnubApi/Pubnub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ public void SetJsonPluggableLibrary(IJsonPluggableLibrary customJson)
#endregion

#region "Constructors"
public Pubnub(PNConfiguration config)
public Pubnub(PNConfiguration config, IHttpClientService httpTransportService = default, ITransportMiddleware middleware = default)
{
if (config == null)
{
Expand Down Expand Up @@ -995,8 +995,8 @@ public Pubnub(PNConfiguration config)
CheckRequiredUserId(config);
eventEmitter = new EventEmitter(pubnubConfig.ContainsKey(InstanceId) ? pubnubConfig[InstanceId] : null, subscribeCallbackListenerList, JsonPluggableLibrary, tokenManager, pubnubLog, this);
CheckCryptoModuleUsageForLogging(config);
IHttpClientService httpClientService = new HttpClientService(config.Proxy);
transportMiddleware = new Middleware(httpClientService,config, this, tokenManager);
IHttpClientService httpClientService = httpTransportService ?? new HttpClientService(proxy:config.Proxy, pubnubLog: config.PubnubLog, verbosity: config.LogVerbosity);
transportMiddleware = middleware ?? new Middleware(httpClientService,config, this, tokenManager);
}

#if UNITY
Expand Down
20 changes: 15 additions & 5 deletions src/Api/PubnubApi/Transport/HttpClientService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;
using System.Linq;
Expand All @@ -11,7 +12,9 @@ namespace PubnubApi
public class HttpClientService : IHttpClientService
{
private readonly HttpClient httpClient;
public HttpClientService(IWebProxy proxy = default)
private IPubnubLog Log { get; set; }
private PNLogVerbosity LogVerbosity { get; set; }
public HttpClientService(IWebProxy proxy = default, IPubnubLog pubnubLog = default, PNLogVerbosity verbosity = default)
{
httpClient = new HttpClient()
{
Expand All @@ -24,6 +27,8 @@ public HttpClientService(IWebProxy proxy = default)
UseProxy = true
});
httpClient.Timeout = Timeout.InfiniteTimeSpan;
Log = pubnubLog;
LogVerbosity = verbosity;
}

public async Task<TransportResponse> GetRequest(TransportRequest transportRequest)
Expand All @@ -44,10 +49,11 @@ public async Task<TransportResponse> GetRequest(TransportRequest transportReques
Headers = httpResult.Headers.ToDictionary(h => h.Key, h => h.Value),
RequestUrl = httpResult.RequestMessage?.RequestUri?.AbsolutePath
};
} catch (Exception ex) {
} catch (Exception e) {
LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity);
response = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = ex
Error = e
};
}
return response;
Expand Down Expand Up @@ -80,6 +86,7 @@ public async Task<TransportResponse> PostRequest(TransportRequest transportReque
RequestUrl = httpResult.RequestMessage?.RequestUri?.AbsolutePath
};
} catch (Exception e) {
LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity);
transportResponse = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = e
Expand Down Expand Up @@ -132,6 +139,7 @@ public async Task<TransportResponse> PutRequest(TransportRequest transportReques
}
catch (Exception e)
{
LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity);
transportResponse = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = e
Expand Down Expand Up @@ -159,10 +167,11 @@ public async Task<TransportResponse> DeleteRequest(TransportRequest transportReq
Headers = httpResult.Headers.ToDictionary(h => h.Key, h => h.Value),
RequestUrl = httpResult.RequestMessage?.RequestUri?.AbsolutePath
};
} catch (Exception ex) {
} catch (Exception e) {
LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity);
response = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = ex
Error = e
};
}
return response;
Expand Down Expand Up @@ -213,6 +222,7 @@ public async Task<TransportResponse> PatchRequest(TransportRequest transportRequ
}
catch (Exception e)
{
LoggingMethod.WriteToLog(Log, $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}", LogVerbosity);
transportResponse = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = e
Expand Down

0 comments on commit 5bef325

Please sign in to comment.