api http

Simplifying HTTP Requests with Guardian

Written on 18th Jan 2025
It will take between 2 and 3 minutes to read this article

Simplifying HTTP Requests with Guardian

When working with APIs, handling rate limits, caching, and error handling can often feel cumbersome. These tasks are repetitive, error-prone, and can clutter your codebase. That’s where Guardian, a PHP package I’ve developed, can help.

Guardian is designed to wrap HTTP requests with intelligent rulesets, making it easier to work with APIs while ensuring you stay within their constraints. Whether you’re integrating with a third-party service or building an internal API, Guardian can make your life a lot easier.


What Problem Does Guardian Solve?

When working with APIs, you often need to:

  • Ensure requests don’t exceed rate limits.
  • Handle various errors gracefully.
  • Cache responses to avoid unnecessary calls.

Guardian provides a simple, clean solution to these problems, letting you focus on building features instead of writing boilerplate code.


Getting Started with Guardian

Here’s how to get started:

Installation

Install the package via Composer:

composer require midnite/guardian

That’s it! You’re ready to start using Guardian. Check out the installation guide for more details.

Identifiers

Each request in Guardian is identified by a unique key, which ensures the rules and cache are applied correctly. Learn how to choose an identifier for your requests.

Making a Request

Guardian makes sending requests straightforward. Here’s an example:

$response = Guardian::make('unique-identifier')
->get('https://api.example.com/data');

For more examples, visit the HTTP request guide.


Caching with Guardian

One of Guardian’s standout features is its caching capabilities. By caching responses, you can reduce the load on external APIs and improve your application’s performance. Guardian supports multiple cache stores, and you can even create a custom cache driver if needed.

Need to clear the cache? That’s easy too. Read more about clearing the cache.


Rulesets: The Core of Guardian

At the heart of Guardian are rulesets, which allow you to define behaviours for your HTTP requests. For example:

  • Rate Limiting Rules: Automatically enforce rate limits. Learn more here.
  • Error Handling Rules: Define how different errors should be handled. More info here.

Handling Exceptions

Guardian’s exceptions make debugging easier. For instance:

  • Rate Limit Exceeded Exception: Triggered when a request exceeds the defined limit.
  • Rule Prevents Execution Exception: Thrown when a rule explicitly blocks a request.

You can explore all the exceptions in the exceptions guide.


Real-World Examples

Here are some ways you can use Guardian:

  • Third-Party APIs: Avoid rate limit errors and cache frequently requested data.
  • Internal Services: Standardise request handling across multiple microservices.

Why Use Guardian?

Guardian reduces boilerplate code, makes your application more robust, and ensures your HTTP requests are efficient and safe. By abstracting common tasks like caching, rate limiting, and error handling, it allows you to focus on building the features that matter.


To get started, check out the full documentation at guardian.midnite.uk.

Code highlighting provided by torchlight.dev.