Laravel - Change pagination root url

Viewed 478

I am building an API Gateway and I am having a small problem returning the pagination urls from a microservice to the API Gateway.

This is the current structure of my API Gateway:

Microservice Arquitecture

When I call the microservice, I can easily pass the paging parameters using the request data:

HTTP::get('http://api.billing.microservice.test/v2/invoices', $request->all());

However, when I make a request for a microservice, it returns the requested data, but with the URL of the microservice:

{
    "data": [
        # data returned from billing microservice with the billing API URL
    ],
    "links": {
        "first": "http://api.billing.microservice.test/v2/invoices?page=1",
        "last": "http://api.billing.microservice.test/v2/invoices?page=10",
        "prev": null,
        "next": "http://api.billing.microservice.test/v2/invoices?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 10,
        "path": "http://api.billing.microservice.test/v2/invoices",
        "per_page": 30,
        "to": 30,
        "total": 300
    }
}

However I need the return to have the main API URL:

{
    "data": [
        # data returned from billing microservice with the main API Address
    ],
    "links": {
        "first": "http://api.main.test/v2/invoices?page=1",
        "last": "http://api.main.microservice.test/v2/invoices?page=10",
        "prev": null,
        "next": "http://api.main.test/v2/invoices?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 10,
        "path": "http://api.main.test/v2/invoices",
        "per_page": 30,
        "to": 30,
        "total": 300
    }
}

Has anyone had to do something similar? What is the best way to achieve the desired results? Do a replace using some kind of regex? Is there anything I can do inside the microservice?

1 Answers

I was able to solve my problem with a simple method that already exists:

Model::paginate()->setPath('http://api.main.microservice.test/v2/invoices');

The result is:

{
    "data": [
        # data
    ],
    "links": {
        "first": "http://api.main.microservice.test/v2/invoices?page=1",
        "last": "http://api.main.microservice.test/v2/invoices?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://api.main.microservice.test/v2/invoices",
        "per_page": 30,
        "to": 3,
        "total": 3
    }
}
Related