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.
When working with APIs, you often need to:
Guardian provides a simple, clean solution to these problems, letting you focus on building features instead of writing boilerplate code.
Here’s how to get started:
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.
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.
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.
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.
At the heart of Guardian are rulesets, which allow you to define behaviours for your HTTP requests. For example:
Guardian’s exceptions make debugging easier. For instance:
You can explore all the exceptions in the exceptions guide.
Here are some ways you can 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.