Skip to content

Commit 04c8e20

Browse files
feat(firestore): add list_of_documents() method
1 parent 90ab574 commit 04c8e20

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

firebase/firestore/__init__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,51 @@ def get(self, token=None):
329329

330330
return docs
331331

332+
def list_of_documents(self, token=None):
333+
""" List all sub-documents of the current collection.
334+
335+
:type token: str
336+
:param token: (Optional) Firebase Auth User ID Token, defaults
337+
to :data:`None`.
338+
339+
340+
:return: A list of document ID's.
341+
:rtype: list
342+
"""
343+
344+
docs = []
345+
346+
path = self._path.copy()
347+
self._path.clear()
348+
349+
if self._credentials:
350+
db_ref = _build_db(self.__datastore, path)
351+
352+
list_doc = list(db_ref.list_documents())
353+
354+
for doc in list_doc:
355+
docs.append(doc.id)
356+
357+
else:
358+
359+
req_ref = f"{self._base_url}/{'/'.join(path)}?key={self._api_key}"
360+
361+
if token:
362+
headers = {"Authorization": "Firebase " + token}
363+
response = self._requests.get(req_ref, headers=headers)
364+
365+
else:
366+
response = self._requests.get(req_ref)
367+
368+
raise_detailed_error(response)
369+
370+
if response.json().get('documents'):
371+
for doc in response.json()['documents']:
372+
doc_id = doc['name'].split('/')
373+
docs.append(doc_id.pop())
374+
375+
return docs
376+
332377
def limit_to_first(self, count):
333378
""" Create a limited query with this collection as parent.
334379

0 commit comments

Comments
 (0)