Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to inject custom PNSDK source #232

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

{
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 @@
<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 @@
<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 @@ -226,6 +226,12 @@
<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\JsonDataParse\EventDeserializer.cs" Link="JsonDataParse\EventDeserializer.cs" />
<Compile Include="..\PubnubApi\RetryConfiguration.cs">
<Link>RetryConfiguration.cs</Link>
Expand Down
Loading