Piranha.core: Is there restAPI support to use Piranha as Headless CMS or from an app? And Swagger support?

Created on 27 Apr 2020  路  11Comments  路  Source: PiranhaCMS/piranha.core

Is there restAPI support to use Piranha as Headless CMS or from an app?

question

Most helpful comment

Version 8.2 will be released in a day or two which will enable you to use Swagger

All 11 comments

Yes, we provide a default package that you can, but it鈥檚 extremely easy to publish your own tailor made api if you take a look at the implementation.

https://github.com/PiranhaCMS/piranha.core/blob/master/core/Piranha.WebApi/PageApiController.cs#L45

Regards

Is鈥檛 installed by default that module? Is there Swagger by default?

Like most stuff in Piranha you install it separately if you want it! https://www.nuget.org/packages/Piranha.WebApi/. Endpoints should be decorated for Swagger, you can add it by adding the package Swashbuckle.AspNetCore and setting it up in Startup.cs

services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo { Title = "PiranhaCMS API", Version = "v1" });
    options.CustomSchemaIds(x => x.FullName);
});

Regards

Maybe some routing problem...

Fetch error
Internal Server Error /swagger/v1/swagger.json

System.NotSupportedException: Ambiguous HTTP method for action - MvcWeb.Controllers.CmsController.Archive (MvcWeb). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0

@biapar this looks like the code generated by one of the Piranha templates. I think you can solve it by adding [HttpGet] above the Archive method in the CmsController.
You might run into the same error message for each of the methods in this controller.

Why not put httpget by default?

I think that there is a problem on Swagger init.

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.NotSupportedException: Ambiguous HTTP method for action - Piranha.AspNetCore.Identity.Controllers.RoleController.List (Piranha.AspNetCore.Identity). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Piranha.AspNetCore.SitemapMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service) in C:\Users\biagioparuolo\Downloads\piranha.core-8.1-sr1\piranha.core-8.1-sr1\core\Piranha.AspNetCore\SitemapMiddleware.cs:line 86   at Piranha.AspNetCore.StartPageMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service) in C:\Users\biagioparuolo\Downloads\piranha.core-8.1-sr1\piranha.core-8.1-sr1\core\Piranha.AspNetCore\StartPageMiddleware.cs:line 111
   at Piranha.AspNetCore.ArchiveMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service) in C:\Users\biagioparuolo\Downloads\piranha.core-8.1-sr1\piranha.core-8.1-sr1\core\Piranha.AspNetCore\ArchiveMiddleware.cs:line 72   at Piranha.AspNetCore.PostMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service) in C:\Users\biagioparuolo\Downloads\piranha.core-8.1-sr1\piranha.core-8.1-sr1\core\Piranha.AspNetCore\PostMiddleware.cs:line 99
   at Piranha.AspNetCore.PageMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service) in C:\Users\biagioparuolo\Downloads\piranha.core-8.1-sr1\piranha.core-8.1-sr1\core\Piranha.AspNetCore\PageMiddleware.cs:line 111
   at Piranha.AspNetCore.AliasMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service) in C:\Users\biagioparuolo\Downloads\piranha.core-8.1-sr1\piranha.core-8.1-sr1\core\Piranha.AspNetCore\AliasMiddleware.cs:line 50
   at Piranha.AspNetCore.ApplicationMiddleware.Invoke(HttpContext context, IApi api, IApplicationService service) in C:\Users\biagioparuolo\Downloads\piranha.core-8.1-sr1\piranha.core-8.1-sr1\core\Piranha.AspNetCore\ApplicationMiddleware.cs:line 50
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

This is in startup.cs:

```
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApi api)
{
if ( env.IsDevelopment() )
{
app.UseDeveloperExceptionPage();
}

        App.Init(api);

        // Configure cache level
        App.CacheLevel = Piranha.Cache.CacheLevel.Full;

        // Build content types
        var pageTypeBuilder = new Piranha.AttributeBuilder.PageTypeBuilder(api)
            .AddType(typeof(Models.BlogArchive))
            .AddType(typeof(Models.StandardPage))
            .AddType(typeof(Models.TeaserPage))
            .Build()
            .DeleteOrphans();
        var postTypeBuilder = new Piranha.AttributeBuilder.PostTypeBuilder(api)
            .AddType(typeof(Models.BlogPost))
            .Build()
            .DeleteOrphans();
        var siteTypeBuilder = new Piranha.AttributeBuilder.SiteTypeBuilder(api)
            .AddType(typeof(Models.StandardSite))
            .Build()
            .DeleteOrphans();

        /**
         *
         * Test another culture in the UI
         *
        var cultureInfo = new System.Globalization.CultureInfo("en-US");
        System.Globalization.CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
        System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
         */

        // Register middleware
        app.UseStaticFiles();
        app.UsePiranha();
        app.UseRouting();






        app.UseAuthentication();
        app.UseAuthorization();
        app.UsePiranhaIdentity();
        app.UsePiranhaManager();
        app.UsePiranhaTinyMCE();



        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Piranha V1");
        });


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            endpoints.MapPiranhaManager();
        });

        Seed.RunAsync(api).GetAwaiter().GetResult();
    }

```

Ah. The fixes that would solve these issues were actually committed to master in the same PR that added the example code for Swagger. https://github.com/PiranhaCMS/piranha.core/pull/1118 This means it will be available in the upcoming minor release.

Version 8.2 will be released in a day or two which will enable you to use Swagger

Thx.

Yes, we provide a default package that you can, but it鈥檚 extremely easy to publish your own tailor made api if you take a look at the implementation.

https://github.com/PiranhaCMS/piranha.core/blob/master/core/Piranha.WebApi/PageApiController.cs#L45

Regards

Hello,
I used the controller, but AllowAnonymousAccess property in the Module class is marked as Internal and can't be used when we are installing the NuGet Package.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stefanolsenn picture stefanolsenn  路  3Comments

w0ns88 picture w0ns88  路  6Comments

jcphlux picture jcphlux  路  4Comments

OmidID picture OmidID  路  3Comments

baibhavs picture baibhavs  路  3Comments