Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutomaticApi

AutomaticApi can automatically generate APIs based on services, just define a REST API interface.

Installation

Package NuGet Stable Downloads
AutomaticApi AutomaticApi AutomaticApi
AutomaticApi.Abstraction AutomaticApi.Abstraction AutomaticApi.Abstraction

Quick start

1. Write service code

public class DemoService
{
    public string Get()
    {
        return "Hello AutomaticApi";
    }
}

2. Define the exposed REST API interface we want

public interface IDemoAService : IAutomaticApi
{
    /// <summary>
    /// DemoA api
    /// </summary>
    /// <returns></returns>
    string Get();
}

We can also define another REST API interface.

public interface IDemoBService : IAutomaticApi
{
    /// <summary>
    /// DemoB api
    /// </summary>
    /// <returns></returns>
    string Get();
}

And implement these interfaces.

public class DemoService : IDemoAService, IDemoBService
...

3. Configure Services

public void ConfigureServices(IServiceCollection services)
{
    services.AddAutomaticApi(op =>
    {
        op.AddApi<IDemoAService, DemoService>(); //only generate IDemoAService

        op.AddApi<DemoService>(); //Generate all api interface in DemoService 

        op.AddAssembly(Assembly.GetEntryAssembly()); //Generate all api interface in Assembly
    });
}

What a REST api interface?

You can totally use it as a controller.

public interface IDemoService : IAutomaticApi
{
    /// <summary>
    /// DemoB api
    /// </summary>
    /// <returns></returns>
    [Authorize]
    [HttpPost]
    [Route("...")]
    string UpdateAsync(Guid id, [FromBody] RequestModel model);
}

However, [HttpPost] [Route] usually doesn't need to be defined. AutomaticApi will generates Route and HttpMethod based on the Method name.

As we all know, some [Attrbute] may can't put it on interface like this.

[Authorize]
public interface IDemoService : IAutomaticApi
...

We can do some customization.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class MyAuthorizeAttribute : AuthorizeAttribute
{
}

If you think this component can help you, please give me a star.

About

AutomaticApi can automatically generate APIs based on services, just define a REST API interface.

Topics

Resources

Stars

10 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages