-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_main.py
70 lines (55 loc) · 2.18 KB
/
fs_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import json
import os
from datetime import datetime
PROJECT_ID = os.getenv("PROJECT_ID", "quiet-invention-417303")
DB_ID = os.getenv("DB_ID", "fs-test")
COLLECTION_ID = os.getenv("COLLECTION_ID", "collection_1")
DOC_ID = os.getenv("DOC_ID", "document_1")
def consultar_firestore(token):
url = f'https://firestore.googleapis.com/v1/projects/{PROJECT_ID}/databases/{DB_ID}/documents/{COLLECTION_ID}/{DOC_ID}'
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
respuesta_json = response.json()
return respuesta_json
else:
print(json.loads(response.text)['error']['message'])
return None
def escribir_firestore(token, count_str):
#url = f'https://firestore.googleapis.com/v1/projects/{PROJECT_ID}/databases/{DB_ID}/documents/{COLLECTION_ID}/{DOC_ID}?currentDocument.exists=true'
url = f"https://firestore.googleapis.com/v1/projects/{PROJECT_ID}/databases/{DB_ID}/documents/{COLLECTION_ID}/{DOC_ID}?updateMask.fieldPaths=count_str"
headers = {"Authorization": f"Bearer {token}", "Content-Type":"application/json"}
#body = {"fields": fields}
body = json.dumps({
"fields": {
"count_str": {
"stringValue": str(count_str)
}
}})
response = requests.patch(url, headers=headers, data=body)
if response.status_code == 200:
respuesta_json = response.json()
return respuesta_json
else:
print(json.loads(response.text)['error']['message'])
return None
def fs_read_write(token):
# read
time_read = datetime.now()
resultado_read = consultar_firestore(token)
fields = resultado_read['fields']
# write
count = int(fields['count_str']['stringValue'])
resultado_write = escribir_firestore(token, count+1)
# read again
resultado_read2 = consultar_firestore(token)
fields2 = resultado_read2['fields']
count2 = int(fields2['count_str']['stringValue'])
time_read2 = datetime.now()
# print
resultado = f'R({time_read}):{count} | U({time_read}):{count2}'
print(resultado)
return resultado
token = ''
response = fs_read_write(token)