Merge pull request 'Versioning' (#21) from Versioning into master
continuous-integration/drone/push Build is passing Details

Reviewed-on: #21
ConsoleClient
Pierre FERREIRA 2 years ago
commit 1a753c2926

@ -9,6 +9,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

@ -13,7 +13,9 @@ using System.Xml.Linq;
namespace API_LoL.Controllers
{
[Route("api/[controller]")]
//[Route("api/[controller]")]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class ChampionsController : ControllerBase
{

@ -0,0 +1,108 @@
using Microsoft.AspNetCore.Mvc;
using Model;
using StubLib;
using DTO;
using DTO.Mapper;
using System.CodeDom.Compiler;
namespace API_LoL.Controllers
{
[ApiVersion("2.0")]
[ApiVersion("2.2")]
[Route("api/v{version:apiVersion}/versioned")]
[ApiController]
public class ChampionsControllerVersioned : ControllerBase
{
public ChampionsControllerVersioned(IDataManager Manager)
{
this.ChampionsManager = Manager.ChampionsMgr;
}
private IChampionsManager ChampionsManager;
// GET api/<ChampionController>/5
//[HttpGet]
//public async Task<IActionResult> Get(String? name = null, String? skill = null, String? characteristic = null, int index = 0, int size = 10)
//{
// if (size - index > 10)
// {
// return BadRequest();
// }
// if (!string.IsNullOrEmpty(name))
// {
// var list = await ChampionsManager.GetItemsByName(name, index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
// else if (!string.IsNullOrEmpty(skill))
// {
// var list = await ChampionsManager.GetItemsBySkill(skill, index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
// else if (!string.IsNullOrEmpty(characteristic))
// {
// var list = await ChampionsManager.GetItems(index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
// else
// {
// var list = await ChampionsManager.GetItems(index, size);
// if (list.Count() != 0)
// {
// return Ok(list.Select(champion => champion?.ToDTO()));
// }
// else { return NoContent(); }
// }
//}
//// POST api/<ChampionController>
//[HttpPost]
//public async Task<IActionResult> Post(ChampionDTO champion)
//{
// if (champion == null)
// {
// return UnprocessableEntity();
// }
// else
// {
// await ChampionsManager.AddItem(champion.ToChampion());
// return CreatedAtAction("Post", champion);
// }
//}
//// PUT api/<ChampionController>/5
//[HttpPut("{id}")]
//public void Put(int id, [FromBody] string value)
//{
//}
//// DELETE api/<ChampionController>/5
//[HttpDelete("{id}")]
//public void Delete(int id)
//{
//}
///---------- Versioning ----------///
[HttpGet, MapToApiVersion("2.0")]
public string GetThatOnlySayHello() => "Hello v2.0!";
//! FIXME : not working, mais avec la version 2.0 ca marche !
[HttpGet, MapToApiVersion("2.2")]
public string GetThatOnlySayHelloV2() => "Hello but i'm from v2.2!";
}
}

@ -1,24 +1,65 @@
using API_LoL;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Versioning;
using Model;
using StubLib;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
//Versioning
///NOT WORKING WHEN CHANGING VERSIONS :
/// voir sur https://blog.christian-schou.dk/how-to-use-api-versioning-in-net-core-web-api/ rubrique "Configure SwaggerOptions"
/// (mais requiere l'injection de dépendance).
/// Sinon, code plus simple disponible par le prof
// Add ApiExplorer to discover versions
builder.Services.AddVersionedApiExplorer(setup =>
{
setup.GroupNameFormat = "'v'VVV";
setup.SubstituteApiVersionInUrl = true;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
builder.Services.AddApiVersioning(o => o.ApiVersionReader = new UrlSegmentApiVersionReader());
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddScoped<IDataManager,StubData>();
var app = builder.Build();
var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(options =>
{
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json",
description.GroupName.ToUpperInvariant());
}
});
}
app.UseHttpsRedirection();

@ -0,0 +1,61 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace API_LoL
{
public class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider _provider;
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
{
_provider = provider;
}
/// <summary>
/// Configure each API discovered for Swagger Documentation
/// </summary>
/// <param name="options"></param>
public void Configure(SwaggerGenOptions options)
{
// add swagger document for every API version discovered
foreach (var description in _provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateVersionInfo(description));
}
}
/// <summary>
/// Configure Swagger Options. Inherited from the Interface
/// </summary>
/// <param name="name"></param>
/// <param name="options"></param>
public void Configure(string name, SwaggerGenOptions options)
{
Configure(options);
}
/// <summary>
/// Create information about the version of the API
/// </summary>
/// <param name="description"></param>
/// <returns>Information about the API</returns>
private OpenApiInfo CreateVersionInfo(ApiVersionDescription desc)
{
var info = new OpenApiInfo()
{
Title = ".NET Core (.NET 6) Web API",
Version = desc.ApiVersion.ToString()
};
if (desc.IsDeprecated)
{
info.Description += " This API version has been deprecated. Please use one of the new APIs available from the explorer.";
}
return info;
}
}
}
Loading…
Cancel
Save