Designbest Rest API: differenze tra le versioni
Da Webmobili Wiki.
| Riga 15: | Riga 15: | ||
Nel file <code>Program.cs</code> configurare così | Nel file <code>Program.cs</code> configurare così | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
const string adminDocName = "v1"; | |||
const string clientsDocName = "clients"; | |||
builder.Services.AddSwaggerGen( | builder.Services.AddSwaggerGen( | ||
opt => { | opt => { | ||
opt.SwaggerDoc( | // SwaggerDoc Admin | ||
opt.SwaggerDoc(adminDocName, new() { | |||
Title = "Designbest REST API", | Title = "Designbest REST API", | ||
Description = "Designbest REST API", | Description = "La Designbest REST API fornisce un accesso completo e flessibile al portale leader mondiale dell'arredamento.", | ||
Version = "v1", | Version = "v1", | ||
Contact = new | Contact = new() { Email = "info@designbest.com", Name = "Webmobili s.r.l." } | ||
}); | }); | ||
// SwaggerDoc Client | |||
opt.SwaggerDoc("clients", new() { | |||
Title = "Designbest REST API for clients", | |||
Description = "La Designbest REST API fornisce un'interfaccia di accesso dati completa e flessibile sul portale leader mondiale dell'arredamento.", | |||
Version = "v1", | |||
Contact = new() { Email = "info@designbest.com", Name = "Webmobili s.r.l." } | |||
}); | |||
var tokenService = new TokenService(builder.Configuration, builder.Environment); | var tokenService = new TokenService(builder.Configuration, builder.Environment); | ||
const string securitySchemeName = JwtBearerDefaults.AuthenticationScheme; | |||
opt.AddSecurityDefinition(securitySchemeName, new() { | |||
In = ParameterLocation.Header, | In = ParameterLocation.Header, | ||
Description = "Please enter a valid token like " + tokenService.GetToken()?.AccessToken, | Description = "Please enter a valid token like " + tokenService.GetToken()?.AccessToken, | ||
| Riga 32: | Riga 43: | ||
Scheme = "Bearer" | Scheme = "Bearer" | ||
}); | }); | ||
opt.AddSecurityRequirement(new | opt.AddSecurityRequirement(new() { | ||
{ | { | ||
new | new() { | ||
Reference = new | Reference = new() { | ||
Type=ReferenceType.SecurityScheme, | Type = ReferenceType.SecurityScheme, | ||
Id= | Id = securitySchemeName | ||
} | |||
}, | }, | ||
Array.Empty<string>() | Array.Empty<string>() | ||
| Riga 53: | Riga 64: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
app.UseSwagger(options => { | app.UseSwagger(options => { | ||
options.RouteTemplate = "designbest/{documentName}/swagger.json"; | |||
}); | }); | ||
if (app.Environment.IsDevelopment() || app.Environment.IsStaging()) { | |||
app.UseSwaggerUI(options => { | |||
options.DocumentTitle = "Designbest REST API"; | |||
options.InjectStylesheet("/css/designbest-swagger.css"); | |||
options.InjectJavascript("/js/swagger-custom.js"); | |||
options.RoutePrefix = "designbest"; | |||
options.SwaggerEndpoint($"/designbest/{adminDocName}/swagger.json", "Designbest REST API"); // "v1" corrisponde al primo parametro di SwaggerDoc("v1", new() { | |||
options.SwaggerEndpoint($"/designbest/{clientsDocName}/swagger.json", "Designbest REST API for clients"); // "clients" corrisponde al primo parametro di SwaggerDoc("client", new() { | |||
}); | |||
} | |||
// ... | // ... | ||
app. | app.MapStaticAssets(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Versione delle 16:39, 8 mag 2025
Access Token
Token valido 100 anni
Per servizi che hanno bisogno di un token fisso e non possono fare la richiesta ogni volta (es. Zapier),
ecco un token che dura 100 anni:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c3IiOiJkZXNpZ25iZXN0cmVzdCIsImV4cCI6NDg4Nzk0MjU5OSwiaXNzIjoid2VibW9iaWxpIiwiYXVkIjoiZGVzaWduYmVzdCJ9.Kqvuli0CAdaX5-Qn-Fxx9i01IWTjjqktwrXZOGdfZ5Q
Test
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c3IiOiJkZXNpZ25iZXN0cmVzdCIsImV4cCI6MTczMjI2OTEwMCwiaXNzIjoid2VibW9iaWxpIiwiYXVkIjoiZGVzaWduYmVzdCJ9.Vbd9m67qhx_T0DgsmSnH5Zn8Vz6j7Dxpu1vsEFyFSSk
Configurazione di Swagger
Nel file Program.cs configurare così
const string adminDocName = "v1";
const string clientsDocName = "clients";
builder.Services.AddSwaggerGen(
opt => {
// SwaggerDoc Admin
opt.SwaggerDoc(adminDocName, new() {
Title = "Designbest REST API",
Description = "La Designbest REST API fornisce un accesso completo e flessibile al portale leader mondiale dell'arredamento.",
Version = "v1",
Contact = new() { Email = "info@designbest.com", Name = "Webmobili s.r.l." }
});
// SwaggerDoc Client
opt.SwaggerDoc("clients", new() {
Title = "Designbest REST API for clients",
Description = "La Designbest REST API fornisce un'interfaccia di accesso dati completa e flessibile sul portale leader mondiale dell'arredamento.",
Version = "v1",
Contact = new() { Email = "info@designbest.com", Name = "Webmobili s.r.l." }
});
var tokenService = new TokenService(builder.Configuration, builder.Environment);
const string securitySchemeName = JwtBearerDefaults.AuthenticationScheme;
opt.AddSecurityDefinition(securitySchemeName, new() {
In = ParameterLocation.Header,
Description = "Please enter a valid token like " + tokenService.GetToken()?.AccessToken,
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer"
});
opt.AddSecurityRequirement(new() {
{
new() {
Reference = new() {
Type = ReferenceType.SecurityScheme,
Id = securitySchemeName
}
},
Array.Empty<string>()
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
opt.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFile));
}
);
e di sotto, per poter cambiare il nome dell'endpoint della documentazione in /designbest e personalizzare la grafica
app.UseSwagger(options => {
options.RouteTemplate = "designbest/{documentName}/swagger.json";
});
if (app.Environment.IsDevelopment() || app.Environment.IsStaging()) {
app.UseSwaggerUI(options => {
options.DocumentTitle = "Designbest REST API";
options.InjectStylesheet("/css/designbest-swagger.css");
options.InjectJavascript("/js/swagger-custom.js");
options.RoutePrefix = "designbest";
options.SwaggerEndpoint($"/designbest/{adminDocName}/swagger.json", "Designbest REST API"); // "v1" corrisponde al primo parametro di SwaggerDoc("v1", new() {
options.SwaggerEndpoint($"/designbest/{clientsDocName}/swagger.json", "Designbest REST API for clients"); // "clients" corrisponde al primo parametro di SwaggerDoc("client", new() {
});
}
// ...
app.MapStaticAssets();
RouteTemplate,RoutePrefixeSwaggerEndpoint()sono collegati e servono a cambiare il nome default per raggiungere la url della documentazioneInjectStylesheet()eInjectJavascript()servono a personalizzare la grafica.
Perché questi funzionino è necessario attivareapp.UseStaticFiles();e creare la cartellawwwrootche conterrà le cartellecssejs- Nel file
Properties/launchSettings.jsonmodificare i campi launchUrl con la stringa"swagger"=> "designbest".