saloonphp / saloon

🤠 Build beautiful API integrations and SDKs with Saloon
https://docs.saloon.dev
MIT License
2.03k stars 105 forks source link

Additional requests with the main request #437

Closed mstfblci closed 1 month ago

mstfblci commented 2 months ago

Hi,

Sometimes, in some APIs, I need other requests to be sent along with a request.

We can build a structure like this:

class AvailabilityRequest extends Request
{

    public function __construct(
        protected array $data
    )
    {
        //
    }

    public function defaultAdditionalRequests(): array
    {
        return [
            new DetailService($this->data)
        ];
    }
}

This will make saloonphp more useful.

craigpotter commented 1 month ago

Why wouldn't you just do

$myConnector->send(new RequestA($data));
$myConnector->send(new RequestB($data));
Sammyjo20 commented 1 month ago

Thanks for the suggestion, but I agree with @craigpotter.

I don't think you should make additional requests inside a Saloon request. If you really need to do it, you can do the following inside of your request:

use Saloon\Http\PendingRequest;

public function boot(PendingRequest $pendingRequest)
{
     $pendingRequest->getConnector()->send(new RequestB);
}

This above code would run before the initial request is sent.