Python3: Google Cloud Functions の使い方

次のような関数を作成します。

Project: project-apr06
関数名: function-apr06
とします。

ツリー構造

```text
$ tree 
.
├── main.py
└── requirements.txt
```

```py: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
#
```

```text:requirements.txt
# Function dependencies, for example:
# package>=version
```

![function_aa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/dacc89e1-29ff-fbf5-8829-a0e5dc1e2c94.png)

## Curl でテスト ##

```bash:curl_get.sh
HOST="https://asia-northeast1-project-apr06.cloudfunctions.net"
curl $HOST/function-apr06 \
 -H "Authorization: bearer $(gcloud auth print-identity-token)"
echo ""
```

```bash: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 でテスト ##

```bash:http_get.sh
HOST="https://asia-northeast1-project-apr06.cloudfunctions.net"
http $HOST/function-apr06 \
  "Authorization: bearer $(gcloud auth print-identity-token)"
```

```bash: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 でテスト ##

トークンの取得

```bash:get_token.sh
gcloud auth print-identity-token > access_token.txt
```

```py: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")
# ------------------------------------------------------------------
```

```py: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")
# ------------------------------------------------------------------
```