Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Added 'throw' method to PendingRequest #41953

Merged
merged 3 commits into from
Apr 13, 2022

Conversation

ash-jc-allen
Copy link
Contributor

Hey! This PR proposes a new throw method that can be added to the PendingRequest class for using on the HTTP facade.

In one of my projects, I have a "base client" (for lack of a better term) that I use when making requests to an API. It looks something like this:

private function client(): PendingRequest
{
    return Http::baseUrl('https://www.getrevue.co/api/v2/')
        ->withHeaders([
            'Authorization' => 'Token '.config('services.revue.api_key'),
        ])
}

And then I want to make any requests, I can call it like so:

$this->client()->post('subscribers', [
    'email' => $email,
    'double_opt_in' => false,
])->throw();

I use the "base client" in quite a few different places with different API routes and noticed that I'd forgotten to add throw() to one of my requests, so a small bug was slipping through. Of course, this is something that my tests should have picked up on, but I think I missed this particular edge case.

So, this PR allows you to add throw to your PendingRequest so that we don't need to remember to place it on the individual responses. For example, I could update my code to this instead:

private function client(): PendingRequest
{
    return Http::baseUrl('https://www.getrevue.co/api/v2/')
        ->withHeaders([
            'Authorization' => 'Token '.config('services.revue.api_key'),
        ])
       ->throw();
}

and then remove throw from the response:

$this->client()->post('subscribers', [
    'email' => $email,
    'double_opt_in' => false,
]);

Now, this means that if there's a client error or server error when using my "base client", it means that an exception will always be thrown.

From what I can see, this appears to be working and I've tried to keep it working the same as the throw method on the Response, but it's possible that I might have missed something off.

If it's something that you think might be useful to other devs, please let me know if there's anything you might want changing 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants