Compare commits
20 Commits
8f82d7f0b7
...
feat/ms-dy
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dc8bd233b | ||
|
|
54fce2990b | ||
|
|
3eed85e7de | ||
|
|
61bb5acdff | ||
|
|
d8ee61d107 | ||
|
|
7189b39d6b | ||
|
|
1fde6e7e34 | ||
|
|
54eca6fceb | ||
|
|
782680cda2 | ||
|
|
aeb1c4a810 | ||
|
|
67a4e91a1b | ||
|
|
6ba7fe230a | ||
|
|
9473ad7619 | ||
|
|
c70257bb28 | ||
|
|
054527b4ba | ||
|
|
836b19df9e | ||
|
|
27fb2af845 | ||
|
|
89b98af1be | ||
|
|
c352ee987c | ||
|
|
590546c9f9 |
37
DigitalData.Swagger.sln
Normal file
37
DigitalData.Swagger.sln
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.9.34622.214
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Swagger.Proxy", "src\DigitalData.Swagger.Proxy\DigitalData.Swagger.Proxy.csproj", "{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}"
|
||||||
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C5ED9B8C-5A36-440E-BF25-8FB0EC870924}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Swagger.MockAPI", "src\DigitalData.Swagger.MockAPI\DigitalData.Swagger.MockAPI.csproj", "{1D229E96-67BC-4717-B6E6-60A067BC498E}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{1D229E96-67BC-4717-B6E6-60A067BC498E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(NestedProjects) = preSolution
|
||||||
|
{2CC25C4F-147E-4A64-9EA9-51C00BC7430E} = {C5ED9B8C-5A36-440E-BF25-8FB0EC870924}
|
||||||
|
{1D229E96-67BC-4717-B6E6-60A067BC498E} = {C5ED9B8C-5A36-440E-BF25-8FB0EC870924}
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {071BB599-BE8A-4680-A008-35B0357D4899}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using DigitalData.Swagger.MockAPI.Repos;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.OData.Formatter;
|
||||||
|
using Microsoft.AspNetCore.OData.Query;
|
||||||
|
using Microsoft.AspNetCore.OData.Results;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Controllers;
|
||||||
|
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class CompaniesController(ICompanyRepo repo) : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ICompanyRepo _repo = repo;
|
||||||
|
|
||||||
|
[EnableQuery(PageSize = 3)]
|
||||||
|
[HttpGet]
|
||||||
|
public IQueryable<Company> Get()
|
||||||
|
{
|
||||||
|
return _repo.GetAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
[EnableQuery]
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public SingleResult<Company> Get([FromODataUri] int key)
|
||||||
|
{
|
||||||
|
return SingleResult.Create(_repo.GetById(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult Post([FromBody] Company company)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
_repo.Create(company);
|
||||||
|
|
||||||
|
return Created("companies", company);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
public IActionResult Put([FromODataUri] int key, [FromBody] Company company)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key != company.ID)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
_repo.Update(company);
|
||||||
|
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete]
|
||||||
|
public IActionResult Delete([FromODataUri] int key)
|
||||||
|
{
|
||||||
|
var company = _repo.GetById(key);
|
||||||
|
if (company is null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
_repo.Delete(company.First());
|
||||||
|
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
public class MockController : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
|
||||||
|
public IActionResult CreateInvoice([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company, Dictionary<string, object>? body)
|
||||||
|
{
|
||||||
|
return Created($"{servicetierName}/ODataV4/{webserviceName}?id={1}", new { Id = 1, Foo = 1});
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{servicetierName}/ODataV4/{webserviceName}")]
|
||||||
|
public IActionResult GetInvoice([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string id)
|
||||||
|
{
|
||||||
|
return Ok(new { Id = id, Foo = 1 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/DigitalData.Swagger.MockAPI/Data/ApiContext.cs
Normal file
11
src/DigitalData.Swagger.MockAPI/Data/ApiContext.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Data;
|
||||||
|
|
||||||
|
public class ApiContext(DbContextOptions<ApiContext> options) : DbContext(options)
|
||||||
|
{
|
||||||
|
public DbSet<Company> Companies { get; set; }
|
||||||
|
|
||||||
|
public DbSet<Product> Products { get; set; }
|
||||||
|
}
|
||||||
75
src/DigitalData.Swagger.MockAPI/Data/DBSeeder.cs
Normal file
75
src/DigitalData.Swagger.MockAPI/Data/DBSeeder.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Data
|
||||||
|
{
|
||||||
|
public class DBSeeder
|
||||||
|
{
|
||||||
|
public static void AddCompaniesData(WebApplication app)
|
||||||
|
{
|
||||||
|
var scope = app.Services.CreateScope();
|
||||||
|
var db = scope.ServiceProvider.GetService<ApiContext>();
|
||||||
|
|
||||||
|
db.Companies.AddRange(new List<Company>()
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
ID = 1,
|
||||||
|
Name = "TechCorp",
|
||||||
|
Size = 500,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 1, CompanyID = 1, Name = "Laptop X1", Price = 1200.99m },
|
||||||
|
new() { ID = 2, CompanyID = 1, Name = "Smartphone S2", Price = 799.49m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 2,
|
||||||
|
Name = "AutoWorks",
|
||||||
|
Size = 300,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new Product { ID = 3, CompanyID = 2, Name = "Electric Car E1", Price = 35000m },
|
||||||
|
new() { ID = 4, CompanyID = 2, Name = "Hybrid SUV H2", Price = 42000m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 3,
|
||||||
|
Name = "MediHealth",
|
||||||
|
Size = 200,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 5, CompanyID = 3, Name = "Vitamin Pack V10", Price = 19.99m },
|
||||||
|
new() { ID = 6, CompanyID = 3, Name = "Protein Shake P5", Price = 25.49m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 4,
|
||||||
|
Name = "HomeStyle",
|
||||||
|
Size = 150,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 7, CompanyID = 4, Name = "Smart Blender B3", Price = 89.99m },
|
||||||
|
new() { ID = 8, CompanyID = 4, Name = "Air Purifier A1", Price = 149.99m }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Company
|
||||||
|
{
|
||||||
|
ID = 5,
|
||||||
|
Name = "GadgetZone",
|
||||||
|
Size = 400,
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new() { ID = 9, CompanyID = 5, Name = "Wireless Earbuds W1", Price = 99.99m },
|
||||||
|
new() { ID = 10, CompanyID = 5, Name = "Smart Watch G5", Price = 199.99m }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
db.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.1.3" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
@DigitalData.Swagger.MockAPI_HostAddress = http://localhost:5055
|
||||||
|
|
||||||
|
GET {{DigitalData.Swagger.MockAPI_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
9
src/DigitalData.Swagger.MockAPI/Entities/Company.cs
Normal file
9
src/DigitalData.Swagger.MockAPI/Entities/Company.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
|
||||||
|
public class Company
|
||||||
|
{
|
||||||
|
public int ID { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public int Size { get; set; }
|
||||||
|
public List<Product>? Products { get; set; }
|
||||||
|
}
|
||||||
9
src/DigitalData.Swagger.MockAPI/Entities/Product.cs
Normal file
9
src/DigitalData.Swagger.MockAPI/Entities/Product.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
|
||||||
|
public class Product
|
||||||
|
{
|
||||||
|
public int ID { get; set; }
|
||||||
|
public int CompanyID { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public decimal Price { get; set; }
|
||||||
|
}
|
||||||
51
src/DigitalData.Swagger.MockAPI/Program.cs
Normal file
51
src/DigitalData.Swagger.MockAPI/Program.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Data;
|
||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using DigitalData.Swagger.MockAPI.Repos;
|
||||||
|
using Microsoft.AspNetCore.OData;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.OData.ModelBuilder;
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
var config = builder.Configuration;
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
builder.Services.AddDbContext<ApiContext>(opt => opt.UseInMemoryDatabase(databaseName: "CompaniesDB"));
|
||||||
|
builder.Services.AddScoped<ICompanyRepo, CompanyRepo>();
|
||||||
|
|
||||||
|
ODataConventionModelBuilder oDataModelbuilder = new();
|
||||||
|
oDataModelbuilder.EntitySet<Company>("Companies");
|
||||||
|
|
||||||
|
builder.Services.AddControllers()
|
||||||
|
.AddOData(options => options
|
||||||
|
.AddRouteComponents("odata", oDataModelbuilder.GetEdmModel())
|
||||||
|
.Select()
|
||||||
|
.Filter()
|
||||||
|
.OrderBy()
|
||||||
|
.SetMaxTop(20)
|
||||||
|
.Count()
|
||||||
|
.Expand()
|
||||||
|
);
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
DBSeeder.AddCompaniesData(app);
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
if (config.GetValue<bool>("UseSwagger"))
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:53791",
|
||||||
|
"sslPort": 44384
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:5055",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "https://localhost:7248;http://localhost:5055",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
46
src/DigitalData.Swagger.MockAPI/Repos/CompanyRepo.cs
Normal file
46
src/DigitalData.Swagger.MockAPI/Repos/CompanyRepo.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Data;
|
||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Repos;
|
||||||
|
|
||||||
|
public class CompanyRepo(ApiContext context) : ICompanyRepo
|
||||||
|
{
|
||||||
|
private readonly ApiContext _context = context;
|
||||||
|
|
||||||
|
public IQueryable<Company> GetAll()
|
||||||
|
{
|
||||||
|
return _context.Companies
|
||||||
|
.Include(a => a.Products)
|
||||||
|
.AsQueryable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IQueryable<Company> GetById(int id)
|
||||||
|
{
|
||||||
|
return _context.Companies
|
||||||
|
.Include(a => a.Products)
|
||||||
|
.AsQueryable()
|
||||||
|
.Where(c => c.ID == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Create(Company company)
|
||||||
|
{
|
||||||
|
_context.Companies
|
||||||
|
.Add(company);
|
||||||
|
_context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(Company company)
|
||||||
|
{
|
||||||
|
_context.Companies
|
||||||
|
.Update(company);
|
||||||
|
_context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(Company company)
|
||||||
|
{
|
||||||
|
_context.Companies
|
||||||
|
.Remove(company);
|
||||||
|
_context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/DigitalData.Swagger.MockAPI/Repos/ICompanyRepo.cs
Normal file
11
src/DigitalData.Swagger.MockAPI/Repos/ICompanyRepo.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using DigitalData.Swagger.MockAPI.Dtos;
|
||||||
|
namespace DigitalData.Swagger.MockAPI.Repos;
|
||||||
|
|
||||||
|
public interface ICompanyRepo
|
||||||
|
{
|
||||||
|
public IQueryable<Company> GetAll();
|
||||||
|
public IQueryable<Company> GetById(int id);
|
||||||
|
public void Create(Company company);
|
||||||
|
public void Update(Company company);
|
||||||
|
public void Delete(Company company);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/DigitalData.Swagger.MockAPI/appsettings.json
Normal file
10
src/DigitalData.Swagger.MockAPI/appsettings.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"UseSwagger": true
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace DigitalData.Swagger.Proxy.Configs
|
||||||
|
{
|
||||||
|
public class OriginServerParams
|
||||||
|
{
|
||||||
|
public required string Url { get; init; }
|
||||||
|
|
||||||
|
public Dictionary<string, string> DefaultHeaders { get; init; } = new();
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/DigitalData.Swagger.Proxy/Controllers/MetaController.cs
Normal file
13
src/DigitalData.Swagger.Proxy/Controllers/MetaController.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using DigitalData.Swagger.Proxy.Configs;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace DigitalData.Swagger.Proxy.Controllers;
|
||||||
|
|
||||||
|
[Route("[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class MetaController(IOptions<OriginServerParams> originServerParamsoptions) : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult GetOriginServerParams() => Ok(originServerParamsoptions.Value);
|
||||||
|
}
|
||||||
58
src/DigitalData.Swagger.Proxy/Controllers/ProxyController.cs
Normal file
58
src/DigitalData.Swagger.Proxy/Controllers/ProxyController.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
namespace DigitalData.Swagger.Proxy.Controllers;
|
||||||
|
|
||||||
|
using DigitalData.Swagger.Proxy.Configs;
|
||||||
|
using Flurl;
|
||||||
|
using Flurl.Http;
|
||||||
|
using Flurl.Util;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
public class ProxyController(IOptions<OriginServerParams> originServerParamsOptions) : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly OriginServerParams _origin = originServerParamsOptions.Value;
|
||||||
|
|
||||||
|
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
|
||||||
|
public async Task<IActionResult> Redir([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company, [FromBody] Dictionary<string, object>? body = null)
|
||||||
|
{
|
||||||
|
var req = _origin.Url
|
||||||
|
.AppendPathSegment(servicetierName)
|
||||||
|
.AppendPathSegment("ODataV4")
|
||||||
|
.AppendPathSegment(webserviceName + "_CreateInvoice")
|
||||||
|
.SetQueryParams(new { company })
|
||||||
|
.WithHeader("X-Forwarded-For", HttpContext.Connection.RemoteIpAddress?.ToString());
|
||||||
|
|
||||||
|
// Add default headers
|
||||||
|
foreach (var header in _origin.DefaultHeaders)
|
||||||
|
req = req.WithHeader(header.Key, header.Value);
|
||||||
|
|
||||||
|
// Add request headers
|
||||||
|
foreach (var header in HttpContext.Request.Headers)
|
||||||
|
req = req.WithHeader(header.Key, header.Value);
|
||||||
|
|
||||||
|
// Add reqest cookies
|
||||||
|
foreach (var cookie in HttpContext.Request.Cookies)
|
||||||
|
req = req.WithCookie(cookie.Key, cookie.Value);
|
||||||
|
|
||||||
|
//post request
|
||||||
|
var res = body is null ? await req.PostAsync() : await req.PostJsonAsync(body);
|
||||||
|
|
||||||
|
// set headers
|
||||||
|
foreach (var (Name, Value) in res.Headers)
|
||||||
|
HttpContext.Response.Headers[Name] = Value;
|
||||||
|
|
||||||
|
// set cookies
|
||||||
|
foreach (var (Name, Value) in res.Cookies.ToKeyValuePairs())
|
||||||
|
HttpContext.Response.Cookies.Append(Name, Value.ToString() ?? string.Empty);
|
||||||
|
|
||||||
|
var resBody = res.GetJsonAsync<dynamic>();
|
||||||
|
|
||||||
|
if (resBody is not null)
|
||||||
|
return new JsonResult(resBody)
|
||||||
|
{
|
||||||
|
StatusCode = res.StatusCode
|
||||||
|
};
|
||||||
|
return StatusCode(res.StatusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Flurl.Http" Version="4.0.2" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
31
src/DigitalData.Swagger.Proxy/Program.cs
Normal file
31
src/DigitalData.Swagger.Proxy/Program.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using DigitalData.Swagger.Proxy.Configs;
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
var config = builder.Configuration;
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
var originServerSection = config.GetRequiredSection("OriginServer")
|
||||||
|
?? throw new InvalidOperationException("The 'OriginServer' section is missing or incorrectly configured in the application configuration.");
|
||||||
|
|
||||||
|
builder.Services.Configure<OriginServerParams>(originServerSection);
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// use swagger in production
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
||||||
41
src/DigitalData.Swagger.Proxy/Properties/launchSettings.json
Normal file
41
src/DigitalData.Swagger.Proxy/Properties/launchSettings.json
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:60947",
|
||||||
|
"sslPort": 44357
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:5082",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "https://localhost:7121;http://localhost:5082",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/DigitalData.Swagger.Proxy/appsettings.json
Normal file
15
src/DigitalData.Swagger.Proxy/appsettings.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"OriginServer": {
|
||||||
|
"Url": "https://localhost:7248",
|
||||||
|
"DefaultHeaders": {
|
||||||
|
"Authorization": "Basic username:password"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user