0

In this link there a get method in Python:

response = drive_service.files().list(q="mimeType='image/jpeg'",
                                          spaces='drive',
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):

I guess the get() method will get a list of files, and if none, it will return an empty list, but where is this documented? The API doc does not have it. It simply says:

Returns:
  An object of the form:
...

It did not document any method.

puravidaso
  • 1,013
  • 1
  • 5
  • 22
  • 1
    I thought that `get` of `for file in response.get('files', []):` is the method of the built-in type. [Ref](https://docs.python.org/3.9/library/stdtypes.html?highlight=get#dict.get) At your `fields`, ` drive_service.files().list()` returns an object like `{"nextPageToken": "###", "files": [{"id": "###", "name": "###"},,,]}`. [Ref](https://developers.google.com/drive/api/v3/reference/files/list) When files of `mimeType='image/jpeg'` isn't found, `{"files": []}` is returned. But I couldn't understand the answer you want. So I posted it as a comment. I apologize for my poor English skill. – Tanaike Feb 06 '22 at 06:26
  • I thought when I posted the message that files().list() returns an API defined object and the API needs to document its members and methods. When I realized it just returns a standard Python dictionary and get() is the dictionary method which is documented [here](https://docs.python.org/3/library/stdtypes.html#typesmapping). So thank you! – puravidaso Feb 06 '22 at 14:08
  • As the value will be `[]` when nothing is returned, as you quoted, I do not think the second argument in `response.get('files', [])` is needed. – puravidaso Feb 06 '22 at 14:19
  • 1
    Thank you for replying. I'm glad you could understand about your issue. I could confirm that my understanding of your question was correct. Also, I'm glad for it. About `As the value will be [] when nothing is returned, as you quoted, I do not think the second argument in response.get('files', []) is needed.`, I think that it's yes. If my comment was useful for your situation, can you post it as an answer? By this, it will be useful for other users who have the same issue. – Tanaike Feb 06 '22 at 22:54
  • I cannot accept a comment as an answer. If I post an answer, it will be treated as I am answering my own questions. If you post an answer and I will accept it. Also could you please pay attention to [my other Google API question](https://stackoverflow.com/questions/71011364/google-drive-api-contains-does-not-match-string-in-middle-in-files-listq)? Thanks! – puravidaso Feb 06 '22 at 23:49
  • Thank you for replying. From your replying, I posted it as an answer. Could you please confirm it? And, about your another question, I would like to check it. – Tanaike Feb 07 '22 at 03:12

2 Answers2

1

files.list method returns a file list response

{
  "kind": "drive#fileList",
  "nextPageToken": string,
  "incompleteSearch": boolean,
  "files": [
    files Resource
  ]
}

File list response contains a list of any file resources returned by the request. If there are not returned the files will be an empty list.

By default it does not return a full file resource object a number of fields are null.

 {
   "kind": "drive#file",
   "id": "1x8-vD-XEA5Spf3qp8x2wltablGF22Lpwup8VtxNY",
   "name": "Experts Activity Dump go/ExpertsActivities",
   "mimeType": "application/vnd.google-apps.spreadsheet"
  },

The way around that is to add an optional param of fields=* and then you will get a full file resource object back. This can result in a very large request though.

The file.get method returns a the file resources for that file id that is requested. So you have to pass it a file.id of the file you want to request.

Background info Original release.

In the original release of the Google drive v3. File.list did not work with fields=* it only worked only returned the default fields leaving everything else null. This force us to use a File.get on every file that we need the full response back for. This has been since fixed making it easer and requiring only a single call to the api to get all the properties of the object populated.

The question remains if this was a bug in the beginning or working as intended and they patched it to stop eating all our quota.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I was not asking about files.get() but response.get() which I now realized that it is just a standard Python dictionary method. – puravidaso Feb 06 '22 at 14:17
1

From your script, I thought that get of for file in response.get('files', []): is the method of the built-in type. Ref

From your fields of fields='nextPageToken, files(id, name)', drive_service.files().list() returns an object like {"nextPageToken": "###", "files": [{"id": "###", "name": "###"},,,]}. Ref When files of mimeType='image/jpeg' isn't found, {"files": []} is returned.

In this case, you can also use response.get('files').

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165