-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from The-Poolz/lomet-refactor
add linq
- Loading branch information
Showing
6 changed files
with
107 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,41 @@ | ||
using Amazon.Lambda.Core; | ||
using KYC.API.Proxy.Utils; | ||
using KYC.API.Proxy.Models; | ||
using KYC.API.Proxy.Models.HttpResponse; | ||
|
||
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] | ||
|
||
namespace KYC.API.Proxy; | ||
|
||
public class LambdaFunction | ||
namespace KYC.API.Proxy | ||
{ | ||
public const string ZeroAddress = "0x0000000000000000000000000000000000000000"; | ||
|
||
private readonly HttpCall httpCall; | ||
private readonly DynamoDb dynamoDb; | ||
|
||
public LambdaFunction() | ||
: this(new HttpCall(), new DynamoDb()) | ||
{ } | ||
|
||
public LambdaFunction(HttpCall httpCall, DynamoDb dynamoDb) | ||
{ | ||
this.httpCall = httpCall; | ||
this.dynamoDb = dynamoDb; | ||
} | ||
|
||
public async Task<OutputData> RunAsync(InputData request) | ||
public class LambdaFunction : LambdaFunctionScenarios | ||
{ | ||
if (string.IsNullOrWhiteSpace(request.Address) || request.Address == ZeroAddress) | ||
{ | ||
return new OutputData | ||
{ | ||
RequestStatus = RequestStatus.error | ||
}; | ||
} | ||
var response = httpCall.GetBlockPassResponse(request.Address); | ||
public LambdaFunction() | ||
: this(new HttpCall(), new DynamoDb()) | ||
{ } | ||
|
||
if (response.Status != RequestStatus.error) | ||
{ | ||
return BuildOutputData(response); | ||
} | ||
public LambdaFunction(HttpCall httpCall, DynamoDb dynamoDb) | ||
: base(httpCall, dynamoDb) | ||
{ } | ||
|
||
var proxy = dynamoDb.GetProxyAddress(request.Address); | ||
if (proxy != null) | ||
public OutputData Run(InputData request) | ||
{ | ||
response = httpCall.GetBlockPassResponse(proxy); | ||
if (!request.Valid) | ||
return OutputData.Error; | ||
|
||
if (response.Status != RequestStatus.error) | ||
var scenarios = new List<Func<InputData, OutputData?>> | ||
{ | ||
return BuildOutputData(response, proxy); | ||
} | ||
} | ||
HandleBlockPassResponse, | ||
HandleProxyAddress, | ||
HandleValidWallet | ||
}; | ||
|
||
var wallets = dynamoDb.GetWallets(request.Address); | ||
foreach (var wallet in wallets) | ||
{ | ||
response = httpCall.GetBlockPassResponse(wallet); | ||
if (response.Status != RequestStatus.error) | ||
foreach (var scenario in scenarios) | ||
{ | ||
if (proxy != wallet) | ||
{ | ||
await dynamoDb.UpdateItemAsync(request.Address, wallet); | ||
} | ||
return BuildOutputData(response, wallet); | ||
var result = scenario(request); | ||
if (result != null) | ||
return result; | ||
} | ||
} | ||
|
||
return new OutputData | ||
{ | ||
RequestStatus = RequestStatus.error | ||
}; | ||
} | ||
|
||
private static OutputData BuildOutputData(Response response, string? proxy = null) | ||
{ | ||
return new OutputData | ||
{ | ||
RequestStatus = response.Status, | ||
Status = response.Data.Status, | ||
Name = response.Data.Identities.GivenName.Value, | ||
Proxy = proxy | ||
}; | ||
return OutputData.Error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using KYC.API.Proxy.Models; | ||
using KYC.API.Proxy.Utils; | ||
|
||
namespace KYC.API.Proxy; | ||
|
||
public class LambdaFunctionScenarios | ||
{ | ||
internal LambdaFunctionScenarios(HttpCall httpCall, DynamoDb dynamoDb) | ||
{ | ||
_httpCall = httpCall; | ||
_dynamoDb = dynamoDb; | ||
} | ||
|
||
private readonly HttpCall _httpCall; | ||
private readonly DynamoDb _dynamoDb; | ||
internal OutputData? HandleBlockPassResponse(InputData request) | ||
{ | ||
var response = _httpCall.GetBlockPassResponse(request.Address); | ||
return response.Status == RequestStatus.success ? new OutputData(response) : null; | ||
} | ||
|
||
internal OutputData? HandleProxyAddress(InputData request) | ||
{ | ||
var proxy = _dynamoDb.GetProxyAddress(request.Address); | ||
if (proxy == null) return null; | ||
|
||
var response = _httpCall.GetBlockPassResponse(proxy); | ||
return response.Status == RequestStatus.success ? new OutputData(response, proxy) : null; | ||
} | ||
|
||
internal OutputData? HandleValidWallet(InputData request) | ||
{ | ||
var proxy = _dynamoDb.GetProxyAddress(request.Address); | ||
|
||
var validWallet = _dynamoDb.GetWallets(request.Address) | ||
.Select(wallet => new { Wallet = wallet, Response = _httpCall.GetBlockPassResponse(wallet) }) | ||
.FirstOrDefault(w => w.Response.Status == RequestStatus.success); | ||
|
||
if (validWallet == null) return null; | ||
|
||
if (proxy != validWallet.Wallet) | ||
_dynamoDb.UpdateItem(request.Address, validWallet.Wallet); | ||
|
||
return new OutputData(validWallet.Response, validWallet.Wallet); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
using Newtonsoft.Json; | ||
using KYC.API.Proxy.Models.HttpResponse; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
namespace KYC.API.Proxy.Models; | ||
|
||
public class OutputData | ||
{ | ||
internal OutputData() { } | ||
public OutputData(Response response, string? proxy = null) | ||
{ | ||
RequestStatus = response.Status; | ||
Status = response.Data.Status; | ||
Name = response.Data.Identities.GivenName.Value; | ||
Proxy = proxy; | ||
} | ||
[JsonConverter(typeof(StringEnumConverter))] | ||
public RequestStatus RequestStatus { get; set; } | ||
public string? Status { get; set; } | ||
public string? Name { get; set; } | ||
public string? Proxy { get; set; } | ||
public static OutputData Error => new() | ||
{ | ||
RequestStatus = RequestStatus.error | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters