Coder Social home page Coder Social logo

instantapis's Introduction

InstantAPIs

Nuget GitHub branch checks state GitHub last commit GitHub contributors

This article contains two different ways to get an instant API:

  • An API based on a DbContext, it will generate the routes it needs given a database class.
  • An API based on a JSON file.

DbContext based API

A proof-of-concept library that generates Minimal API endpoints for an Entity Framework context.

For a given Entity Framework context, MyContext

public class MyContext : DbContext 
{
    public MyContext(DbContextOptions<MyContext> options) : base(options) {}

    public DbSet<Contact> Contacts => Set<Contact>();
    public DbSet<Address> Addresses => Set<Address>();

}

We can generate all of the standard CRUD API endpoints using this syntax in Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSqlite<MyContext>("Data Source=contacts.db");

var app = builder.Build();

app.MapInstantAPIs<MyContext>();

app.Run();

Now we can navigate to /api/Contacts and see all of the Contacts in the database. We can filter for a specific Contact by navigating to /api/Contacts/1 to get just the first contact returned. We can also post to /api/Contacts and add a new Contact to the database. Since there are multiple DbSet, you can make the same calls to /api/Addresses.

Demo

Check out Fritz giving a demo, showing the advantage of InstantAPIs on YouTube: https://youtu.be/vCSWXAOEpBo

A JSON based API

An API will be generated based on JSON file, for now it needs to be named mock.json.

A typical content in mock.json looks like so:

{
  "products" : [{
    "id": 1,
    "name": "pizza"
  }, {
    "id": 2,
    "name": "pineapple pizza"
  }, {
    "id": 3,
    "name": "meat pizza"
  }],
  "customers" : [{
    "id": 1,
    "name": "customer1"
  }]
  
}

The above JSON will create the following routes:

HTTP Verb Endpoint
GET /products
GET /products/{id}
POST /products
DELETE /products/{id}
GET /customers
GET /customers/{id}
POST /customers
DELETE /customers/{id}

Demo

To use this, do the following:

  1. Create a new minimal API, if you don't already have one:

    dotnet new web -o DemoApi -f net6.0
    cd DemoApi 
  2. Add the NuGet package for Fritz.InstantAPIs:

    dotnet add package Fritz.InstantAPIs --prerelease
  3. In Program.cs, add the following namespace:

    using Mock;
  4. Create a file mock.json and give it for example the following content:

    {
       "products" : [{
         "id": 1,
         "name": "pizza"
       }]
     }
  5. Now add the following code for the routes to be created:

    app.UseJsonRoutes();

Here's an example program:

using Mock;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.UseJsonRoutes();
app.Run();

Coming features

Support for:

Community

This project is covered by a code of conduct that all contributors must abide by. Contributions are welcome and encouraged..

instantapis's People

Contributors

csharpfritz avatar timheuer avatar jamesmontemagno avatar softchris avatar christiannagel avatar verbedr avatar mkdotcs avatar mojjozz avatar mpaulosky avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.