この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
SORACOMには、開発者向けにAPIが提供されています。
本記事では、このAPIを使って、SIMグループ情報を取得してみました。
おすすめの方
- SORACOM APIを使いたい方
SORACOM APIを使う準備
- SAMユーザの作成
- SAMユーザのアクセス権限を付与
- SAMユーザの「認証キーID&認証キーシークレット」を取得
上記については、次のブログを参考に行います。
なお、今回のアクセス権限は下記となります。
{
"statements": [
{
"api": [
"Group:listGroups"
],
"effect": "allow"
}
]
}
SORACOM APIでSIMグループを取得する
いまのSIMグループを確認する
いまのSIMグループは、3つあります。
利用するAPI
次のAPIを利用します。
- GET /groups
Pythonスクリプト
次のスクリプトを使います。内容的には、SORACOM APIを使うためのAPIキーを取得して、SIMグループの設定更新APIにアクセスしています。
お試し実装のため、下記となっています。本番運用時には適宜検討し、実装してください。
- APIトークンの有効期限はデフォルト設定です。
- レートリミットの考慮をしていません。
app.py
import json
import requests
from typing import Tuple
SORACOM_ENDPOINT = 'https://api.soracom.io/v1'
SORACOM_KEY_ID = 'keyId-aaa'
SORACOM_SECRET = 'secret-bbb'
def main():
api_key, token = get_token()
get_groups(api_key, token)
def get_token() -> Tuple[str, str]:
headers = {'Content-Type': 'application/json'}
data = {
'authKeyId': SORACOM_KEY_ID,
'authKey': SORACOM_SECRET,
}
resp = requests.post(f'{SORACOM_ENDPOINT}/auth', headers=headers, data=json.dumps(data))
d = resp.json()
return d['apiKey'], d['token']
def get_groups(api_key: str, token: str) -> None:
headers = {
'Content-Type': 'application/json',
'X-Soracom-API-Key': api_key,
'X-Soracom-Token': token,
}
resp = requests.get(
f'{SORACOM_ENDPOINT}/groups',
headers=headers
)
print(resp)
print(json.dumps(resp.json(), indent=2))
if __name__ == '__main__':
main()
スクリプトを実行する
python app.py
次のJSONを取得できました。バッチリです。(一部加工済)
[
{
"operatorId": "OPaaa",
"groupId": "0bd26e39-xxx",
"createdAt": 1658910000001,
"lastModifiedAt": 1658910000002,
"configuration": {},
"tags": {
"name": "test-2"
},
"lastModifiedTime": 1658910000002,
"createdTime": 1658910000001
},
{
"operatorId": "OPbbbb",
"groupId": "3afa5669-yyy",
"createdAt": 1658920000001,
"lastModifiedAt": 1658920000002,
"configuration": {
"SoracomFunnel": {
"destination": {
"provider": "aws",
"service": "aws-iot",
"resourceUrl": "xxx-ats.iot.ap-northeast-1.amazonaws.com/soracom/#{imsi}"
},
"credentialsId": "foobar-20220727",
"contentType": "json",
"enabled": true
}
},
"tags": {
"name": "test-1"
},
"lastModifiedTime": 1658920000002,
"createdTime": 1658920000001
},
{
"operatorId": "OPccc",
"groupId": "f58d1d0e-zzz",
"createdAt": 1658930000001,
"lastModifiedAt": 1658930000003,
"configuration": {
"SoracomHarvest": {
"enabled": true
},
"SoracomAir": {
"userdata": {
"SensorData": {},
"Common": {},
"Setting": {
"itr": {
"i2": 1
}
}
},
"metadata": {
"enabled": true
}
},
"SoracomFunnel": {
"destination": {
"provider": "aws",
"service": "aws-iot",
"resourceUrl": "foo-ats.iot.ap-northeast-1.amazonaws.com/soracom/gps-multiunit/#{imsi}"
},
"credentialsId": "funnel-aws-iot-core",
"contentType": "json",
"enabled": true
}
},
"tags": {
"name": "GPSマルチユニット"
},
"lastModifiedTime": 1658930000003,
"createdTime": 1658930000001
}
]
さいごに
SORACOM APIでSIMグループの取得ができました。 SIMグループに紐づく設定(SORACOM Funnelなど)も取得できています。