From cd5167869abf797bfbc4a8d93739b4dc94b5f269 Mon Sep 17 00:00:00 2001 From: "PUBNUB\\jakub.grzesiowski" Date: Mon, 20 Jan 2025 10:19:16 +0100 Subject: [PATCH] Add option to inject custom PNSDK source --- src/Api/PubnubApi/PNSDK/DotNetPNSDKSource.cs | 17 +++++++++++++++++ src/Api/PubnubApi/PNSDK/IPNSDKSource.cs | 6 ++++++ src/Api/PubnubApi/Pubnub.cs | 17 +++++------------ src/Api/PubnubApi/Transport/Middleware.cs | 2 +- src/Api/PubnubApiPCL/PubnubApiPCL.csproj | 6 ++++++ src/Api/PubnubApiUWP/PubnubApiUWP.csproj | 6 ++++++ src/Api/PubnubApiUnity/PubnubApiUnity.csproj | 6 ++++++ 7 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 src/Api/PubnubApi/PNSDK/DotNetPNSDKSource.cs create mode 100644 src/Api/PubnubApi/PNSDK/IPNSDKSource.cs diff --git a/src/Api/PubnubApi/PNSDK/DotNetPNSDKSource.cs b/src/Api/PubnubApi/PNSDK/DotNetPNSDKSource.cs new file mode 100644 index 000000000..957e8ba34 --- /dev/null +++ b/src/Api/PubnubApi/PNSDK/DotNetPNSDKSource.cs @@ -0,0 +1,17 @@ +using System.Globalization; +using System.Reflection; + +namespace PubnubApi.PNSDK; + +public class DotNetPNSDKSource : IPNSDKSource +{ + public string GetPNSDK() + { + var assembly = typeof(Pubnub).GetTypeInfo().Assembly; + var assemblyName = new AssemblyName(assembly.FullName); + string assemblyVersion = assemblyName.Version.ToString(); + var targetFramework = assembly.GetCustomAttribute()?.FrameworkDisplayName?.Replace(".",string.Empty).Replace(" ", string.Empty); + + return string.Format(CultureInfo.InvariantCulture, "{0}/CSharp/{1}", targetFramework??"UNKNOWN", assemblyVersion); + } +} \ No newline at end of file diff --git a/src/Api/PubnubApi/PNSDK/IPNSDKSource.cs b/src/Api/PubnubApi/PNSDK/IPNSDKSource.cs new file mode 100644 index 000000000..0a7f2dc64 --- /dev/null +++ b/src/Api/PubnubApi/PNSDK/IPNSDKSource.cs @@ -0,0 +1,6 @@ +namespace PubnubApi.PNSDK; + +public interface IPNSDKSource +{ + public string GetPNSDK(); +} \ No newline at end of file diff --git a/src/Api/PubnubApi/Pubnub.cs b/src/Api/PubnubApi/Pubnub.cs index 23fda5969..c433af6cc 100644 --- a/src/Api/PubnubApi/Pubnub.cs +++ b/src/Api/PubnubApi/Pubnub.cs @@ -12,6 +12,7 @@ using PubnubApi.Security.Crypto.Cryptors; using PubnubApi.EventEngine.Common; using System.Collections.Concurrent; +using PubnubApi.PNSDK; namespace PubnubApi { @@ -36,16 +37,6 @@ private List subscribeCallbackListenerList #if UNITY private static System.Func> OnCleanupCall; #endif - - static Pubnub() - { - var assembly = typeof(Pubnub).GetTypeInfo().Assembly; - var assemblyName = new AssemblyName(assembly.FullName); - string assemblyVersion = assemblyName.Version.ToString(); - var targetFramework = assembly.GetCustomAttribute()?.FrameworkDisplayName?.Replace(".",string.Empty).Replace(" ", string.Empty); - - Version = string.Format(CultureInfo.InvariantCulture, "{0}/CSharp/{1}", targetFramework??"UNKNOWN", assemblyVersion); - } #if UNITY /// @@ -942,7 +933,7 @@ public void SetJsonPluggableLibrary(IJsonPluggableLibrary customJson) JsonPluggableLibrary = customJson; } - public static string Version { get; private set; } + public string Version { get; private set; } internal readonly ITransportMiddleware transportMiddleware; @@ -958,7 +949,7 @@ public void SetJsonPluggableLibrary(IJsonPluggableLibrary customJson) #endregion #region "Constructors" - public Pubnub(PNConfiguration config, IHttpClientService httpTransportService = default, ITransportMiddleware middleware = default) + public Pubnub(PNConfiguration config, IHttpClientService httpTransportService = default, ITransportMiddleware middleware = default, IPNSDKSource ipnsdkSource = default) { if (config == null) { @@ -995,6 +986,8 @@ public Pubnub(PNConfiguration config, IHttpClientService httpTransportService = CheckRequiredUserId(config); eventEmitter = new EventEmitter(pubnubConfig.ContainsKey(InstanceId) ? pubnubConfig[InstanceId] : null, subscribeCallbackListenerList, JsonPluggableLibrary, tokenManager, pubnubLog, this); CheckCryptoModuleUsageForLogging(config); + //Defaulting to DotNet PNSDK source if no custom one is specified + Version = (ipnsdkSource == default) ? new DotNetPNSDKSource().GetPNSDK() : ipnsdkSource.GetPNSDK(); IHttpClientService httpClientService = httpTransportService ?? new HttpClientService(proxy:config.Proxy, pubnubLog: config.PubnubLog, verbosity: config.LogVerbosity); transportMiddleware = middleware ?? new Middleware(httpClientService,config, this, tokenManager); } diff --git a/src/Api/PubnubApi/Transport/Middleware.cs b/src/Api/PubnubApi/Transport/Middleware.cs index 1535454d1..2a282ad7b 100644 --- a/src/Api/PubnubApi/Transport/Middleware.cs +++ b/src/Api/PubnubApi/Transport/Middleware.cs @@ -34,7 +34,7 @@ public TransportRequest PreapareTransportRequest(RequestParameter requestParamet Dictionary commonQueryParameters = new Dictionary { { "uuid",UriUtil.EncodeUriComponent(configuration.UserId.ToString(),PNOperationType.PNSubscribeOperation, false, false, true)}, - { "pnsdk", UriUtil.EncodeUriComponent(Pubnub.Version, PNOperationType.PNSubscribeOperation, false, false, true) } + { "pnsdk", UriUtil.EncodeUriComponent(pnInstance.Version, PNOperationType.PNSubscribeOperation, false, false, true) } }; if (configuration.IncludeInstanceIdentifier) diff --git a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj index a5c2af288..f7f5dd747 100644 --- a/src/Api/PubnubApiPCL/PubnubApiPCL.csproj +++ b/src/Api/PubnubApiPCL/PubnubApiPCL.csproj @@ -567,6 +567,12 @@ Added support for Type field in membership APIs. PNConfiguration.cs + + PNSDK\DotNetPNSDKSource.cs + + + PNSDK\IPNSDKSource.cs + Proxy\PubnubProxy.cs diff --git a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj index 105fef7f7..91ad3c097 100644 --- a/src/Api/PubnubApiUWP/PubnubApiUWP.csproj +++ b/src/Api/PubnubApiUWP/PubnubApiUWP.csproj @@ -618,6 +618,12 @@ Added support for Type field in membership APIs. + + PNSDK\DotNetPNSDKSource.cs + + + PNSDK\IPNSDKSource.cs + diff --git a/src/Api/PubnubApiUnity/PubnubApiUnity.csproj b/src/Api/PubnubApiUnity/PubnubApiUnity.csproj index ed0d66ae7..c6e8f488d 100644 --- a/src/Api/PubnubApiUnity/PubnubApiUnity.csproj +++ b/src/Api/PubnubApiUnity/PubnubApiUnity.csproj @@ -225,6 +225,12 @@ JsonDataParse\DeserializeToInternalObjectUtility.cs + + + PNSDK\DotNetPNSDKSource.cs + + + PNSDK\IPNSDKSource.cs RetryConfiguration.cs