Designbest Rest API: differenze tra le versioni
| Riga 124: | Riga 124: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | </div> | ||
* <div>Sostituito <code>swagger.json</code> con quello generato dal progetto '''Designbest REST API Core''' <code>https://apicore.dbdemo47.com/designbest/clients/swagger.json</code> stando attenti a modificare la stringa | * <div>Sostituito <code>swagger.json</code> con quello generato dal progetto '''Designbest REST API Core''' <code>https://apicore.dbdemo47.com/designbest/clients/swagger.json</code> <br/> | ||
stando attenti a modificare la stringa | |||
<pre> | <pre> | ||
"servers": [ | "servers": [ | ||
Versione delle 10:24, 28 lug 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.eyJ1c3IiOiJkZXNpZ25iZXN0cmVzdCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IkFkbWluIiwiZXhwIjo0OTA1MzIxMDk1LCJpc3MiOiJ3ZWJtb2JpbGkiLCJhdWQiOiJkZXNpZ25iZXN0In0.BT49K_oZyq9JW6mqNKHeGj3tGlIKvQorUlBcO7gdbvY
Test
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c3IiOiJkZXNpZ25iZXN0cmVzdCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IkFkbWluIiwiZXhwIjo0OTA1ODUyMTAxLCJpc3MiOiJ3ZWJtb2JpbGkiLCJhdWQiOiJkZXNpZ25iZXN0In0.v6mu9ShFdlIl1Xsq7Z4YYc09LgL53T2fYyibjmA4RGc
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".
Verb PUT e DELETE
A quanto pare esiste un modulo chiamato WebDAV su Windows Server che intercetta tutte le chiamate PUT e DELETE e non permette che arrivino alla RestAPI.
Il modulo deve essere disattivato dal sito tramite il web.config.
Creare nella solution un file web.release.config col seguente codice:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location>
<system.webServer>
<modules xdt:Transform="Insert">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" xdt:Transform="Insert" />
</handlers>
</system.webServer>
</location>
</configuration>
Andare su Properties e nella Build Action segnare None.
API Reference per clienti
https://apidocs.designbest.com/
Il progetto deriva da qui
https://github.com/swagger-api/swagger-ui
utilizzando la base html/js ho
- Cambiato le
favicon - Aggiunto il file
designbest-swagger.css - Modificato il file
index.htmlper aggiungere nell'<head>
<link rel="stylesheet" type="text/css" href="designbest-swagger.css" />
- Sostituito
swagger.jsoncon quello generato dal progetto Designbest REST API Corehttps://apicore.dbdemo47.com/designbest/clients/swagger.json
stando attenti a modificare la stringa
"servers": [
{
"url": "https://apicore.dbdemo47.com"
}
],
in
"servers": [
{
"url": "https://apicore.designbest.com"
}
],