Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ dmypy.json

# Pyre type checker
.pyre/
.DS_Store
5 changes: 2 additions & 3 deletions ufirestore/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ def cb(cur, s):

return self.cursor(path, cb)

def process(self, name):
def process(self):
return {
"name": name,
"fields": self.data
}

Expand Down Expand Up @@ -208,4 +207,4 @@ def where(self, field, op, value):
return self

def process(self):
return self.data
return self.data
35 changes: 18 additions & 17 deletions ufirestore/ufirestore.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ujson
import urequests
import _thread

import urequests

class FirestoreException(Exception):
def __init__(self, message, code=400):
Expand Down Expand Up @@ -42,38 +41,39 @@ def get_resource_name(url):
return url[url.find("projects"):]


def send_request(path, method="get", params=dict(), data=None, dump=True):
def send_request(path, method="GET", params=dict(), data=None, dump=True):
headers = {}

if FIREBASE_GLOBAL_VAR.ACCESS_TOKEN:
headers["Authorization"] = "Bearer " + FIREBASE_GLOBAL_VAR.ACCESS_TOKEN

response = urequests.request(
method, path, params=params, headers=headers, json=data)
if method == "POST":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also worth noting that the patch function should be a "PATCH" method to work properly (this may be a new thing, this is such an old pull request that it may predate it)

response = urequests.request(method, path, data=None, json=data)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should include the headers, no?

else:
response = urequests.request(method, path, headers=headers)

if dump == True:
if response.status_code < 200 or response.status_code > 299:
print(response.text)
raise FirestoreException(response.reason, response.status_code)

json = response.json()
if json.get("error"):
jsonResponse = response.json()
if jsonResponse.get("error"):
error = json["error"]
code = error["code"]
message = error["message"]
raise FirestoreException(message, code)
return json
return jsonResponse


class INTERNAL:

def patch(DOCUMENT_PATH, DOC, cb, update_mask=None):
PATH = construct_url(DOCUMENT_PATH)
LOCAL_PARAMS = to_url_params()
if update_mask:
for field in update_mask:
LOCAL_PARAMS += "updateMask.fieldPaths=" + field
DATA = DOC.process(get_resource_name(PATH))
LOCAL_OUTPUT = send_request(PATH+LOCAL_PARAMS, "post", data=DATA)
LOCAL_PARAMS += "updateMask=" + field
DATA = DOC.process()
LOCAL_OUTPUT = send_request(PATH, "POST", data=DATA)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here one would need to use a PATCH method and the updateMask.fieldPaths notation is correct in the original, it just needs proper ampersand joining as the to_url_params function supports

if cb:
try:
return cb(LOCAL_OUTPUT)
Expand All @@ -86,7 +86,7 @@ def create(COLLECTION_PATH, DOC, cb, document_id=None):
PATH = construct_url(COLLECTION_PATH)
PARAMS = {"documentId": document_id}
DATA = DOC.process(get_resource_name(PATH))
LOCAL_OUTPUT = send_request(PATH, "post", PARAMS, DATA)
LOCAL_OUTPUT = send_request(PATH, "POST", PARAMS, DATA)
if cb:
try:
return cb(LOCAL_OUTPUT)
Expand All @@ -101,7 +101,7 @@ def get(DOCUMENT_PATH, cb, mask=None):
if mask:
for field in mask:
LOCAL_PARAMS += "mask.fieldPaths=" + field
LOCAL_OUTPUT = send_request(PATH+LOCAL_PARAMS, "get")
LOCAL_OUTPUT = send_request(PATH+LOCAL_PARAMS, "GET")
if cb:
try:
return cb(LOCAL_OUTPUT)
Expand Down Expand Up @@ -167,7 +167,7 @@ def list_collection_ids(DOCUMENT_PATH, cb, page_size=None, page_token=None):
"pageSize": page_size,
"pageToken": page_token
}
LOCAL_OUTPUT = send_request(PATH, "post", data=DATA)
LOCAL_OUTPUT = send_request(PATH, "POST", data=DATA)
if cb:
try:
return cb(LOCAL_OUTPUT.get("collectionIds"),
Expand All @@ -183,7 +183,7 @@ def run_query(DOCUMENT_PATH, query, cb):
DATA = {
"structuredQuery": query.data
}
LOCAL_OUTPUT = send_request(PATH, "post", data=DATA)
LOCAL_OUTPUT = send_request(PATH, "POST", data=DATA)
if cb:
try:
return cb(LOCAL_OUTPUT.get("document"))
Expand Down Expand Up @@ -267,3 +267,4 @@ def run_query(PATH, query, bg=True, cb=None):
INTERNAL.run_query, [PATH, query, cb])
else:
return INTERNAL.run_query(PATH, query, cb)