次のような関数を作成します。
Project: project-apr06
関数名: function-apr06
とします。
ツリー構造
$ tree
.
├── main.py
└── requirements.txt
main.py
import sys
#
def hello_http(request):
sys.stderr.write("*** hello_http *** start ***\n")
rvalue = f'*** message from gcloud ***'
request_json = request.get_json()
if request.args and 'message' in request.args:
sys.stderr.write("*** hello_http *** get ***\n")
rvalue = 'GET ' + request.args.get('message')
elif request_json and 'message' in request_json:
sys.stderr.write("*** hello_http *** post ***\n")
rvalue = 'POST ' + request_json['message']
else:
rlvaue = f'Hello World!'
#
sys.stderr.write("*** hello_http *** end ***\n")
#
return rvalue
#
requirements.txt
# Function dependencies, for example:
# package>=version
Curl でテスト
curl_get.sh
HOST="https://asia-northeast1-project-apr06.cloudfunctions.net"
curl $HOST/function-apr06 \
-H "Authorization: bearer $(gcloud auth print-identity-token)"
echo ""
curl_post.sh
HOST="https://asia-northeast1-project-apr06.cloudfunctions.net"
curl -X POST $HOST/function-apr06 \
-H "Content-Type: application/json" \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-d '{"message": "こんにちは Apr/06/2022"}'
echo ""
Httpie でテスト
http_get.sh
HOST="https://asia-northeast1-project-apr06.cloudfunctions.net"
http $HOST/function-apr06 \
"Authorization: bearer $(gcloud auth print-identity-token)"
http_post.sh
HOST="https://asia-northeast1-project-apr06.cloudfunctions.net"
http $HOST/function-apr06 \
"Authorization: bearer $(gcloud auth print-identity-token)" \
message="おはようございます。 Apr/06/2022"
Python3 でテスト
トークンの取得
get_token.sh
gcloud auth print-identity-token > access_token.txt
client_get.py
#! /usr/bin/python3
#
# client_get.py
#
# Apr/23/2022
#
# ------------------------------------------------------------------
import sys
import json
import requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
with open('access_token.txt') as ff:
access_token = ff.read().strip()
#
#print(access_token)
host="https://asia-northeast1-project-apr06.cloudfunctions.net"
url_aa=host + "/function-apr06?message=本日は晴天なり"
#
#
headers = {'Authorization': 'bearer {}'.format(access_token)}
#
rr = requests.get(url_aa,headers=headers)
#
print(rr.text)
print()
print(rr)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
client_post.py
#! /usr/bin/python3
#
# client_post.py
#
# Apr/23/2022
#
# ------------------------------------------------------------------
import sys
import json
import requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
with open('access_token.txt') as ff:
access_token = ff.read().strip()
#
host="https://asia-northeast1-project-apr06.cloudfunctions.net"
url_aa=host + "/function-apr06"
#
#
headers = {'Authorization': 'bearer {}'.format(access_token)}
#
payload = {}
payload["message"] = "今は夜です。"
#
rr = requests.post(url_aa,json=payload,headers=headers)
#
print(rr.text)
print()
print(rr)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------