def batch(path: str): """ Create a batch read/write object. This allows modifications of multiple documents in a single commit. """ return _FirestoreBatch(Firebase.get_db().batch(), Firebase.get_db().collection(path))
def upload(path_to_file: str): """Uploads from a file on the system""" file_extension = path_to_file.split(".")[-1] blob_name = Bucket._auto_id() + "/img." + file_extension blob = Firebase.get_bucket().blob(blob_name) blob.upload_from_filename(path_to_file) return blob_name
def get(path: str) -> Generator: """ Fetch all items in a path from the Firestore database :param path: The Firestore path of the collection to get :type path: str :return: An iterable containing the documents in the path. """ return Firebase.get_db().collection(path).stream()
def get_url(blob_name: str) -> Optional[str]: """Downloads file from Firebase Storage""" try: blob = Firebase.get_bucket().get_blob(blob_name) except ValueError: return None if blob is None: return None return blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET')
def sign_in_with_tokens(id_token, refresh_token, user_id): """ Creates database and bucket for Firebase. Also updates credential store with the given tokens :param id_token: The JWT token from Firebase :param refresh_token: A refresh_token from Firebase used to renew tokens :param user_id: The user id for the signed in user :return The user id for the signed in user """ Firebase.create_db() Firebase.create_bucket() credential_store.put('tokens', id_token=id_token, refresh_token=refresh_token, user_id=user_id) UserMeGetter.set_me(user_id) if App.get_running_app(): App.get_running_app().is_authenticated = True return user_id
def subscribe(path: str, callback: Callable): """ Subscribes to a collection, executing callback whenever the collection is updated. Example on a callable function def on_snapshot(collection_snapshot, collection_change_snapshot, timestamp): for doc in collection_snapshot: print(u’{} => {}’.format)(doc.id, doc.to_dict())) :param path: Full path to a collection :param callback: The function that will be called when an update happens in the collection """ Firestore.refs[path] = Firebase.get_db().collection(path).on_snapshot( callback)
def subscribe_document(collection_path: str, document: str, callback: Callable): """ Subscribes to a document, executing callback whenever the collection is updated. Example on a callable function def on_snapshot(document_snapshot): for doc in document_snapshot: print(u’{} => {}’.format)(doc.id, doc.to_dict())) :param collection_path: The collection, e.g. /users :param document: The last bit of the path specifying the document :param callback: The function that will be called when an update happens in the document """ path = collection_path + "/" + document Firestore.refs[path] = Firebase.get_db().collection( collection_path).document(document).on_snapshot(callback)
def download_as_string(path: str) -> Optional[str]: """Downloads file from Firebase Storage""" blob = Firebase.get_bucket().get_blob(path) if blob is None: return None return blob.download_as_string()
def delete(path: str): """Deletes a file from Firebase Storage""" blob = Firebase.get_bucket().get_blob(path) blob.delete()
def get_raw(path: str): """Returns raw collection at a given path in the database""" return Firebase.get_db().collection(path)