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

Some page streaming methods have no `Pages` method

Open saicheems opened this issue 10 years ago • 17 comments

I believe this only happens when the response has a nextPageToken field, but no pageToken field.

For example, cloudresourcemanager.organizations.search has a page streaming response, but the Go client library doesn't implement it as one: https://godoc.org/google.golang.org/api/cloudresourcemanager/v1#OrganizationsSearchCall

saicheems avatar Oct 26 '16 04:10 saicheems

More specifically, the pageToken is anomalously a field of the request body rather than a query parameter of the method. See https://github.com/googleapis/toolkit/issues/692.

tcoffee-google avatar Nov 07 '16 21:11 tcoffee-google

Sorry, just getting to this.

How can you tell from the json discovery doc that that method is a streaming method? It looks like an ordinary RPC to me, although it's structured differently. It still has a page size, token, and nextPageToken.

jba avatar Nov 07 '16 23:11 jba

Not sure if we mean different things by "streaming" --- we've been using the term "page streaming" just to mean that the method returns results page by page using page tokens. The vast, vast majority of APIs implement such methods according to style guidelines with a method query parameter called pageToken and a response field called nextPageToken, but a few (see googleapis/toolkit#692) make the pageToken a field of the request body instead.

tcoffee-google avatar Nov 07 '16 23:11 tcoffee-google

OK. Probably shouldn't use "streaming" for that -- "pagination" would be better.

Now my question is, does this issue affect the code generator? I thought it only worked with the more recent Google API framework, not the one that the google.golang.org/api methods are based on.

jba avatar Nov 08 '16 14:11 jba

Not exactly sure what you're asking, but I think the code at issue is here: https://github.com/google/google-api-go-client/blob/master/google-api-go-generator/gen.go#L1604

Right now, the generator assumes that any paging method will have pageToken as a query parameter, but in some exceptional cases it appears as a field of the request body instead.

tcoffee-google avatar Nov 08 '16 22:11 tcoffee-google

Finally coming back to this after learning more about the generator.

So yes, the Go generator in this repo does not generate a Pages method if the page token is in the request instead of a query parameter. That's arguably a bug, but it's easy to work around by writing your own loop. It's not like Pages does much for you. (It doesn't even deal with page size.)

Since the generator in this repo is superseded by the one in googleapis/toolkit, I don't think it's worth the effort to fix this. Reopen if you disagree.

jba avatar Dec 16 '16 18:12 jba

it's easy to work around by writing your own loop

That may be true, but if we want to generate correct samples, we need to automate this generation, and that seems more complicated than just fixing this bug. Looks like a fairly trivial fix to me, unless there's some subtlety I'm overlooking.

the generator in this repo is superseded by the one in googleapis/toolkit

What do you mean by this?

tcoffee-google avatar Dec 19 '16 18:12 tcoffee-google

Btw, here's the generator function that decides whether or not a method is paging:

https://github.com/google/google-api-go-client/blob/master/google-api-go-generator/gen.go#L1355

saicheems avatar Dec 19 '16 18:12 saicheems

This is more complicated than necessary to correctly identify paging methods for current APIs. The following logic would suffice:

IF (
  the response has a field named "pageToken" or "nextPageToken" AND (
    the method has a parameter named "pageToken" OR 
    the request body has a field named "pageToken" or "nextPageToken") AND 
  the method is not bigquery.tabledata.list)
THEN the method is paging 
ELSE the method is not paging.

The exception for bigquery.tabledata.list is necessary because bigquery.jobs.getQueryResults (a paging method) has an identical relevant signature, and they will not change their API.

The name "nextPageToken" occurs as a request body field only in one method in an undocumented API (identitytoolkit.relyingparty.downloadAccount), which I presume to be paging, as this signature occurs nowhere else.

tcoffee-google avatar Dec 19 '16 22:12 tcoffee-google

It would not hurt to also allow "nextPageToken" as the name of the method parameter; this case currently does not occur.

tcoffee-google avatar Dec 19 '16 22:12 tcoffee-google

It looks like bigquery.tabledata.list can be used to page through results: you can set a page token on the request, and you get a next-page token in the response. Why don't you consider it a paging method?

jba avatar Dec 21 '16 21:12 jba

According to the docs, the token you get in the response is not a next-page token, but a stable-access identifier for the result set that you can use to retrieve the same results again when the underlying table is changing.

tcoffee-google avatar Dec 21 '16 22:12 tcoffee-google

The docs are just poorly worded. What they mean by

Providing this token instead of the startIndex parameter can help you retrieve stable results when an underlying table is changing

is that if you page through results using startIndex, incrementing it each time by the number of rows you got, then you may miss rows or get rows twice if there have been changes to the table in a page you've already read. But if you use pageToken, you won't have that problem.

I confirmed that using pageToken really does do paging by using the embedded API explorer in https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/list. I have a small table with an ID column that contains, in order,

2, 1, 4, 5, ...

First I ran

GET https://www.googleapis.com/bigquery/v2/projects/veener-jba/datasets/ds1/tables/products/data?maxResults=2&key={YOUR_API_KEY}

which returned rows with IDs 2 and 1, as well as a page token. Then I used the page token:

GET https://www.googleapis.com/bigquery/v2/projects/veener-jba/datasets/ds1/tables/products/data?maxResults=2&pageToken=BGRYXYBDLEAQAAASAUIIBAEAAUNAICACCABCBMFOCUVAA%3D%3D%3D&key={YOUR_API_KEY}

and got back rows 4 and 5.

jba avatar Dec 22 '16 00:12 jba

CL https://code-review.googlesource.com/#/c/10110. Thomas, I couldn't add you as a reviewer because gerrit, but you should be able to visit and comment.

jba avatar Dec 22 '16 00:12 jba

So just to clarify, in bigquery.tabledata.list you used the response pageToken in precisely the way one would typically use a response nextPageToken?

tcoffee-google avatar Dec 22 '16 00:12 tcoffee-google

That is correct.

jba avatar Dec 22 '16 11:12 jba

Super, thanks for catching that.

tcoffee-google avatar Dec 22 '16 20:12 tcoffee-google