Skip to content

Commit

Permalink
Add option to inject custom PNSDK source
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-grzesiowski committed Jan 20, 2025
1 parent 59ba055 commit cd51678
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/Api/PubnubApi/PNSDK/DotNetPNSDKSource.cs
Original file line number Diff line number Diff line change
@@ -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<System.Runtime.Versioning.TargetFrameworkAttribute>()?.FrameworkDisplayName?.Replace(".",string.Empty).Replace(" ", string.Empty);

return string.Format(CultureInfo.InvariantCulture, "{0}/CSharp/{1}", targetFramework??"UNKNOWN", assemblyVersion);
}
}
6 changes: 6 additions & 0 deletions src/Api/PubnubApi/PNSDK/IPNSDKSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PubnubApi.PNSDK;

public interface IPNSDKSource
{
public string GetPNSDK();
}
17 changes: 5 additions & 12 deletions src/Api/PubnubApi/Pubnub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using PubnubApi.Security.Crypto.Cryptors;
using PubnubApi.EventEngine.Common;
using System.Collections.Concurrent;
using PubnubApi.PNSDK;

namespace PubnubApi
{
Expand All @@ -36,16 +37,6 @@ private List<SubscribeCallback> subscribeCallbackListenerList
#if UNITY
private static System.Func<UnsubscribeAllOperation<object>> 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<System.Runtime.Versioning.TargetFrameworkAttribute>()?.FrameworkDisplayName?.Replace(".",string.Empty).Replace(" ", string.Empty);

Version = string.Format(CultureInfo.InvariantCulture, "{0}/CSharp/{1}", targetFramework??"UNKNOWN", assemblyVersion);
}

#if UNITY
/// <summary>
Expand Down Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Api/PubnubApi/Transport/Middleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public TransportRequest PreapareTransportRequest(RequestParameter requestParamet
Dictionary<string, string> commonQueryParameters = new Dictionary<string, string>
{
{ "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)
Expand Down
6 changes: 6 additions & 0 deletions src/Api/PubnubApiPCL/PubnubApiPCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ Added support for Type field in membership APIs.</PackageReleaseNotes>
<Compile Include="..\PubnubApi\PNConfiguration.cs">
<Link>PNConfiguration.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\PNSDK\DotNetPNSDKSource.cs">
<Link>PNSDK\DotNetPNSDKSource.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\PNSDK\IPNSDKSource.cs">
<Link>PNSDK\IPNSDKSource.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\Properties\AssemblyInfo.cs" Link="Properties\AssemblyInfo.cs" />
<Compile Include="..\PubnubApi\Proxy\PubnubProxy.cs">
<Link>Proxy\PubnubProxy.cs</Link>
Expand Down
6 changes: 6 additions & 0 deletions src/Api/PubnubApiUWP/PubnubApiUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,12 @@ Added support for Type field in membership APIs.</PackageReleaseNotes>
<Compile Include="..\PubnubApi\Model\Derived\Objects\PNRemoveUuidMetadataResultExt.cs" Link="Model\Derived\Objects\PNRemoveUuidMetadataResultExt.cs" />
<Compile Include="..\PubnubApi\Model\Derived\Objects\PNSetChannelMetadataResultExt.cs" Link="Model\Derived\Objects\PNSetChannelMetadataResultExt.cs" />
<Compile Include="..\PubnubApi\Model\Derived\Objects\PNSetUuidMetadataResultExt.cs" Link="Model\Derived\Objects\PNSetUuidMetadataResultExt.cs" />
<Compile Include="..\PubnubApi\PNSDK\DotNetPNSDKSource.cs">
<Link>PNSDK\DotNetPNSDKSource.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\PNSDK\IPNSDKSource.cs">
<Link>PNSDK\IPNSDKSource.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\Transport\HttpClientService.cs" Link="Transport\HttpClientService.cs" />
<Compile Include="..\PubnubApi\Transport\Middleware.cs" Link="Transport\Middleware.cs" />
<Compile Include="..\PubnubApi\Transport\RequestParameter.cs" Link="Transport\RequestParameter.cs" />
Expand Down
6 changes: 6 additions & 0 deletions src/Api/PubnubApiUnity/PubnubApiUnity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@
<Compile Include="..\PubnubApi\Interface\IUnsubscribeOperation.cs" Link="Interface\IUnsubscribeOperation.cs" />
<Compile Include="..\PubnubApi\JsonDataParse\DeserializeToInternalObjectUtility.cs">
<Link>JsonDataParse\DeserializeToInternalObjectUtility.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\PNSDK\DotNetPNSDKSource.cs">
<Link>PNSDK\DotNetPNSDKSource.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\PNSDK\IPNSDKSource.cs">
<Link>PNSDK\IPNSDKSource.cs</Link>
</Compile>
<Compile Include="..\PubnubApi\RetryConfiguration.cs">
<Link>RetryConfiguration.cs</Link>
Expand Down

0 comments on commit cd51678

Please sign in to comment.