Simplify Your REST API Responses with Milogo for Gin-Gonic

DDD
Release: 2024-11-27 10:27:09
Original
383 people have browsed it

Simplify Your REST API Responses with Milogo for Gin-Gonic

Gin-Gonic Middleware that implements fields selection pattern

At my company we use Go to build internal tools. Recently I worked in a REST API using gin-gonic, that required displaying a lot of data across many endpoints.

One must have feature in this type of scenario, is pagination, but and often overlooked pattern is partial response (a.k.a field selection). Which is also a very nice addition to filter out the amount of data in the responses of your web server.

What’s field selection?

Let us first make it clear what I mean with a field selection. Imagine that you have the following endpoint:

// > GET /api/products
[
  {
    "id": 1,
    "createdAt": "2024-18-11",
    "updatedAt": "2024-18-11",
    "code": "1",
    "price": {
      "amount": 100,
      "currency": "EUR"
    },
    "manufacturedBy": "myshop",
    "stock": 552,
    ...
  },
  ...
]
Copy after login

Using partial responses, clients can filter the output with a fields query parameter, e.g.

// > GET /api/products?fields=code,price
[
  {
    "code": "1",
    "price": {
      "amount": 100,
      "currency": "EUR"
    }
  },
  ...
]
Copy after login

Only the fields declared in the query parameter are returned. Reducing the payload size and saving bandwidth.

Introducing Milogo

I could not find any implementation available for this pattern, so I decided to create it myself, and that is how Milogo was born.
Milogo is a Gin middleware that processes API responses, filters out fields specified in the fields query parameter, and return only the requested data.

Some of the main features available:

  • Support for json objects and json arrays.
    So Milogo can filter fields for JSON responses that starts with an array of items or just with one single item.

  • Support for filtering out fields in nested json objects.
    Milogo also supports filtering nested JSON objects with the following format e.g. code,price(amount)

  • Support for json wrapped in another json.
    Sometimes the JSON responses are wrapped in another JSON object, Milogo support filtering the actual payload (see example wrapped):

// GET /products?fields=code,price(amount)
{
  "data": [
    {
      "code": 1,
      "price": {
        "amount": 100
      }
    },
    ...
  ],
  "_metadata": {
    ...
  }
}
Copy after login

Getting Started

As any gin middleware, Milogo is really easy to use and setup, you can follow the README in the github repository, but basically:

r := gin.Default()
r.Use(Milogo())
Copy after login

is enough to add Milogo middleware to your api.

In the previous example Milogo is applied to every single endpoint, but it is also possible to apply only to a group of endpoints, or just to an specific endpoint, e.g.

group := r.Group("/products", milogo.Milogo())
group.GET("", productsHandler)
Copy after login

In the previous example, only the /products group would have the Milogo’s middleware applied.

Conclusion

Milogo is designed to simplify REST API development and improve client-server interactions. Check out the GitHub repository for examples and documentation.

Feel free to reach out or contribute — let’s make REST APIs efficient together!

The above is the detailed content of Simplify Your REST API Responses with Milogo for Gin-Gonic. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template