-
Notifications
You must be signed in to change notification settings - Fork 2
Muhannad/api gateway #124
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
Merged
Merged
Muhannad/api gateway #124
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8ba2910
native adapters
samerzughul d9cec1f
remove md
samerzughul 14fb13d
api gateway initial
mmalkhatib 9b98028
m
mmalkhatib e44b5b3
Add GlobalAdapterValuesSet entity and CRUD operations for API Gateway…
mmalkhatib a93be33
Add Get handler for GlobalAdapterValuesSet and modify Create response…
AhmadRAbuhussein c17bf89
Add AdapterProperties to Partner entity and update Get and Update han…
AhmadRAbuhussein 9e625f9
Add handler names for AddPartner, RemovePartner, and UpdatePartner; e…
AhmadRAbuhussein 399cede
Remove sensitive connection strings and configuration settings from a…
AhmadRAbuhussein 2eaaf20
Remove sensitive configuration settings from launchSettings.json
AhmadRAbuhussein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or 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,126 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net.Mime; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using SW.Bitween.Domain; | ||
| using SW.Bitween.Domain.Gateway; | ||
| using SW.Bitween.Model; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.Controllers; | ||
|
|
||
| [ApiController] | ||
| [Route("api/[controller]")] | ||
| public class GatewayController( | ||
| BitweenDbContext dbContext, | ||
| RequestContext requestContext, | ||
| IInfolinkCache cache, | ||
| XchangeService xchangeService, | ||
| BitweenOptions bitweenSettings) : ControllerBase | ||
| { | ||
| [HttpPost("{gatewayApiName}/sync")] | ||
| public Task<IActionResult> PostSync([FromRoute] string gatewayApiName) | ||
| { | ||
| return ProcessAsync(gatewayApiName, resultSync: true); | ||
| } | ||
|
|
||
| [HttpPost("{gatewayApiName}/async")] | ||
| public Task<IActionResult> PostAsync([FromRoute] string gatewayApiName) | ||
| { | ||
| return ProcessAsync(gatewayApiName, resultSync: false); | ||
| } | ||
|
|
||
| private async Task<IActionResult> ProcessAsync([FromRoute] string gatewayApiName, bool resultSync) | ||
| { | ||
| var globalAdapterValuesSet = await cache.ListGlobalAdapterValuesSetsAsync(); | ||
| var apiGateway = await dbContext.Set<ApiGateway>() | ||
| .Include(ag => ag.Partners) | ||
| .ThenInclude(agp => agp.Partner) | ||
| .FirstOrDefaultAsync(ag => ag.UrlName == gatewayApiName); | ||
|
|
||
| if (apiGateway == null) | ||
| return NotFound(); | ||
|
|
||
| // Resolve partner using API key | ||
| var (authorized, partner, keyName) = await dbContext.CheckPartnerAuthorized(requestContext); | ||
|
|
||
| if (!authorized) | ||
| return Unauthorized(); | ||
|
|
||
| // Verify partner is part of the API Gateway | ||
| var apiGatewayPartner = apiGateway.Partners.FirstOrDefault(agp => agp.PartnerId == partner.Id); | ||
| if (apiGatewayPartner == null) | ||
| return Unauthorized(); | ||
|
|
||
| var subscription = await cache.SubscriptionByIdAsync(apiGatewayPartner.SubscriptionId); | ||
|
|
||
|
|
||
| var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync(); | ||
|
|
||
| var xchangeFile = new XchangeFile(json); | ||
|
|
||
| var validatorProperties = subscription.ValidatorProperties.ToDictionary() | ||
| .Fill(partner, globalAdapterValuesSet); | ||
| await xchangeService.RunValidator(subscription.ValidatorId, validatorProperties, | ||
| xchangeFile); | ||
|
|
||
| var xchangeReferences = new List<string> { $"partnerkey: {keyName}" }; | ||
| var globalAdapterValuesSets = await dbContext.Set<GlobalAdapterValuesSet>().ToArrayAsync(); | ||
| var xchangeId = await xchangeService.SubmitSubscriptionXchange(subscription.Id, xchangeFile, | ||
| xchangeReferences.ToArray(), partner, globalAdapterValuesSets); | ||
| if (!resultSync) | ||
| { | ||
| return Accepted(xchangeId); | ||
| } | ||
|
|
||
| var waitResponse = 120; | ||
| // check headers for wait response value | ||
| var waitResponseHeader = Request.Headers["Wait-Period"].FirstOrDefault(); | ||
| if (int.TryParse(waitResponseHeader, out var waitResponseValue)) | ||
| { | ||
| waitResponse = waitResponseValue <= 0 ? 120 : waitResponseValue; | ||
| } | ||
|
|
||
| var currentFibTerm = 1; | ||
| var previousTerm = 1; | ||
| while (currentFibTerm <= waitResponse) | ||
| { | ||
| await Task.Delay(TimeSpan.FromSeconds(currentFibTerm)); | ||
| var nextTerm = Math.Min(currentFibTerm + previousTerm, 8); | ||
| previousTerm = currentFibTerm; | ||
| currentFibTerm = nextTerm; | ||
| if (!await dbContext.Set<XchangeResult>() | ||
| .AsNoTracking() | ||
| .AnyAsync(i => i.Id == xchangeId)) continue; | ||
|
|
||
| var xchangeResult = await dbContext.FindAsync<XchangeResult>(xchangeId); | ||
|
|
||
|
|
||
| switch (xchangeResult!.Success) | ||
| { | ||
| case true when xchangeResult.ResponseSize == 0: | ||
| { | ||
| return Ok(xchangeId); | ||
| } | ||
| case true when xchangeResult.ResponseSize != 0: | ||
| { | ||
| var response = await xchangeService.GetFile(xchangeId, XchangeFileType.Response); | ||
| return new ContentResult | ||
| { | ||
| StatusCode = xchangeResult.ResponseBad ? 400 : 200, | ||
| Content = response, | ||
| ContentType = xchangeResult.ResponseContentType ?? MediaTypeNames.Application.Json, | ||
| }; | ||
| } | ||
| case false: | ||
| return BadRequest(); | ||
| } | ||
| } | ||
|
|
||
| return Accepted(xchangeId); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,16 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.Domain.Gateway; | ||
|
|
||
| public class ApiGateway : BaseEntity,IAudited | ||
| { | ||
| public string Name { get; set; } | ||
| public string UrlName { get; set; } | ||
| public ICollection<ApiGatewayPartner> Partners { get; set; } | ||
| public DateTime CreatedOn { get; set; } | ||
| public string CreatedBy { get; set; } | ||
| public DateTime? ModifiedOn { get; set; } | ||
| public string ModifiedBy { get; set; } | ||
| } |
This file contains hidden or 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,18 @@ | ||
| using System; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.Domain.Gateway; | ||
|
|
||
| public class ApiGatewayPartner: IAudited | ||
| { | ||
| public ApiGateway ApiGateway { get; set; } | ||
| public int ApiGatewayId { get; set; } | ||
| public Partner Partner { get; set; } | ||
| public int PartnerId { get; set; } | ||
| public Subscription Subscription { get; set; } | ||
| public int SubscriptionId { get; set; } | ||
| public DateTime CreatedOn { get; set; } | ||
| public string CreatedBy { get; set; } | ||
| public DateTime? ModifiedOn { get; set; } | ||
| public string ModifiedBy { get; set; } | ||
| } |
11 changes: 11 additions & 0 deletions
11
SW.Bitween.Api/Domain/GlobalAdapterValue/GlobalAdapterValuesSet.cs
This file contains hidden or 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,11 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.Domain; | ||
|
|
||
| public class GlobalAdapterValuesSet:BaseEntity<string> | ||
| { | ||
| public string Name { get; set; } | ||
| public Dictionary<string, string> Values { get; set; } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.