1
1

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 3 years have passed since last update.

Python3 の Rest で Cloud Firestore のデータを作成 (Create)

Posted at

こちらと同じことを Rest で行います。
Python3 で Cloud Firestore のデータを作成 (Create)

firestore_create_rest.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	firestore_create_rest.py
#
#					Jan/31/2020
#
# ------------------------------------------------------------------
import  sys
import  json
import  requests
#
from get_token import get_token_proc
# ------------------------------------------------------------------
def add_data_proc(url_base,token,key,name,population,date_mod):
	unit_aa = {}
	unit_aa["name"] = {"stringValue": name}
	unit_aa["population"] = {"integerValue": population}
	unit_aa["date_mod"] = {"timestampValue": date_mod}
	payload = {"fields": unit_aa}
	str_json = json.dumps(payload)
	headers = {"Authorization": "Bearer " + token,
		"Content-Type": 'application/json'}
	url = url_base + "?documentId=" + key
	try:
		rr=requests.post(url,data=str_json,headers=headers)
		print(rr.text)
	except Exception as ee:
		sys.stderr.write("*** error *** in requests.post ***\n")
		sys.stderr.write(str(ee) + "\n")
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
token = get_token_proc()
#
#print(token)
#
url="https://firestore.googleapis.com/v1/projects/project-jan25-2020/databases/(default)/documents/cities"
#
add_data_proc(url,token,'t0921','宇都宮',38921,"2010-10-5T00:15:07.0Z")
add_data_proc(url,token,'t0922','小山',56417,"2010-9-12T01:16:07.0Z")
add_data_proc(url,token,'t0923','佐野',92185,"2010-12-8T02:17:07.0Z")
add_data_proc(url,token,'t0924','足利',39546,"2010-7-4T03:18:07.0Z")
add_data_proc(url,token,'t0925','日光',18492,"2010-10-2T04:19:07.0Z")
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
get_token.py
# -*- coding: utf-8 -*-
#
#	firestore_token.py
#
#					Jan/31/2020
#
# ------------------------------------------------------------------
import  os
import  sys
import	subprocess
# ------------------------------------------------------------------
def get_token_proc():
	token = ""
	command=["gcloud","auth","application-default","print-access-token"]
#
	try:
		my_env = os.environ.copy()
		token_byte = subprocess.check_output(command,env=my_env)
		token = token_byte.decode('utf-8')
	except Exception as ee:
		sys.stderr.write("*** error *** in subprocess.check_output ***\n")
		sys.stderr.write(str(ee) + "\n")
#
	return	token[:-1]
# ------------------------------------------------------------------

実行コマンド

export GOOGLE_APPLICATION_CREDENTIALS="***.json"
./firestore_create_rest.py
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?