Python
OpenAPI Specificationから生成 されたPython用クライアントライブラリを使用するサンプルを示します。
注釈
自動生成されたPythonのクライアントで時刻を使用する場合(基準時刻など)は必ずタイムゾーンの指定が必要なのでご注意ください。
basetime = datetime.now(tz=timezone.utc) # 必ず`tz` を指定します。
注釈
自動生成されたPythonのクライアントはデフォルトで入力と戻り値に対してバリデーションを実行します。しかし、一部そのバリデーションが意図した動作をせず、 正常な値であるにも関わらずエラーとなるケースがあります。
# エラーメッセージの例
Required value type is int and passed type was NoneType at ['received_data']['expected_data_points']
このようなエラーが出力された場合は、APIコールを行うメソッドを呼び出す際に、クライアントによるバリデーションを無効化するオプションを追加します。
api_meas_instance.replace_project_measurement_sequence(
... 省略
project_uuid=...,
measurement_uuid=...,
sequences_uuid=...,
measurement_sequence_group_replace=...,
_check_return_type=False, # 戻り値のバリデーションを無効にする
_check_input_type=False, # 入力値のバリデーションを無効にする
)
APIトークンを使用してAPIにアクセスする
以下のコードでは、APIトークンを使用して認証を行います。その後、認証したユーザー自身の情報を取得します。
実行時には以下を指定してください。
base_url
: 接続先intdash APIのベースURLapi_token
: ユーザーのAPIトークン(APIトークンはMy Pageで作成できます)
import argparse
import apiclient
from apiclient.api import auth_me_api
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--base_url", default="http://localhost:8080/api")
parser.add_argument(
"--api_token", default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
args = parser.parse_args()
configuration = apiclient.Configuration(
host=args.base_url,
api_key={"IntdashToken": args.api_token},
)
with apiclient.ApiClient(configuration) as api_client:
api_instance = auth_me_api.AuthMeApi(api_client)
try:
api_response = api_instance.get_me()
print(f"Hello {api_response.name}!!")
except apiclient.ApiException as e:
print(e)
OAuth2トークンを使用してAPIにアクセスする
注釈
このサンプルコードは次のライブラリに依存します。
requests-oauthlib = "^1.3.1"
実行時には以下を指定してください。
base_url
: 接続先intdash APIのベースURLclient_id
: エッジのUUIDclient_secret
エッジの作成時に発行されるクライアントシークレット
import argparse
import apiclient
from apiclient.api import auth_edges_api
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--base_url", default="http://localhost:8080/api")
parser.add_argument(
"--client_id", default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
parser.add_argument(
"--client_secret", default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
args = parser.parse_args()
client = BackendApplicationClient(client_id=args.client_id)
token_url = f"{args.base_url}/auth/oauth2/token"
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(
token_url=token_url,
include_client_id=True,
client_secret=args.client_secret,
)
configuration = apiclient.Configuration(
host=args.base_url,
access_token=token["access_token"],
)
# Enter a context with an instance of the API client
with apiclient.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = auth_edges_api.AuthEdgesApi(api_client=api_client)
try:
# Check HTTP Authorization
api_response = api_instance.get_me_as_edge()
print(f"Hello {api_response.name}!!")
except apiclient.ApiException as e:
print(e)
前述のサンプルコードはトークンのリフレッシュは行いません。
トークンが有効期間内か確認する必要がある場合は、トークン交換時の expires_in
を使用して判定してください。次に示します。
トークンの有効期限の日時を得るコードスニペット:
expires_in = token.get("expires_in")
expires_at = time.time() + expires_in
有効期限が切れている場合は、トークンを取得し、configurationに再設定するコードスニペット:
if expires_at < time.time():
token = oauth.fetch_token(
token_url=token_url,
include_client_id=True,
client_secret=client_secret
)
configuration.access_token = token["access_token"]
過去の計測のデータポイントを取得する
以下は、計測を指定して、その計測内のデータポイントを取得するスクリプトのサンプルです。 このサンプルでは認証にはAPIトークンを利用しています。
実行時には以下を指定してください。
base_url
: 接続先intdash APIのベースURLapi_token
: ユーザーのAPIトークン(APIトークンはMy Pageで作成できます)project_uuid
: プロジェクトのUUIDmeas_uuid
: 計測のUUID
import argparse
import io
import json
import apiclient
from apiclient.api import meas_data_points_api
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--base_url", default="http://localhost:8080/api")
parser.add_argument(
"--api_token", default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
parser.add_argument(
"--project_uuid", default="00000000-0000-0000-0000-000000000000"
)
parser.add_argument(
"--meas_uuid", default="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
args = parser.parse_args()
configuration = apiclient.Configuration(
host=args.base_url,
api_key={"IntdashToken": args.api_token},
)
with apiclient.ApiClient(configuration) as api_client:
api_instance = meas_data_points_api.MeasDataPointsApi(api_client=api_client)
try:
stream: io.BufferedReader = api_instance.list_project_data_points(
project_uuid=args.project_uuid,
name=args.meas_uuid,
)
while True:
line = stream.readline()
if not line:
break
resp = json.decoder.JSONDecoder().decode(line.decode())
print(resp)
except apiclient.ApiException as e:
print(e)