Generate code for different requests types from config

57 views Asked by At

I have an app that makes a lot of requests to different urls. Each of them have different request & response structure.

Configurations of the requests types located in Mysql databse.

It contains next data:

  • URL
  • Method
  • Query params
  • Body params
  • Headers
  • Response structure

So while before I was using Node.js for this things it was easy to make solution for this.

But with Go I see only way in using reflect package for this. But reflect harms performance of the app.

Is there a simple way to generate code for this?

Example of request config:

{
  id: 1,
  name: "Req1",
  url: "http://example.com",
  query: [
    {name: "user_id", source: "user", value: "id" },
    {name: "user_ip", source: "user", value: "ip" },
    {name: "token", source: "const", value: "xxx" },
  ],
  headers: [
   {name: "Accept-Language", source: "user", value: "language"}
  ],
  body: [
    {"name": "user.ua", "user", "ua"}
  ]
}

User example:

{
  ip: "127.0.0.1",
  id: "123",
  ua: "User Agent...",
  "language": "en"
}

And in the output should be made next request:

URL: http://example.com?user_id=123&user_ip=127.0.0.1&token=xxx
Headers:
{
  Accept-Language: en
}
Body:
{
  user: {
    ua: "User Agent..."
  }
}

In body might be used path of the param.

Is there a tool for automatically generating this type of code?

0

There are 0 answers