|
| 1 | +""" |
| 2 | +How to use Invocable scripts Cloud API to create custom endpoints that query data |
| 3 | +""" |
| 4 | +import datetime |
| 5 | + |
| 6 | +from influxdb_client import InfluxDBClient, InvocableScriptsService, ScriptCreateRequest, ScriptInvocationParams, \ |
| 7 | + ScriptLanguage |
| 8 | + |
| 9 | +""" |
| 10 | +Define credentials |
| 11 | +""" |
| 12 | +influx_cloud_url = 'https://us-west-2-1.aws.cloud2.influxdata.com' |
| 13 | +influx_cloud_token = '...' |
| 14 | +bucket_name = '...' |
| 15 | +org_name = '...' |
| 16 | + |
| 17 | +with InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token, org=org_name, debug=False, timeout=20_000) as client: |
| 18 | + uniqueId = str(datetime.datetime.now()) |
| 19 | + """ |
| 20 | + Find Organization ID by Organization API. |
| 21 | + """ |
| 22 | + org = client.organizations_api().find_organizations(org=org_name)[0] |
| 23 | + |
| 24 | + scripts_service = InvocableScriptsService(api_client=client.api_client) |
| 25 | + |
| 26 | + """ |
| 27 | + Create Invocable Script |
| 28 | + """ |
| 29 | + print(f"------- Create -------\n") |
| 30 | + create_request = ScriptCreateRequest(name=f"my_scrupt_{uniqueId}", |
| 31 | + description="my first try", |
| 32 | + language=ScriptLanguage.FLUX, |
| 33 | + org_id=org.id, |
| 34 | + script=f"from(bucket: params.bucket_name) |> range(start: -30d) |> limit(n:2)") |
| 35 | + |
| 36 | + created_script = scripts_service.post_scripts(script_create_request=create_request) |
| 37 | + print(created_script) |
| 38 | + |
| 39 | + """ |
| 40 | + Invoke a script |
| 41 | + """ |
| 42 | + print(f"\n------- Invoke -------\n") |
| 43 | + response = scripts_service.post_scripts_id_invoke(script_id=created_script.id, |
| 44 | + script_invocation_params=ScriptInvocationParams( |
| 45 | + params={"bucket_name": bucket_name})) |
| 46 | + print(response) |
| 47 | + |
| 48 | + """ |
| 49 | + List scripts |
| 50 | + """ |
| 51 | + print(f"\n------- List -------\n") |
| 52 | + scripts = scripts_service.get_scripts().scripts |
| 53 | + print("\n".join([f" ---\n ID: {it.id}\n Name: {it.name}\n Description: {it.description}" for it in scripts])) |
| 54 | + print("---") |
| 55 | + |
| 56 | + """ |
| 57 | + Delete previously created Script |
| 58 | + """ |
| 59 | + print(f"------- Delete -------\n") |
| 60 | + scripts_service.delete_scripts_id(script_id=created_script.id) |
| 61 | + print(f" Successfully deleted script: '{created_script.name}'") |
0 commit comments