google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

fix: batch requesting RequestInterface

Open gbretas opened this issue 1 year ago • 3 comments

This error causing the batch add requesting an Psr\Http\Message\RequestInterface;. But this is an error.

gbretas avatar Feb 14 '24 13:02 gbretas

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Feb 14 '24 13:02 google-cla[bot]

I have the same problem and I have not yet managed to use the batch on the google calendar api.

Google\Http\Batch::add(): Argument #1 ($request) must be of type Psr\Http\Message\RequestInterface, Google\Service\Calendar\Event given, called in...

devstack-be avatar Mar 03 '24 08:03 devstack-be

I fix my problem. @gbretas You need to add thoses lines:

            $this->httpClient->setDefer(true);
            $calendarService = new CalendarService($this->httpClient);
            $calendarService->getClient()->setUseBatch(true);
            $batch = $calendarService->createBatch();

...
                $request = $calendarService->events->insert(YOUR_CALENDAR_ID, $event);
                $requestId = YOUR_REQUEST_ID
                $batch->add($request, $requestId);
            try {
                $results = $batch->execute();
            }
            catch (Exception $e ){
                Log::info($e->getMessage());
            }

devstack-be avatar Mar 03 '24 09:03 devstack-be

@gbretas see @devstack-be's comment above, or see the batch example in examples/batch.php:

https://github.com/googleapis/google-api-php-client/blob/017400f609c1fb71ab5ad824c50eabd4c3eaf779/examples/batch.php#L54-L78

The proper usage is to pass the RequestInterface object after calling the method in batch-mode. The typehint we have there is correct.

bshaffer avatar May 02 '24 16:05 bshaffer