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.29k stars 3.52k forks source link

Batch retrieving Google_Service_AdExchangeSeller->accounts_reports not working (v1 branch) #960

Closed PsychodelEKS closed 6 years ago

PsychodelEKS commented 8 years ago

I'm trying to use batch requests to retreive reports data from AdExchange account, but it does not seem to work as expected:

simple call version works fine (abstract code):

$reportData = $Service->accounts_reports->generate($googleAccountId, $startDate, $endDate, $optParams);

but using batch it fails:

$Service->getClient()->setUseBatch(true);
$batch = new Google_Http_Batch($Service->getClient());

$batch->add($Service->accounts_reports->generate($googleAccountId, $startDate, $endDate, $optParams), 1);

$reportsData = $batch->execute();

with:

Array
(
    [response-1] => Google_Service_AdExchangeSeller_Report Object
        (
            [collection_key:protected] => warnings
            [internal_gapi_mappings:protected] => Array
                (
                )

            [averages] =>
            [headersType:protected] => Google_Service_AdExchangeSeller_ReportHeaders
            [headersDataType:protected] => array
            [kind] => adexchangeseller#report
            [rows] =>
            [totalMatchedRows] =>
            [totals] =>
            [warnings] => Array
                (
                    [0] => This is an empty report: no dimensions or metrics were provided.
                )

            [modelData:protected] => Array
                (
                )

            [processed:protected] => Array
                (
                )

        )
)

generate method correctly returns Google_Http_Request object when called after setUseBatch(true), all report params are set correctly according to it's dump.

Is that me doing smth wrong, or is it broken, or not supported on server side?

PsychodelEKS commented 8 years ago

Played around with this myself - the problem is with `Google_Http_Request::toBatchString (Http/Request.php)

it uses http_build_query to rebuild url with query string for report and breaks the url, because some parameters which are specified as arrays must not be treated as array when building url, i.e.:

$params = array(
'metrics' => array('metr_1', 'metr_2'),
'dimensions' => array('dim_1', 'dim_2'),
)

are converted to:

metric%5B0%5D=metr_1&metric%5B1%5D=metr_2&dimensions%5B0%5D=dim_1&dimensions%5B1%5D=dim_2

and must be:

metric=metr_1&metric=metr_2&dimensions=dim_1&dimensions=dim_2

that is handled correctly for non-batch requests with Google_Http_REST::createRequestUri, but this method uses modified parameters array which is not stored for later usage in request itself

the easy way to fix this seems to use prebuilt url from the request and to remove server addr from it: $path = str_replace('https://www.googleapis.com', '', $this->getUrl()); but not sure if totally conforms all the requirements (works fine for me) $path = preg_replace('/(dimension|metric)%5B\d+?%5D/im', '$1', $path); is more fool-proof, but still not of production quality

mattwhisenhunt commented 6 years ago

@PsychodelEKS Is this still an issue for you? I still haven't looked at the v1 branch at all.

PsychodelEKS commented 6 years ago

@mattwhisenhunt I did not actually update the lib since the issue was created, so dunno if it was fixed or not. Since Google seems to migrate to DFP API now, we've moved to it from this one, so for me - not an issue anymore =)