1+ from typing import Union
2+
13import requests
24from pydantic import HttpUrl
35
46from conSys .con_sys_api import ConnectedSystemsRequestBuilder
57from conSys .constants import APITerms
68
79
8- def list_all_observations (server_addr : HttpUrl , api_root : str = APITerms .API .value ):
10+ def list_all_observations (server_addr : HttpUrl , api_root : str = APITerms .API .value , headers = None ):
911 """
1012 Lists all observations
1113 :return:
@@ -15,12 +17,15 @@ def list_all_observations(server_addr: HttpUrl, api_root: str = APITerms.API.val
1517 .with_api_root (api_root )
1618 .for_resource_type (APITerms .OBSERVATIONS .value )
1719 .build_url_from_base ()
20+ .with_headers (headers )
21+ .with_request_method ('GET' )
1822 .build ())
19- resp = requests . get ( api_request . url , params = api_request . body , headers = api_request . headers )
20- return resp . json ()
23+
24+ return api_request . make_request ()
2125
2226
23- def list_observations_from_datastream (server_addr : HttpUrl , datastream_id : str , api_root : str = APITerms .API .value ):
27+ def list_observations_from_datastream (server_addr : HttpUrl , datastream_id : str , api_root : str = APITerms .API .value ,
28+ headers = None ):
2429 """
2530 Lists all observations of a datastream
2631 :return:
@@ -30,15 +35,17 @@ def list_observations_from_datastream(server_addr: HttpUrl, datastream_id: str,
3035 .with_api_root (api_root )
3136 .for_resource_type (APITerms .DATASTREAMS .value )
3237 .with_resource_id (datastream_id )
33- .for_resource_type (APITerms .OBSERVATIONS .value )
38+ .for_sub_resource_type (APITerms .OBSERVATIONS .value )
3439 .build_url_from_base ()
40+ .with_headers (headers )
41+ .with_request_method ('GET' )
3542 .build ())
36- resp = requests .get (api_request .url , params = api_request .body , headers = api_request .headers )
37- return resp .json ()
3843
44+ return api_request .make_request ()
3945
40- def add_observations_to_datastream (server_addr : HttpUrl , datastream_id : str , request_body : dict ,
41- api_root : str = APITerms .API .value ):
46+
47+ def add_observations_to_datastream (server_addr : HttpUrl , datastream_id : str , request_body : Union [str , dict ],
48+ api_root : str = APITerms .API .value , headers = None ):
4249 """
4350 Adds an observation to a datastream by its id
4451 :return:
@@ -48,15 +55,18 @@ def add_observations_to_datastream(server_addr: HttpUrl, datastream_id: str, req
4855 .with_api_root (api_root )
4956 .for_resource_type (APITerms .DATASTREAMS .value )
5057 .with_resource_id (datastream_id )
51- .for_resource_type (APITerms .OBSERVATIONS .value )
58+ .for_sub_resource_type (APITerms .OBSERVATIONS .value )
5259 .with_request_body (request_body )
5360 .build_url_from_base ()
61+ .with_headers (headers )
62+ .with_request_method ('POST' )
5463 .build ())
55- resp = requests .post (api_request .url , params = api_request .body , headers = api_request .headers )
56- return resp .json ()
5764
65+ return api_request .make_request ()
5866
59- def retrieve_observation_by_id (server_addr : HttpUrl , observation_id : str , api_root : str = APITerms .API .value ):
67+
68+ def retrieve_observation_by_id (server_addr : HttpUrl , observation_id : str , api_root : str = APITerms .API .value ,
69+ headers = None ):
6070 """
6171 Retrieves an observation by its id
6272 :return:
@@ -67,13 +77,15 @@ def retrieve_observation_by_id(server_addr: HttpUrl, observation_id: str, api_ro
6777 .for_resource_type (APITerms .OBSERVATIONS .value )
6878 .with_resource_id (observation_id )
6979 .build_url_from_base ()
80+ .with_headers (headers )
81+ .with_request_method ('GET' )
7082 .build ())
71- resp = requests . get ( api_request . url , params = api_request . body , headers = api_request . headers )
72- return resp . json ()
83+
84+ return api_request . make_request ()
7385
7486
75- def update_observation_by_id (server_addr : HttpUrl , observation_id : str , request_body : dict ,
76- api_root : str = APITerms .API .value ):
87+ def update_observation_by_id (server_addr : HttpUrl , observation_id : str , request_body : Union [ str , dict ] ,
88+ api_root : str = APITerms .API .value , headers = None ):
7789 """
7890 Updates an observation by its id
7991 :return:
@@ -85,12 +97,15 @@ def update_observation_by_id(server_addr: HttpUrl, observation_id: str, request_
8597 .with_resource_id (observation_id )
8698 .with_request_body (request_body )
8799 .build_url_from_base ()
100+ .with_headers (headers )
101+ .with_request_method ('PUT' )
88102 .build ())
89- resp = requests .put (api_request .url , params = api_request .body , headers = api_request .headers )
90- return resp .json ()
91103
104+ return api_request .make_request ()
92105
93- def delete_observation_by_id (server_addr : HttpUrl , observation_id : str , api_root : str = APITerms .API .value ):
106+
107+ def delete_observation_by_id (server_addr : HttpUrl , observation_id : str , api_root : str = APITerms .API .value ,
108+ headers = None ):
94109 """
95110 Deletes an observation by its id
96111 :return:
@@ -101,6 +116,8 @@ def delete_observation_by_id(server_addr: HttpUrl, observation_id: str, api_root
101116 .for_resource_type (APITerms .OBSERVATIONS .value )
102117 .with_resource_id (observation_id )
103118 .build_url_from_base ()
119+ .with_headers (headers )
120+ .with_request_method ('DELETE' )
104121 .build ())
105- resp = requests . delete ( api_request . url , params = api_request . body , headers = api_request . headers )
106- return resp . json ()
122+
123+ return api_request . make_request ()
0 commit comments