I built a transport-independent foundation for JSON API clients in Perl
Writing a simple API client is easy. my $response = $http -> get ( $url ); Then the client starts being used in production. Suddenly, it needs to handle: query parameters JSON encoding and decoding structured errors retries and Retry-After rate limits pagination authentication request IDs logging and metrics idempotency At that point, what started as a small API wrapper often becomes a collection…
The article discusses the creation of HTTP::API::Core, a small Perl module designed to act as a transport-independent foundation for JSON API clients. The author found that the majority of the code required to build an API client is not specific to the API being wrapped, but rather consists of infrastructure code common to most API clients. This led to the development of HTTP::API::Core, which separates HTTP transport from API-client policy.
The core idea behind HTTP::API::Core is to keep the HTTP transport separate from the application-level policies surrounding it. This is achieved by using a different HTTP library as the transport layer, such as HTTP::Tiny, LWP, Mojo::UserAgent, or Furl, while HTTP::API::Core handles the policy aspects. The HTTP implementation is responsible for making the request, while HTTP::API::Core manages retry, pagination, error handling, authentication, request IDs, logging, metrics, and idempotency.
The module is designed to be dependency-light and does not replace the existing Perl HTTP ecosystem. It sits above the transport layer, allowing developers to choose their preferred HTTP library. The API client uses HTTP::API::Core to handle repetitive infrastructure code, while the application-level policies remain separate.
An example of using HTTP::API::Core is shown in the article, where a simple API client is created with base URL, headers, timeout, and retry settings. The client can then use the API in a simple and concise manner, with the core handling the repetitive infrastructure. The author also discusses how retry, pagination, and rate limits are handled in a consistent and customizable manner, providing flexibility for different API styles and requirements.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.