google-ads-php icon indicating copy to clipboard operation
google-ads-php copied to clipboard

Asynchronious reporting data requests ("GoogleAdsServiceClient->searchAsync()")

Open hakimio opened this issue 4 years ago • 3 comments

Problem you are trying to solve:

I would like to be able to download multiple reports in parallel.

Solution you'd like:

Right now when you call GoogleAdsServiceClient->search() method it in turn calls GapicClientTrait->getPagedListResponse() and blocks execution because of the wait() call at the end of the method.

I would like to have non-blocking GoogleAdsServiceClient->searchAsync() method which would just return GuzzleHttp\Promise\PromiseInterface and allow you to choose when to wait() for the promise to be resolved.

Additional context:

hakimio avatar Dec 14 '21 10:12 hakimio

Hi @hakimio,

Thank you for sending this enhancement request.

The implementation is owned by gax-php. Could you please elaborate a bit more on the use cases?

PierrickVoulet avatar Dec 14 '21 18:12 PierrickVoulet

Use case is simple: downloading multiple reports in parallel to cache them in local database.

hakimio avatar Dec 14 '21 19:12 hakimio

Proposed simple solution which works:

    public function searchAsync(string $customerId, string $query, array $optionalArgs = []): PromiseInterface
    {
        $request = new SearchGoogleAdsRequest();
        $requestParamHeaders = [];
        $request->setCustomerId($customerId);
        $request->setQuery($query);
        $requestParamHeaders['customer_id'] = $customerId;
        if (isset($optionalArgs['pageToken'])) {
            $request->setPageToken($optionalArgs['pageToken']);
        }

        if (isset($optionalArgs['pageSize'])) {
            $request->setPageSize($optionalArgs['pageSize']);
        }

        if (isset($optionalArgs['validateOnly'])) {
            $request->setValidateOnly($optionalArgs['validateOnly']);
        }

        if (isset($optionalArgs['returnTotalResultsCount'])) {
            $request->setReturnTotalResultsCount($optionalArgs['returnTotalResultsCount']);
        }

        if (isset($optionalArgs['summaryRowSetting'])) {
            $request->setSummaryRowSetting($optionalArgs['summaryRowSetting']);
        }

        $requestParams = new RequestParamsHeaderDescriptor($requestParamHeaders);
        $optionalArgs['headers'] = isset($optionalArgs['headers']) ? array_merge($requestParams->getHeader(), $optionalArgs['headers']) : $requestParams->getHeader();
        
        return $this->startCall('Search', SearchGoogleAdsResponse::class, $optionalArgs, $request);
    }

startCall() method is from GapicClientTrait class.

hakimio avatar Dec 15 '21 09:12 hakimio