Code Based Configuration

API Framework can be configured through code.

Adding Plugins using Extension Methods

Many of the plugins provided by the Weik.io team contain extensions methods which make it easy to include a plugin into your App. The extension methods are accessible using a fluent builder syntax. After adding a package reference to the plugin, the API (and the endpoints) can be added through the ConfigureServices.

For example, here's how the [https://www.nuget.org/packages/weikio.apiframework.plugins.sqlserver](SQL Server plugin) can be configured through code:

services.AddApiFrameworkStarterKit()
    .AddSqlServer("/eshop",
        new SqlServerOptions()
        {
            // This provides a read only access to the db used in the documentations
            ConnectionString =
                "Server=tcp:adafydevtestdb001.database.windows.net,1433;User ID=docs;Password=3h1@*6PXrldU4F95;Integrated Security=false;Initial Catalog=adafyweikiodevtestdb001;",
            Tables = new[] { "Product*", "Sales*" }
        });

For an another example, here's how the [https://www.nuget.org/packages/weikio.apiframework.plugins.openapi](OpenAPI plugin) can be configured through the extension method provided by the plugin:

services.AddApiFrameworkStarterKit()
    .AddOpenApi("/petstore",
        new ApiOptions()
        {
            SpecificationUrl = "https://petstore3.swagger.io/api/v3/openapi.json",
            ApiUrl = "https://petstore3.swagger.io/api/v3",
            IncludeHttpMethods = new[] { "GET", "POST" }
        });

And here's how the commands can be chained using the fluent builder:

services.AddApiFrameworkStarterKit()
    .AddSqlServer("/eshop",
        new SqlServerOptions()
        {
            // This provides a read only access to the db used in the documentations
            ConnectionString =
                "Server=tcp:adafydevtestdb001.database.windows.net,1433;User ID=docs;Password=3h1@*6PXrldU4F95;Integrated Security=false;Initial Catalog=adafyweikiodevtestdb001;",
            Tables = new[] { "Product*", "Sales*" }
        })
    .AddOpenApi("/petstore",
        new ApiOptions()
        {
            SpecificationUrl = "https://petstore3.swagger.io/api/v3/openapi.json",
            ApiUrl = "https://petstore3.swagger.io/api/v3",
            IncludeHttpMethods = new[] { "GET", "POST" }
        });

Not all plugins provide extensions methods. In these cases the plugin's assembly should be manually added as an API.

Adding Assemblies

It is possible to add APIs by providing the assembly contain the API or APIs to API Framework. Here's an example of a scenario where application references the SQL Server plugin package but the extension method for the plugin is not used:

services.AddApiFrameworkStarterKit()
    .AddApi(typeof(SqlServerOptions).Assembly)
    .AddEndpoint("/mysql", ("Weikio.ApiFramework.Plugins.SqlServer", "2.0.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*", "Sales*" }
    });

It is possible to provide the assembly using a path:

services.AddApiFrameworkStarterKit()
    .AddApi(@"C:\app\plugin\Weikio.ApiFramework.Plugins.SqlServer.dll")
    .AddEndpoint("/mysql", ("Weikio.ApiFramework.Plugins.SqlServer", "2.0.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*", "Sales*" }
    });

In addition to configuring API Framework by using assemblies, also types can be used.

Adding Types

APIs can be added by providing a type. Given that you have a class called Calculator and you aim to use that as an API, you can use the following syntax:

services.AddApiFrameworkStarterKit()
    .AddApi<Calculator>("/mycalc");

Without the fluent syntax

It is possible to configure the API Framework without using the fluent syntax. Here's an example where APIs and Endpoints are created using the IServiceCollection:

services.AddApiFrameworkStarterKit();

services.Configure<ApiPluginOptions>(options =>
{
    options.ApiPluginAssemblies.Add(typeof(SqlServerOptions).Assembly);
});

services.AddSingleton(provider =>
{
    var def = new EndpointDefinition("/mysql", ("Weikio.ApiFramework.Plugins.SqlServer", "2.0.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*", "Sales*"}
        });
    return def;
});