0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3 の Rest で Cloud Firestore のデータを更新 (Update)

Posted at
firestore_update_rest.py
# ! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	firestore_update_rest.py
#
#					Jan/31/2020
#
# ------------------------------------------------------------------
import  sys
import  json
import  datetime
import  requests
#
from get_token import get_token_proc
# ------------------------------------------------------------------
def get_current_time_proc():
	date_xxx = "%s" %  datetime.datetime.today()
	date_yyy = date_xxx.split()
	date_mod = "%sT%sZ" %  (date_yyy[0],date_yyy[1])
#
#	print(date_mod)
#
	return date_mod
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_in = sys.argv[1]
population_in = int(sys.argv[2])
print("%s\t%d" % (key_in,population_in))
#
token = get_token_proc()
#
project = 'project-jan25-2020'
#
url_base="https://firestore.googleapis.com/v1/projects/" + project + "/databases/(default)/documents/cities/" + key_in
url = url_base + "?updateMask.fieldPaths=population&updateMask.fieldPaths=date_mod"
#
unit_aa = {}
unit_aa["population"] = {"integerValue": population_in}
date_mod = get_current_time_proc()
unit_aa["date_mod"] = {"timestampValue": date_mod}
payload = {"fields": unit_aa}
str_json = json.dumps(payload)
headers = {"Authorization": "Bearer " + token,
		"Content-Type": 'application/json'}
#
try:
	rr=requests.patch(url,data=str_json,headers=headers)
	print(rr.text)
#
except Exception as ee:
	sys.stderr.write("*** error *** in requests.delete ***\n")
	sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

get_token.py はこちら
Python3 の Rest で Cloud Firestore のデータを作成 (Create)

実行コマンド

export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_update_rest.py t0922 98456200
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?