Skip to content

Commit

Permalink
feat(issue-149): implement api gateway for e-sender api
Browse files Browse the repository at this point in the history
Merge pull request #151 from live-dev999/live-dev999/issue149
  • Loading branch information
live-dev999 committed Jan 13, 2022
2 parents 53c881d + aeaf196 commit 3188b2b
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace O2NextGen.Web.BFF.Core.Config
{
public class UrlsConfig
{
public string ESenderUrl { get; set; }
public string CGenUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;

namespace O2NextGen.Mobile.BFF.Core.Features.CGen
namespace O2NextGen.Web.BFF.Core.Features.C_Gen
{
[Route("api/features/c-gen/[controller]")]
public class CertificatesController : Controller
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;

namespace O2NextGen.Mobile.BFF.Core.Features.CGen
namespace O2NextGen.Web.BFF.Core.Features.C_Gen
{
[Route("api/features/c-gen/[controller]")]
public class VersionController : Controller
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using O2NextGen.Web.BFF.Core.Features.E_Sender.Models;
using O2NextGen.Web.BFF.Core.Features.E_Sender.Services;

namespace O2NextGen.Web.BFF.Core.Features.E_Sender
{
[Route("api/features/e-sender")]
public class ESenderController : Controller
{
private readonly IESenderService _senderService;

public ESenderController(IESenderService senderService)
{
_senderService = senderService;
}

#region Methods

[HttpGet]
[Route("")]
public async Task<IActionResult> GetAllAsync()
{
throw new NotImplementedException();
}

[HttpGet]
[Route("{id}")]
public async Task<IActionResult> GetByIdAsync(long id, CancellationToken ct)
{
var result = _senderService.GetAsync(id, ct);
return Ok(result);
}

[HttpPut]
[Route("id")]
public async Task<IActionResult> UpdateAsync(long id, [FromBody] MailRequestViewModel model,
CancellationToken ct)
{
throw new NotImplementedException();
}

[HttpPost]
[HttpPut]
[Route("")]
public async Task<IActionResult> AddAsync([FromBody] MailRequestViewModel model, CancellationToken ct)
{
var result = await _senderService.AddAsync(model, ct);
return CreatedAtAction(nameof(GetByIdAsync), new {id = result.Id}, result);
}

#endregion

[HttpDelete]
[Route("id")]
public async Task<IActionResult> RemoveAsync(long id, CancellationToken ct)
{
throw new NotImplementedException();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace O2NextGen.Web.BFF.Core.Features.E_Sender.Models
{
public class MailRequestViewModel
{
public long Id { get; set; }
public string From { get; set; }
public string To { get; set; }
public string Body { get; set; }
public string Subject { get; set; }
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using O2NextGen.Web.BFF.Core.Config;
using O2NextGen.Web.BFF.Core.Features.E_Sender.Models;

namespace O2NextGen.Web.BFF.Core.Features.E_Sender.Services
{
public class ESenderService: IESenderService
{
private readonly HttpClient _httpClient;
private readonly IOptions<UrlsConfig> _config;
private string ApiVersion { get; } = "1.0";

public ESenderService(HttpClient httpClient, IOptions<UrlsConfig> config)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_config = config ?? throw new ArgumentNullException(nameof(config));
}
public async Task<MailRequestViewModel> AddAsync(MailRequestViewModel model, CancellationToken ct)
{
var response = await _httpClient.PostAsJsonAsync(_config.Value.ESenderUrl+"/api/emailsender",model,ct);
return await response.Content.ReadAsAsync<MailRequestViewModel>(ct);
}

public async Task<MailRequestViewModel> GetAsync(long id, CancellationToken ct)
{
var response = await _httpClient.GetAsync(_config.Value.ESenderUrl+"/api/emailsender",ct);
return await response.Content.ReadAsAsync<MailRequestViewModel>(ct);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using O2NextGen.Web.BFF.Core.Features.E_Sender.Models;

namespace O2NextGen.Web.BFF.Core.Features.E_Sender.Services
{
public interface IESenderService
{
Task<MailRequestViewModel> AddAsync(MailRequestViewModel model, CancellationToken ct);
Task<MailRequestViewModel> GetAsync(long id, CancellationToken ct);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;

namespace O2NextGen.Web.BFF.Core.Features.ESender
namespace O2NextGen.Web.BFF.Core.Features.E_Sender
{
[Route("api/features/e-sender/[controller]")]
public class VersionController : Controller
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;

namespace O2NextGen.Web.BFF.Core.Infrastructure
{
public class HttpClientAuthorizationDelegatingHandler
: DelegatingHandler
{
private readonly IHttpContextAccessor _httpContextAccesor;

public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContextAccesor)
{
_httpContextAccesor = httpContextAccesor;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var authorizationHeader = _httpContextAccesor.HttpContext
.Request.Headers["Authorization"];

if (!string.IsNullOrEmpty(authorizationHeader))
{
request.Headers.Add("Authorization", new List<string>() {authorizationHeader});
}

var token = await GetToken();

if (token != null)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
}

return await base.SendAsync(request, cancellationToken);
}

async Task<string> GetToken()
{
const string ACCESS_TOKEN = "access_token";

return await _httpContextAccesor.HttpContext
.GetTokenAsync(ACCESS_TOKEN);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,7 +13,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using O2NextGen.Web.BFF.Core.Config;
using O2NextGen.Web.BFF.Core.Features.E_Sender.Services;

namespace O2NextGen.Web.BFF.Core
{
Expand All @@ -24,7 +29,8 @@ public Startup(IConfiguration configuration)

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCustomMvc(Configuration);
services.AddApplicationServices();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand All @@ -33,14 +39,63 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseHttpsRedirection();

// app.UseHttpsRedirection();
// app.UseRouting();
//
// app.UseAuthorization();
app.UseMvc();
}
}

//Todo: will move to file
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions();
services.Configure<UrlsConfig>(configuration.GetSection("urls"));

var mvcBuilder = services.AddMvcCore();
mvcBuilder.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
mvcBuilder.AddJsonFormatters();
return services;
}

public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");

// var identityUrl = configuration.GetValue<string>("urls:e-sender");
// services.AddAuthentication(options =>
// {
// options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
// options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
//
// })
// .AddJwtBearer(options =>
// {
// options.Authority = identityUrl;
// options.RequireHttpsMetadata = false;
// options.Audience = "webshoppingagg";
// });

return services;
}
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
//register delegating handlers
// services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//register http services

services
.AddHttpClient<IESenderService, ESenderService>();

return services;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"Default": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"urls": {
"ESenderUrl": "https://e-sender"
}
}

0 comments on commit 3188b2b

Please sign in to comment.