Saloon 2 is Here

Packages

February 21st, 2023

Saloon 2 is Here

The Saloon package for PHP released version 2, rebuilt from the ground up with faster concurrency, retries, HTTP client agnostic design, solo requests, and more:

  • Namespace changes (i.e., SaloonRequest -> Request)
  • Decoupling Guzzle from the Saloon V2 Package
  • Improved developer experience
  • Connector-first design
  • New Middleware pipeline
  • Request concurrency and pooling
  • Better interface adoption
  • Improved interactions with requests, headers, query params, and config
  • Improved multi-part requests
  • Improved Laravel support
  • Improved exception handling
  • Solo requests
  • New pagination helpers
  • And more

Solo requests are perfect for APIs for which you might make one request to the API. Typically, you'd need to define a connector to make requests. With a solo request, you define a class that extends SoloRequest and call send() to use it!

class GetPokemonRequest extends SoloRequest
{
protected Method $method = Method::GET;
 
public function resolveEndpoint()
{
return 'https://pokeapi.co/api/v2/pokemon';
}
}
 
// No Connector Needed!
 
$request = new GetPokemonRequest;
$response = $request->send();

Pagination is another new feature that makes it easy to iterate through hundreds of pages without writing any boilerplate code:

$connector = new SpotifyConnector;
 
// Create a paginator and pass in a request class, in this example
// we'll pass in the LikedSongsRequest which will retrieve all
// the liked songs of the authenticated user.
 
$paginator = $connector->paginate(new LikedSongsRequest);
 
// Create a Laravel LazyCollection from the paginator and iterate
// over each of the results. Traditionally, the liked songs endpoint
// only lets you get 50 tracks per request, but the paginator will
// automatically grab every page of results and pass it into a
// single collection! 🔥
 
$collection = $paginator->collect('items')->map(function ($track) {
return sprintf('%s - %s', $track['artist'], $track['name']);
});
 
// Convert the LazyCollection into an array.
 
$data = $collection->all();

To see everything new in V2, check out What's new in v2 in the official documentation. Congratulations to Sam Carré and all the contributors that worked on Saloon V2!

Related: API Integrations using Saloon in Laravel

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.