@manhng

Welcome to my blog!

Api Best Practices

March 11, 2022 21:59

Api Best Practices (edit)

REST API Best Practices and Standards in 2022 (hevodata.com)

https://hevodata.com/learn/rest-api-best-practices/

REST API Best Practices – REST Endpoint Design Examples (freecodecamp.org)

https://www.freecodecamp.org/news/rest-api-best-practices-rest-endpoint-design-examples/

HTTP Method

Method Description
GET Used to retrieve a representation of a resource.
POST Used to create new new resources and sub-resources
PUT Used to update existing resources
PATCH Used to update existing resources
DELETE Used to delete existing resources

HTTP Status Code

Status Code range Meaning
100 – 199 Informational Responses.
For example, 102 indicates the resource is being processed
300 – 399 Redirects
For example, 301 means Moved permanently
400 – 499 Client-side errors
400 means bad request and 404 means resource not found
500 – 599 Server-side errors
For example, 500 means an internal server error

Common error HTTP status codes include:

  • 400 Bad Request – This means that client-side input fails validation.
  • 401 Unauthorized – This means the user isn’t not authorized to access a resource. It usually returns when the user isn’t authenticated.
  • 403 Forbidden – This means the user is authenticated, but it’s not allowed to access a resource.
  • 404 Not Found – This indicates that a resource is not found.
  • 500 Internal server error – This is a generic server error. It probably shouldn’t be thrown explicitly.
  • 502 Bad Gateway – This indicates an invalid response from an upstream server.
  • 503 Service Unavailable – This indicates that something unexpected happened on server side (It can be anything like server overload, some parts of the system failed, etc.).

13 Best Practices for Building RESTful APIs (viblo.asia)

https://viblo.asia/p/13-best-practices-for-building-restful-apis-Eb85o97WZ2G

1. OOP

OOP site:docs.microsoft.com

  1. Abstraction

  2. Inheritance

  3. Polymorphism

  4. Encapsulation

Classes, structs, and records | Microsoft Docs

https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/

2. SOLID

SOLID site:docs.microsoft.com

  1. Single Responsibility principle

  2. Open/closed principle

  3. Liskov substitution principle

  4. Interface Segregation principle

  5. Dependency Inversion principle

Here are some relate articles you can refer to them:

SOLID With .Net Core

Solid Principles with C# .NET Core with Real-World Usage

How to apply SOLID principles with practical examples in C#

Sample: order-fullfillment-solid-practice

Designing the microservice application layer and Web API | Microsoft Docs

https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/microservice-application-layer-web-api-design

3. Custom Wrapper

ASP.NET Core and Web API: A Custom Wrapper for Managing Exceptions and Consistent Responses (vmsdurano.com)

https://vmsdurano.com/asp-net-core-and-web-api-a-custom-wrapper-for-managing-exceptions-and-consistent-responses/

NuGet: Swashbuckle

Code Samples

//Global.asax.cs
GlobalConfiguration.Configuration.Filters.Add(new CustomAuthorizeFilter());

//WebApiConfig.cs
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

//WebApiConfig.cs
config.EnableCors();

//Controllers
[EnableCors(origins: "*", headers: "*", methods: "*", SupportsCredentials = true)]
public class BaseApiController : ApiController { ... }

//BaseApiController.cs
protected string GetModelStateErrorMessage()
{
var sb = new StringBuilder();
foreach (ModelState modelState in ModelState.Values)
{
foreach (ModelError error in modelState.Errors)
{
sb.AppendLine(error.ErrorMessage);
}
}
return sb.ToString();
}

Categories

Recent posts