googleapis / google-api-php-client

A PHP client library for accessing Google APIs
http://googleapis.github.io/google-api-php-client/
Apache License 2.0
9.31k stars 3.52k forks source link

fix: batch requesting RequestInterface #2560

Closed gbretas closed 5 months ago

gbretas commented 7 months ago

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

google-cla[bot] commented 7 months ago

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.

devstack-be commented 7 months ago

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 commented 7 months ago

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());
            }
bshaffer commented 5 months ago

@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.