1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 | import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint
userName = "replace with your API user name"
password = "replace with your API password"
# Enter a context with an instance of the API client
with openapi_client.ApiClient() as api_client:
# Create an instance of the API class
api_instance = openapi_client.AuthenticationJWTTokenApi(api_client)
login_request = openapi_client.LoginRequest(userName=userName, password=password)
try:
# Retrieve a JWT token
token_response = api_instance.obtain_token(login_request)
print("The response of AuthenticationJWTTokenApi->obtain_token:\n")
pprint(token_response)
# Set the obtained token in the headers for subsequent requests
api_client.default_headers["Authorization"] = f"Bearer {token_response.jwt_token}"
# Query all data sets
datasets_api = openapi_client.DataSetsApi(api_client)
datasets_response = datasets_api.get_all_data_sets()
pprint(datasets_response)
# Create a new project
projects_api = openapi_client.ProjectsApi(api_client)
create_project_response = projects_api.create_project(openapi_client.CreatePublicProjectRequest(
name="My first project",
description="My first project created through the REST API"))
pprint(create_project_response)
# Attach the first data set to the project
projects_resources_datasets_api = openapi_client.ProjectResourcesDataSetsApi(api_client)
projects_resources_datasets_api.add_data_set_to_project(
project_id=create_project_response.id,
public_add_data_set_to_project_request=openapi_client.PublicAddDataSetToProjectRequest(
data_set_id=datasets_response[0].id
))
# Widgets can only be added to a group, so first create a new widget group
project_resources_widgets_api = openapi_client.ProjectResourcesWidgetsApi(api_client)
create_widget_group_response = project_resources_widgets_api.create_widget_group(
project_id=create_project_response.id,
create_public_widget_group_request=openapi_client.CreatePublicWidgetGroupRequest(
name="My API created widget group"
)
)
# Create the properties for the dataset
properties_per_data_set = {
datasets_response[0].id: openapi_client.PublicVisualAnalyticsDataSetProperties(
gridded_display_options=openapi_client.PublicVisualAnalyticsGriddedDisplayOptions(
mode='COLOR_BY_NUMBER_OF_RECORDS',
color_by_number_of_records_color_map={
"heat_map": {
"name": 'RAINBOW'
}
}
)
)
}
# Add a visual analytics widget to the group
project_resources_widgets_api.create_visual_analytics_widget(
project_id=create_project_response.id,
widget_group_id=create_widget_group_response.id,
create_public_visual_analytics_widget_request=openapi_client.CreatePublicVisualAnalyticsWidgetRequest(
title="Map fitted on entire area",
properties=openapi_client.PublicVisualAnalyticsWidgetProperties(
widget_type='SPATIAL_MAP',
map_index=0
),
state={
"individual_map_states": [
{
"temporal_range_filter": {
"end_time_type": 'BEFORE_END_OF_DATA',
"end_time_delta_in_seconds": 0,
"start_time_type": 'BEFORE_END_OF_FILTER',
"start_time_delta_in_seconds": 60 * 60 * 24 * 7,
"end_time_in_seconds": 0,
"start_time_in_seconds": 0
},
"data_set_properties": properties_per_data_set,
"time_line_properties": {
"properties": [
{
"data_set_id": datasets_response[0].id,
"mode": 'DISPLAY_NUMBER_OF_ASSETS'
}
]
}
}
]
}
)
)
except ApiException as e:
print("Exception when calling the REST API: %s\n" % e)
|