Quick Start

This Quick Start shows to get up and running with API Framework.

1. Create App Using Web Api template
dotnet new webapi
2. Add API Framework
dotnet add package Weikio.ApiFramework.AspNetCore.Starterkit -v 1.0.0-alpha.0.82
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddApiFrameworkStarterKit();
}
3. Add Plugins
dotnet add package Weikio.ApiFramework.Plugins.SqlServer -v 1.1.0-alpha.0.8
dotnet add package Weikio.ApiFramework.Plugins.OpenApi -v 1.0.0-alpha.0.23

The project file should look like the following at this point:

<ItemGroup>
  <PackageReference Include="Weikio.ApiFramework.AspNetCore.Starterkit" Version="1.0.0-alpha.0.82" />
  <PackageReference Include="Weikio.ApiFramework.Plugins.OpenApi" Version="1.0.0-alpha.0.23" />
  <PackageReference Include="Weikio.ApiFramework.Plugins.SqlServer" Version="1.1.0-alpha.0.8" />
</ItemGroup>
4. Create API using delegate
Func<ILogger<Startup>, string, string> helloApi = (logger, name) =>
{
    logger.LogInformation("Running the API using parameter {Name}", name);
    return $"Hello {name}!";
};
5. Create API using POCO
public class CalculatorApi
{
    public int Configuration { get; set; }
    public int Sum(int x, int y)
    {
        return x + y + Configuration;
    }
}
6. Register the available APIs
services.AddApiFrameworkStarterKit()
    .AddApi(helloApi)
    .AddApi<CalculatorApi>()
    .AddSqlServer()
    .AddOpenApi();
7. Create Endpoints
services.AddApiFrameworkStarterKit()
    .AddApi(helloApi)
    .AddApi<CalculatorApi>()
    .AddSqlServer()
    .AddOpenApi()
    .AddEndpoint<CalculatorApi>("/mycal", 15)
    .AddEndpoint<CalculatorApi>("/second", 100)
    .AddEndpoint<CalculatorApi>("/third", 0)
    .AddEndpoint("/sqlserver", ("Weikio.ApiFramework.Plugins.SqlServer", Version.Parse("1.1.0.0")), new SqlServerOptions()
    {
        ConnectionString =
            "Server=tcp:adafydevtestdb001.database.windows.net,1433;User ID=docs;Password=3h1@*6PXrldU4F95;Integrated Security=false;Initial Catalog=adafyweikiodevtestdb001;"
    })
    .AddEndpoint("/sqlserver_products",  ("Weikio.ApiFramework.Plugins.SqlServer", Version.Parse("1.1.0.0")), new SqlServerOptions()
    {
        ConnectionString =
            "Server=tcp:adafydevtestdb001.database.windows.net,1433;User ID=docs;Password=3h1@*6PXrldU4F95;Integrated Security=false;Initial Catalog=adafyweikiodevtestdb001;",
        Tables = new[] { "Product*" },
    })
    .AddEndpoint("/pets", "Weikio.ApiFramework.Plugins.OpenApi",
        new ApiOptions() { SpecificationUrl = "https://petstore.swagger.io/v2/swagger.json", IncludeHttpMethods = new[] { "GET" } });

var endpointDefinition = new EndpointDefinition("/pets_writeonly", "Weikio.ApiFramework.Plugins.OpenApi", 
    new ApiOptions()
    {
        SpecificationUrl = "https://petstore.swagger.io/v2/swagger.json", ExcludeHttpMethods = new[] { "GET" }
    });

services.AddSingleton(endpointDefinition);
8. View the result

Open the Swagger:

https://localhost:5001/swagger/index.html

9. Test the endpoints

The full source code
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Weikio.ApiFramework.Abstractions;
using Weikio.ApiFramework.ApiProviders.PluginFramework;
using Weikio.ApiFramework.AspNetCore;
using Weikio.ApiFramework.AspNetCore.StarterKit;
using Weikio.ApiFramework.Core.Extensions;
using Weikio.ApiFramework.Plugins.OpenApi;
using Weikio.ApiFramework.Plugins.SqlServer;
using Weikio.ApiFramework.Plugins.SqlServer.Configuration;

namespace quickstart
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Func<ILogger<Startup>, string, string> helloApi = (logger, name) =>
            {
                logger.LogInformation("Running the API using parameter {Name}", name);
                return $"Hello {name}!";
            };
            
            services.AddControllers();

            services.AddApiFrameworkStarterKit()
                .AddApi(helloApi)
                .AddApi<CalculatorApi>()
                .AddSqlServer()
                .AddOpenApi()
                .AddEndpoint<CalculatorApi>("/mycal", 15)
                .AddEndpoint<CalculatorApi>("/second", 100)
                .AddEndpoint<CalculatorApi>("/third", 0)
                .AddEndpoint("/sqlserver", ("Weikio.ApiFramework.Plugins.SqlServer", Version.Parse("1.1.0.0")), new SqlServerOptions()
                {
                    ConnectionString =
                        "Server=tcp:adafydevtestdb001.database.windows.net,1433;User ID=docs;Password=3h1@*6PXrldU4F95;Integrated Security=false;Initial Catalog=adafyweikiodevtestdb001;"
                })
                .AddEndpoint("/sqlserver_products",  ("Weikio.ApiFramework.Plugins.SqlServer", Version.Parse("1.1.0.0")), new SqlServerOptions()
                {
                    ConnectionString =
                        "Server=tcp:adafydevtestdb001.database.windows.net,1433;User ID=docs;Password=3h1@*6PXrldU4F95;Integrated Security=false;Initial Catalog=adafyweikiodevtestdb001;",
                    Tables = new[] { "Product*" },
                })
                .AddEndpoint("/pets", "Weikio.ApiFramework.Plugins.OpenApi",
                    new ApiOptions() { SpecificationUrl = "https://petstore.swagger.io/v2/swagger.json", IncludeHttpMethods = new[] { "GET" } });
            
            var endpointDefinition = new EndpointDefinition("/pets_writeonly", "Weikio.ApiFramework.Plugins.OpenApi", 
                new ApiOptions()
                {
                    SpecificationUrl = "https://petstore.swagger.io/v2/swagger.json", ExcludeHttpMethods = new[] { "GET" }
                });

            services.AddSingleton(endpointDefinition);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
    
    public class CalculatorApi
    {
        public int Configuration { get; set; }
        public int Sum(int x, int y)
        {
            return x + y + Configuration;
        }
    }
}
GitHub & More Samples

The code is available from GitHub: https://github.com/weikio/ApiFramework.Samples/tree/main/quickstart

For more samples, please see: https://github.com/weikio/ApiFramework.Samples