I pointed one Eloquent-style client at 62 public APIs. Here's how they answered.
At work I had a lot of external APIs to talk to from Laravel. Each one got its own little client, its own pagination loop, its own way of saying "not found". I wanted the thing I already knew, Post::where(...)->paginate() , to work against all of them, so I wrote laravel-rest . Later I spent a while on the performance side (eager loading, concurrent requests, memoization) and started wondering…
In the author's experience working with external APIs in Laravel, each API required its own custom client, including pagination, error handling, and unique data structures. This led to the development of laravel-rest, a package designed to provide a uniform approach to interacting with APIs regardless of their specific characteristics.
To test the effectiveness of the package, the author compiled a catalog of 62 public APIs, ranging from simple JSON arrays to complex envelope structures like JSON:API, OData, and GraphQL. The APIs covered various pagination methods such as page, offset, and cursor-based pagination, as well as different envelope shapes.
The author then sent real requests to each of these 62 APIs, conducting 470 different scenarios to ensure comprehensive testing. The results of these requests were analyzed and presented, showcasing the raw data returned by the APIs.
For example, when querying the PokeAPI for a list of Pokémon, the response included a total count of 1351 Pokémon, with pagination metadata provided in the form of "next" and "previous" links. The author implemented a Pokemon model with specific configuration settings, allowing the package to automatically handle pagination and data retrieval. The model returned the first three Pokémon results, including their names and corresponding URLs.
Another example involved the Art Institute of Chicago API, which returned data in an envelope format with a foreign key relationship. The author demonstrated how to use the package to retrieve an artwork by its ID, including related information such as the artist's title. The package seamlessly handled the envelope structure, allowing the model to access the desired data without any additional modifications.
Finally, the author showcased a use case that closely aligned with the package's purpose: querying multiple related resources in a single request. Using the Artwork model, the author demonstrated how to retrieve multiple artworks and their associated artists using a single request. The package efficiently handled the request, fetching the required data and presenting it in a format compatible with Eloquent's pagination and model methods.
In summary, the author's experience with various APIs and their implementation of the laravel-rest package demonstrated the package's ability to provide a consistent, efficient, and uniform approach to interacting with diverse APIs, regardless of their specific structures or pagination methods.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.