|
| 1 | +Tutorials |
| 2 | +==================== |
| 3 | + |
| 4 | +.. warning:: |
| 5 | + |
| 6 | + This is a work in progress, some features may change and packages may move or be removed |
| 7 | + before the 1.0 release. |
| 8 | + |
| 9 | +Using the API |
| 10 | +============================ |
| 11 | +There are three main ways to interact with the API |
| 12 | + |
| 13 | +1. Direct API calls |
| 14 | +----------------------------------------- |
| 15 | +In the modules for parts 1 and 2, there are separate files that represent the different parts of the API. These files |
| 16 | +contain methods for interacting with the remote server directly. |
| 17 | + |
| 18 | +2. Default API Helper |
| 19 | +----------------------------------------- |
| 20 | +Using the Default API helper allows for repeated calls to the same API server. This is useful when |
| 21 | +building a library on top of this project. |
| 22 | + |
| 23 | +3. Custom API Requests |
| 24 | +----------------------------------------- |
| 25 | +If you need the most flexibility, you can create custom requests using the request builder and API helper objects |
| 26 | + |
| 27 | +.. note:: |
| 28 | + |
| 29 | + The following examples will not convert the responses back into Objects, but it is possible to do so |
| 30 | + using the appropriate class's `model_validate` method on the response object's string representation. |
| 31 | + |
| 32 | +Interacting with Systems |
| 33 | +============================ |
| 34 | + |
| 35 | +List Systems |
| 36 | +----------------------------------------- |
| 37 | +.. note:: |
| 38 | + |
| 39 | + Using a direct call from `Systems` file |
| 40 | +.. code-block:: python |
| 41 | +
|
| 42 | + from consys4py import Systems |
| 43 | +
|
| 44 | + Systems.list_all_systems(server_url) |
| 45 | +
|
| 46 | +Retrieve Specific System |
| 47 | +----------------------------------------- |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + from consys4py import Systems |
| 52 | +
|
| 53 | + Systems.retrieve_system_by_id(server_url, system_id) |
| 54 | +
|
| 55 | +Add/Create System |
| 56 | +----------------------------------------- |
| 57 | +The method for creating a new system does take a string or properly structured `dict`. |
| 58 | +For ease of use, the `SmlJSONBody` or `GeoJSONBody` can be used. They use Pydantic to validate the data. |
| 59 | +Just be sure to convert them to a JSON string before passing them to the `create_new_systems` method. |
| 60 | + |
| 61 | +.. code-block:: python |
| 62 | +
|
| 63 | + from consys4py import Systems |
| 64 | +
|
| 65 | + sml = SmlJSONBody(object_type='SimpleProcess', id=str(random.randint(1000, 9999)), |
| 66 | + description="A Test System inserted from the Python Connected Systems API Client", |
| 67 | + unique_id=f'urn:test:client:sml-single', label=f'Test System - SML Single', |
| 68 | + definition="http://test.com") |
| 69 | +
|
| 70 | + sml_as_str = sml.model_dump_json(exclude_none=True, by_alias=True) |
| 71 | + Systems.create_new_systems(server_url, sml_as_str, uname="test", pword="test", |
| 72 | + headers=sml_json_headers) |
| 73 | +
|
| 74 | +Datastreams and Observations |
| 75 | +============================ |
| 76 | + |
| 77 | +Add a Datastream and Observation |
| 78 | +------------------------------------------------------------ |
| 79 | +.. note:: |
| 80 | + |
| 81 | + This assumes that the user knows the system id and the datastream id |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + from consys4py import Datastreams |
| 86 | +
|
| 87 | + time_schema = TimeSchema(label="Test Datastream Time", definition="http://test.com/Time", name="timestamp", |
| 88 | + uom=URI(href="http://test.com/TimeUOM")) |
| 89 | + bool_schema = BooleanSchema(label="Test Datastream Boolean", definition="http://test.com/Boolean", |
| 90 | + name="testboolean") |
| 91 | + datarecord_schema = SWEDatastreamSchema(encoding=JSONEncoding(), obs_format=ObservationFormat.SWE_JSON.value, |
| 92 | + record_schema=DataRecordSchema(label="Test Datastream Record", |
| 93 | + definition="http://test.com/Record", |
| 94 | + fields=[time_schema, bool_schema])) |
| 95 | +
|
| 96 | + datastream_body = DatastreamBodyJSON(name="Test Datastream", output_name="Test Output #1", datastream_schema=datarecord_schema) |
| 97 | + temp_test_json = datastream_body.model_dump_json(exclude_none=True, by_alias=True) |
| 98 | +
|
| 99 | + resp = Datastreams.add_datastreams_to_system(server_url, retrieved_systems[1]['id'], |
| 100 | + datastream_body.model_dump_json(exclude_none=True, by_alias=True), |
| 101 | + headers=json_headers) |
| 102 | +
|
| 103 | + the_time = datetime.utcnow().isoformat() + 'Z' |
| 104 | + time_millis = test_time_start.timestamp() * 1000 |
| 105 | +
|
| 106 | + obs = ObservationOMJSONInline(phenomenon_time=the_time, |
| 107 | + result_time=the_time, |
| 108 | + result={ |
| 109 | + "timestamp": time_millis, |
| 110 | + "testboolean": True |
| 111 | + }) |
| 112 | + print(f'Observation: {obs.model_dump_json(exclude_none=True, by_alias=True)}') |
| 113 | + resp = Observations.add_observations_to_datastream(server_url, ds_id, |
| 114 | + obs.model_dump_json(exclude_none=True, by_alias=True), |
| 115 | + headers=json_headers) |
0 commit comments