Skip to content

Commit

Permalink
Updates 1.Missing appkey will not break app anymore 2. Local and remo…
Browse files Browse the repository at this point in the history
…te Ip Addresses included to package data
  • Loading branch information
kayhantolga committed Mar 31, 2022
1 parent 72c05e2 commit 80f50c1
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://portal.lasercateyes.com</PackageProjectUrl>
<PackageIcon>LaserCatEyes_Logo.png</PackageIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Betalgo Up Ltd.</Authors>
<Company>Betalgo Up Ltd.</Company>
<Product>Laser Cat Eyes</Product>
Expand All @@ -24,6 +24,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
</ItemGroup>

Expand All @@ -38,4 +39,4 @@
</None>
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using LaserCatEyes.Domain;
using LaserCatEyes.Domain.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace LaserCatEyes.DataServiceSdk.DotNetStandard
Expand All @@ -13,19 +14,21 @@ public class LaserCatEyesDataService : ILaserCatEyesDataService
{
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
});

private readonly Guid _deviceId;
private readonly LaserCatEyesOptions _laserCatEyesOptions;
private readonly LaserCatEyesSystemOptions _laserCatEyesSystemOptions;
private readonly bool _serviceReady;

public LaserCatEyesDataService(IOptions<LaserCatEyesOptions> laserCatEyesOptions, IOptions<LaserCatEyesSystemOptions> laserCatEyesSystemOptions)
public LaserCatEyesDataService(IOptions<LaserCatEyesOptions> laserCatEyesOptions, IOptions<LaserCatEyesSystemOptions> laserCatEyesSystemOptions, ILogger<LaserCatEyesDataService> logger)
{

_laserCatEyesOptions = laserCatEyesOptions.Value;
_laserCatEyesSystemOptions = laserCatEyesSystemOptions.Value;

if (string.IsNullOrEmpty(_laserCatEyesOptions.AppKey))
{
throw new Exception("LaserCatEyes AppKey can not be null!");
logger.LogWarning("LaserCatEyes AppKey is NULL!");
return;
}

var deviceName = $"{Environment.MachineName}:{Environment.UserName}";
Expand Down Expand Up @@ -61,18 +64,35 @@ public LaserCatEyesDataService(IOptions<LaserCatEyesOptions> laserCatEyesOptions
_deviceId = httpResponseMessage.Content.ReadAsAsync<SubAppUpdateResponseModel>().Result.DeviceId;

_client.DefaultRequestHeaders.Add(Constants.Headers.AlgoronaDeviceId, _deviceId.ToString());
_serviceReady = true;
}


public async Task<HttpResponseMessage> ReportTask(PackageData data)
{
if (!_serviceReady)
{
return null;
}

data.DeviceUuid = _laserCatEyesOptions.DeviceUuid;
data.DeviceId = _deviceId;
return await _client.PostAsJsonAsync(_laserCatEyesSystemOptions.Endpoints.DataSendPackage, data);
}


public bool IsServiceReady()
{
return _serviceReady;
}

public void Report(PackageData data)
{
if (!_serviceReady)
{
return;
}

Task.Run(() => ReportTask(data)).Forget();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://portal.lasercateyes.com</PackageProjectUrl>
<PackageIcon>LaserCatEyes_Logo.png</PackageIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Betalgo Up Ltd.</Authors>
<Company>Betalgo Up Ltd.</Company>
<Product>Laser Cat Eyes</Product>
Expand Down
27 changes: 24 additions & 3 deletions LaserCatEyes.DataServiceSdk/LaserCatEyesDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using LaserCatEyes.Domain;
using LaserCatEyes.Domain.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace LaserCatEyes.DataServiceSdk
Expand All @@ -13,18 +14,21 @@ public class LaserCatEyesDataService : ILaserCatEyesDataService
{
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
});

private readonly Guid _deviceId;
private readonly LaserCatEyesOptions _laserCatEyesOptions;
private readonly LaserCatEyesSystemOptions _laserCatEyesSystemOptions;
private readonly bool _serviceReady;

public LaserCatEyesDataService(IOptions<LaserCatEyesOptions> laserCatEyesOptions, IOptions<LaserCatEyesSystemOptions> laserCatEyesSystemOptions)
public LaserCatEyesDataService(IOptions<LaserCatEyesOptions> laserCatEyesOptions, IOptions<LaserCatEyesSystemOptions> laserCatEyesSystemOptions, ILogger<LaserCatEyesDataService> logger)
{
_laserCatEyesOptions = laserCatEyesOptions.Value;
_laserCatEyesSystemOptions = laserCatEyesSystemOptions.Value;

if (string.IsNullOrEmpty(_laserCatEyesOptions.AppKey))
{
throw new Exception("LaserCatEyes AppKey can not be null!");
logger.LogWarning("LaserCatEyes AppKey is NULL!");
return;
}

var deviceName = $"{Environment.MachineName}:{Environment.UserName}";
Expand Down Expand Up @@ -57,18 +61,35 @@ public LaserCatEyesDataService(IOptions<LaserCatEyesOptions> laserCatEyesOptions
_deviceId = httpResponseMessage.Content.ReadAsAsync<SubAppUpdateResponseModel>().Result.DeviceId;

_client.DefaultRequestHeaders.Add(Constants.Headers.AlgoronaDeviceId, _deviceId.ToString());
_serviceReady = true;
}


public async Task<HttpResponseMessage> ReportTask(PackageData data)
{
if (!_serviceReady)
{
return null;
}

data.DeviceUuid = _laserCatEyesOptions.DeviceUuid;
data.DeviceId = _deviceId;
return await _client.PostAsJsonAsync(_laserCatEyesSystemOptions.Endpoints.DataSendPackage, data);
}


public bool IsServiceReady()
{
return _serviceReady;
}

public void Report(PackageData data)
{
if (!_serviceReady)
{
return;
}

Task.Run(() => ReportTask(data)).Forget();
}

Expand Down
1 change: 1 addition & 0 deletions LaserCatEyes.Domain/ILaserCatEyesDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace LaserCatEyes.Domain
{
public interface ILaserCatEyesDataService
{
bool IsServiceReady();
void Report(PackageData data);
Task<HttpResponseMessage> ReportTask(PackageData data);
}
Expand Down
2 changes: 1 addition & 1 deletion LaserCatEyes.Domain/LaserCatEyes.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://portal.lasercateyes.com</PackageProjectUrl>
<PackageIcon>LaserCatEyes_Logo.png</PackageIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Betalgo Up Ltd.</Authors>
<Company>Betalgo Up Ltd.</Company>
<Product>Laser Cat Eyes</Product>
Expand Down
8 changes: 6 additions & 2 deletions LaserCatEyes.Domain/Models/PackageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ public class PackageData
public Guid? DeviceId { get; set; }
public Guid? DeviceUuid { get; set; }
public Guid? AppId { get; set; }
public string LocalIpAddress { get; set; }
public string RemoteIpAddress { get; set; }

public static PackageData CreateRequestPackage(Guid id, string url, MethodType methodType, List<string> headers, string body, DateTime? timeStamp)
public static PackageData CreateRequestPackage(Guid id, string url, MethodType methodType, List<string> headers, string body, DateTime? timeStamp, string localIpAddress, string remoteIpAddress)
{
return new PackageData
{
Id = id,
RequestPackage = new RequestPackage(id, url, methodType, headers, body, timeStamp)
RequestPackage = new RequestPackage(id, url, methodType, headers, body, timeStamp),
LocalIpAddress = localIpAddress,
RemoteIpAddress = remoteIpAddress
};
}

Expand Down
5 changes: 5 additions & 0 deletions LaserCatEyes.DotNetSdk.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml"
xmlns:wpf="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Algorona/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
22 changes: 18 additions & 4 deletions LaserCatEyes.EndpointListener/HttpListenerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,33 @@
using LaserCatEyes.Domain;
using LaserCatEyes.Domain.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace LaserCatEyes.EndpointListener
{
public class EndpointListenerMiddleware : IMiddleware
{
private readonly ILaserCatEyesDataService _laserCatEyesDataService;

public EndpointListenerMiddleware(ILaserCatEyesDataService laserCatEyesDataService)
private readonly bool _isServiceReady;
public EndpointListenerMiddleware(ILaserCatEyesDataService laserCatEyesDataService, ILogger<EndpointListenerMiddleware> logger)
{
if (laserCatEyesDataService == null)
{
logger.LogWarning($"Couldn't bind {nameof(EndpointListenerMiddleware)} because {nameof(ILaserCatEyesDataService)} is null");
return;
}
if(!laserCatEyesDataService.IsServiceReady())
{
logger.LogWarning($"Couldn't bind {nameof(EndpointListenerMiddleware)} because {nameof(ILaserCatEyesDataService)} was not ready");
return;
}
_laserCatEyesDataService = laserCatEyesDataService;
_isServiceReady = true;
}

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
if (context?.Request == null)
if (context?.Request == null || !_isServiceReady)
{
await next(context);
return;
Expand All @@ -35,7 +47,9 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
Utilities.HttpMethodStringToEnumConverter(context.Request.Method),
context.Request.Headers?.SelectMany(r => r.Value.Select(value => $"{r.Key}:{value}")).ToList(),
await Utilities.ReadBodyStream(context.Request.Body),
DateTime.UtcNow
DateTime.UtcNow,
context.Connection?.LocalIpAddress?.ToString(),
context.Connection?.RemoteIpAddress?.ToString()
));

var originalResponseBody = context.Response.Body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://portal.lasercateyes.com</PackageProjectUrl>
<PackageIcon>LaserCatEyes_Logo.png</PackageIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Betalgo Up Ltd.</Authors>
<Company>Betalgo Up Ltd.</Company>
<Product>Laser Cat Eyes</Product>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://portal.lasercateyes.com</PackageProjectUrl>
<PackageIcon>LaserCatEyes_Logo.png</PackageIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Betalgo Up Ltd.</Authors>
<Company>Betalgo Up Ltd.</Company>
<Product>Laser Cat Eyes</Product>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,44 @@
using System.Threading;
using System.Threading.Tasks;
using LaserCatEyes.Domain;
using Microsoft.Extensions.Logging;

namespace LaserCatEyes.HttpClientListener.DotNetStandard
{
public class LaserCatEyesHttpMessageHandler : DelegatingHandler
{
private readonly ILaserCatEyesDataService _laserCatEyesDataService;
private readonly bool _serviceReady;

public LaserCatEyesHttpMessageHandler(ILaserCatEyesDataService laserCatEyesDataService)
public LaserCatEyesHttpMessageHandler(ILaserCatEyesDataService laserCatEyesDataService,ILogger<LaserCatEyesHttpMessageHandler> logger)
{
_laserCatEyesDataService = laserCatEyesDataService ?? throw new ArgumentNullException(nameof(laserCatEyesDataService));
if (laserCatEyesDataService == null)
{
logger.LogWarning($"Couldn't bind {nameof(LaserCatEyesHttpMessageHandler)} because {nameof(ILaserCatEyesDataService)} is null");
return;
}
if (!laserCatEyesDataService.IsServiceReady())
{
logger.LogWarning($"Couldn't bind {nameof(LaserCatEyesHttpMessageHandler)} because {nameof(ILaserCatEyesDataService)} was not ready");
return;
}
_laserCatEyesDataService = laserCatEyesDataService;
_serviceReady = true;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
if (request == null || !_serviceReady)
{
return await base.SendAsync(null, cancellationToken);
return await base.SendAsync(request, cancellationToken);
}

var operationId = Guid.NewGuid();

_laserCatEyesDataService.Report(PackageDataHelper.RequestPackageDataFromHttpRequestMessage(operationId, request));

var response = await base.SendAsync(request, cancellationToken);

_laserCatEyesDataService.Report(PackageDataHelper.ResponsePackageDataFromHttpResponseMessage(operationId, response));

return response;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public static PackageData RequestPackageDataFromHttpRequestMessage(Guid id, Http
request.RequestUri.ToString(),
Utilities.HttpMethodStringToEnumConverter(request.Method.Method),
request.Headers.SelectMany(r => r.Value.Select(value => $"{r.Key}:{value}")).ToList(),
request.Content?.ReadAsStringAsync().Result, DateTime.UtcNow);
request.Content?.ReadAsStringAsync().Result,
DateTime.UtcNow,
null,
null);
}

public static PackageData ResponsePackageDataFromHttpResponseMessage(Guid id, HttpResponseMessage response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://portal.lasercateyes.com</PackageProjectUrl>
<PackageIcon>LaserCatEyes_Logo.png</PackageIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Betalgo Up Ltd.</Authors>
<Company>Betalgo Up Ltd.</Company>
<Product>Laser Cat Eyes</Product>
Expand Down
21 changes: 17 additions & 4 deletions LaserCatEyes.HttpClientListener/LaserCatEyesHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@
using System.Threading;
using System.Threading.Tasks;
using LaserCatEyes.Domain;
using Microsoft.Extensions.Logging;

namespace LaserCatEyes.HttpClientListener
{
public class LaserCatEyesHttpMessageHandler : DelegatingHandler
{
private readonly ILaserCatEyesDataService _laserCatEyesDataService;
private readonly bool _serviceReady;

public LaserCatEyesHttpMessageHandler(ILaserCatEyesDataService laserCatEyesDataService)
public LaserCatEyesHttpMessageHandler(ILaserCatEyesDataService laserCatEyesDataService,ILogger<LaserCatEyesHttpMessageHandler> logger)
{
_laserCatEyesDataService = laserCatEyesDataService ?? throw new ArgumentNullException(nameof(laserCatEyesDataService));
if (laserCatEyesDataService == null)
{
logger.LogWarning($"Couldn't bind {nameof(LaserCatEyesHttpMessageHandler)} because {nameof(ILaserCatEyesDataService)} is null");
return;
}
if (!laserCatEyesDataService.IsServiceReady())
{
logger.LogWarning($"Couldn't bind {nameof(LaserCatEyesHttpMessageHandler)} because {nameof(ILaserCatEyesDataService)} was not ready");
return;
}
_laserCatEyesDataService = laserCatEyesDataService;
_serviceReady = true;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
if (request == null || !_serviceReady)
{
return await base.SendAsync(null, cancellationToken);
return await base.SendAsync(request, cancellationToken);
}

var operationId = Guid.NewGuid();
Expand Down
Loading

0 comments on commit 80f50c1

Please sign in to comment.