Skip to content

Commit

Permalink
refactor (#50)
Browse files Browse the repository at this point in the history
* refactor

* skip the count

* refactor

* fix test

* refactor

* refactor
  • Loading branch information
Lomet authored Oct 14, 2024
1 parent 7da3e27 commit 35699b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
41 changes: 14 additions & 27 deletions src/AdminKycProxy/LambdaFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Flurl.Http;
using KYC.DataBase;
using SecretsManager;
using EnvironmentManager;
using Amazon.Lambda.Core;
using AdminKycProxy.Models;

Expand All @@ -13,7 +12,7 @@ namespace AdminKycProxy;

public class LambdaFunction
{
private const int MaxRetries = 20;
private const int limit = 20;
private readonly LambdaSettings lambdaSettings;
private readonly KycDbContext context;

Expand All @@ -29,37 +28,25 @@ public LambdaFunction(SecretManager secretManager, KycDbContext context)

public async Task<HttpStatusCode> RunAsync()
{
var skip = new EnvManager().GetEnvironmentValue<int>("DOWNLOADED_FROM", true);
var url = new Url(lambdaSettings.Url);
url = url.SetQueryParam("skip", skip);
url = url.SetQueryParam("limit", MaxRetries);

var hasMore = true;
while (hasMore)
{
var response = url
HttpResponse? response = null;
var skip = context.Users.Count();
var url = lambdaSettings.Url
.SetQueryParam("limit", limit)
.WithHeader("Authorization", lambdaSettings.SecretApiKey)
.WithHeader("cache-control", "no-cache")
.GetAsync()
.ReceiveJson<HttpResponse>()
.GetAwaiter()
.GetResult();
.WithHeader("cache-control", "no-cache");

if (response.Data.Records.Length == 0)
{
hasMore = false;
skip = response.Data.Total;
continue;
}
while (response == null || response.Data.Records.Length > 0)
{
response = await url
.SetQueryParam("skip", skip)
.GetJsonAsync<HttpResponse>();

context.Users.AddRange(response.Data.Records);
context.Users.AddRange(response.Data.Records.Where(x =>
!context.Users.Contains(x)));

skip += MaxRetries;
url.SetQueryParam("skip", skip);
skip += limit;
}

await context.SaveChangesAsync();

return HttpStatusCode.OK;
}
}
4 changes: 2 additions & 2 deletions tests/AdminKycProxy.Tests/LambdaFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ internal async Task RunAsync()
};
using var httpTest = new HttpTest();
httpTest
.ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?skip=0&limit=20")
.ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?limit=20&skip=0")
.RespondWithJson(response);
httpTest
.ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?skip=20&limit=20")
.ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?limit=20&skip=20")
.RespondWithJson(new HttpResponse());

var context = new DbContextFactory<KycDbContext>().Create(ContextOption.InMemory, Guid.NewGuid().ToString());
Expand Down

0 comments on commit 35699b5

Please sign in to comment.