Skip to content

Commit ccdc0b0

Browse files
Fix type conflicts in APIHelper and ConnectedSystemsAPIRequest
1 parent 9406c3c commit ccdc0b0

File tree

4 files changed

+75
-35
lines changed

4 files changed

+75
-35
lines changed

conSys4Py/con_sys_api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Union
2+
13
from pydantic import BaseModel, HttpUrl, Field
24

35
from conSys4Py.endpoints import Endpoint
@@ -6,11 +8,11 @@
68

79
class ConnectedSystemAPIRequest(BaseModel):
810
url: HttpUrl = Field(None)
9-
body: dict = Field(None)
11+
body: Union[dict, str] = Field(None)
1012
params: dict = Field(None)
1113
request_method: str = Field('GET')
1214
headers: dict = Field(None)
13-
auth: tuple = Field(None)
15+
auth: Union[tuple, None] = Field(None)
1416

1517
def make_request(self):
1618
match self.request_method:

conSys4Py/core/default_api_helpers.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ class APIHelper(ABC):
8181
user_auth: bool = False
8282

8383
def create_resource(self, res_type: APIResourceTypes, json_data: any, parent_res_id: str = None,
84-
from_collection: bool = False, url_endpoint: str = None):
84+
from_collection: bool = False, url_endpoint: str = None, req_headers: dict = None):
8585
"""
8686
Creates a resource of the given type with the given data, will attempt to create a sub-resource if parent_res_id
8787
is provided.
88+
:param req_headers:
8889
:param res_type:
8990
:param json_data:
9091
:param parent_res_id:
@@ -98,15 +99,16 @@ def create_resource(self, res_type: APIResourceTypes, json_data: any, parent_res
9899
else:
99100
url = f'{self.server_url}/{self.api_root}/{url_endpoint}'
100101
api_request = ConnectedSystemAPIRequest(url=url, request_method='POST', auth=self.get_helper_auth(),
101-
body=json_data)
102+
body=json_data, headers=req_headers)
102103
return api_request.make_request()
103104

104105
def retrieve_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_id: str = None,
105106
from_collection: bool = False,
106-
collection_id: str = None, url_endpoint: str = None):
107+
collection_id: str = None, url_endpoint: str = None, req_headers: dict = None):
107108
"""
108109
Retrieves a resource or list of resources if no res_id is provided, will attempt to retrieve a sub-resource if
109110
parent_res_id is provided.
111+
:param req_headers:
110112
:param res_type:
111113
:param res_id:
112114
:param parent_res_id:
@@ -119,14 +121,16 @@ def retrieve_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_
119121
url = self.resource_url_resolver(res_type, None, parent_res_id, from_collection)
120122
else:
121123
url = f'{self.server_url}/{self.api_root}/{url_endpoint}'
122-
api_request = ConnectedSystemAPIRequest(url=url, request_method='GET', auth=self.get_helper_auth())
124+
api_request = ConnectedSystemAPIRequest(url=url, request_method='GET', auth=self.get_helper_auth(),
125+
headers=req_headers)
123126
return api_request.make_request()
124127

125128
def update_resource(self, res_type: APIResourceTypes, res_id: str, json_data: any, parent_res_id: str = None,
126-
from_collection: bool = False, url_endpoint: str = None):
129+
from_collection: bool = False, url_endpoint: str = None, req_headers: dict = None):
127130
"""
128131
Updates a resource of the given type by its id, if necessary, will attempt to update a sub-resource if
129132
parent_res_id is provided.
133+
:param req_headers:
130134
:param res_type:
131135
:param res_id:
132136
:param json_data:
@@ -140,14 +144,15 @@ def update_resource(self, res_type: APIResourceTypes, res_id: str, json_data: an
140144
else:
141145
url = f'{self.server_url}/{self.api_root}/{url_endpoint}'
142146
api_request = ConnectedSystemAPIRequest(url=url, request_method='PUT', auth=self.get_helper_auth(),
143-
body=json_data)
147+
body=json_data, headers=req_headers)
144148
return api_request.make_request()
145149

146150
def delete_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_id: str = None,
147-
from_collection: bool = False, url_endpoint: str = None):
151+
from_collection: bool = False, url_endpoint: str = None, req_headers: dict = None):
148152
"""
149153
Deletes a resource of the given type by its id, if necessary, will attempt to delete a sub-resource if
150154
parent_res_id is provided.
155+
:param req_headers:
151156
:param res_type:
152157
:param res_id:
153158
:param parent_res_id:
@@ -159,7 +164,8 @@ def delete_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_id
159164
url = self.resource_url_resolver(res_type, None, parent_res_id, from_collection)
160165
else:
161166
url = f'{self.server_url}/{self.api_root}/{url_endpoint}'
162-
api_request = ConnectedSystemAPIRequest(url=url, request_method='DELETE', auth=self.get_helper_auth())
167+
api_request = ConnectedSystemAPIRequest(url=url, request_method='DELETE', auth=self.get_helper_auth(),
168+
headers=req_headers)
163169
return api_request.make_request()
164170

165171
# Helpers

tests/test_commands.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ def test_setup():
4242
definition="http://test.com/Record",
4343
fields=[time_schema, count_schema]))
4444
request_body = ControlStreamJSONSchema(name="Test Control Channel", input_name="TestControlInput1",
45-
schema=control_schema)
45+
control_stream_schema=control_schema)
4646
ctl_resp = ControlChannels.add_control_streams_to_system(server_url, system_id,
4747
request_body.model_dump_json(exclude_none=True,
4848
by_alias=True),
4949
headers=json_headers)
5050
print(ctl_resp)
5151

5252
control_streams = ControlChannels.list_all_control_streams(server_url).json()
53-
command_json = CommandJSON(control_id=control_streams["items"][0]["id"],
54-
issue_time=datetime.now().isoformat() + 'Z',
55-
params={"timestamp": datetime.now().timestamp() * 1000, "testcount": 1})
56-
57-
print(f'Issuing Command: {command_json.model_dump_json(exclude_none=True, by_alias=True)}')
58-
cmd_resp = Commands.send_commands_to_specific_control_stream(server_url, control_streams["items"][0]["id"],
59-
command_json.model_dump_json(exclude_none=True,
60-
by_alias=True),
61-
headers=json_headers)
62-
print(cmd_resp)
53+
# command_json = CommandJSON(control_id=control_streams["items"][0]["id"],
54+
# issue_time=datetime.now().isoformat() + 'Z',
55+
# params={"timestamp": datetime.now().timestamp() * 1000, "testcount": 1})
56+
#
57+
# print(f'Issuing Command: {command_json.model_dump_json(exclude_none=True, by_alias=True)}')
58+
# cmd_resp = Commands.send_commands_to_specific_control_stream(server_url, control_streams["items"][0]["id"],
59+
# command_json.model_dump_json(exclude_none=True,
60+
# by_alias=True),
61+
# headers=json_headers)
62+
# print(cmd_resp)
6363

6464

6565
def test_subscribe_and_command():
@@ -97,19 +97,32 @@ def on_message_all(client, userdata, msg):
9797
issue_time=datetime.now().isoformat() + 'Z',
9898
params={"timestamp": datetime.now().timestamp() * 1000, "testcount": 1})
9999

100-
# print(f'Issuing Command: {command_json.model_dump_json(exclude_none=True, by_alias=True)}')
101-
# cmd_resp = Commands.send_commands_to_specific_control_stream(server_url, control_streams["items"][0]["id"],
102-
# command_json.model_dump_json(exclude_none=True,
103-
# by_alias=True),
104-
# headers=json_headers)
100+
print(f'Issuing Command: {command_json.model_dump_json(exclude_none=True, by_alias=True)}')
101+
cmd_resp = Commands.send_commands_to_specific_control_stream(server_url, control_streams["items"][0]["id"],
102+
command_json.model_dump_json(exclude_none=True,
103+
by_alias=True),
104+
headers=json_headers)
105105
# try issuing a command from the MQTT client
106-
mqtt_client.publish(f'/api/controls/{control_id}/commands', command_json.model_dump_json(exclude_none=True,
107-
by_alias=True),
108-
1)
106+
# mqtt_client.publish(f'/api/controls/{control_id}/commands', command_json.model_dump_json(exclude_none=True,
107+
# by_alias=True),
108+
# 1)
109109
# print(f'\n*****Command Response: {cmd_resp}*****')
110110
status_resp = {
111111
'id': '*******',
112112
'command@id': "unknown",
113113
'statusCode': 'COMPLETED'
114114
}
115-
Commands.add_command_status_reports(server_url, "0", json.dumps(status_resp))
115+
# Commands.add_command_status_reports(server_url, "0", json.dumps(status_resp))
116+
117+
118+
def test_command_dahua():
119+
system_id = "tstk16o31es4m"
120+
control_stream_id = "k08p16h6k4a6c"
121+
control_input = CommandJSON(control_id=control_stream_id, issue_time=datetime.now().isoformat() + 'Z',
122+
params={"pan": 180})
123+
print(f'Issuing Command: {control_input.model_dump_json(exclude_none=True, by_alias=True)}')
124+
cmd_resp = Commands.send_commands_to_specific_control_stream(server_url, control_stream_id,
125+
control_input.model_dump_json(exclude_none=True,
126+
by_alias=True),
127+
headers=json_headers)
128+
print(f'\n*****Command Response: {cmd_resp}*****')

tests/test_default_api_helpers.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import conSys4Py.core.default_api_helpers as helpers
2-
from conSys4Py import APIResourceTypes
2+
from conSys4Py import APIResourceTypes, GeoJSONBody
3+
4+
server_url = 'http://localhost:8282/sensorhub'
5+
api_endpoint = 'api'
6+
geojson_headers = {"Content-Type": "application/geo+json"}
37

48

59
def test_determine_parent_type():
@@ -34,8 +38,8 @@ def test_resource_type_to_ep():
3438

3539

3640
def test_resolve_resource_ep():
37-
server_url = 'http://localhost:8181'
38-
api_endpoint = 'api'
41+
# server_url = 'http://localhost:8181'
42+
# api_endpoint = 'api'
3943
api_helper = helpers.APIHelper(server_url, api_endpoint)
4044
assert api_helper.server_url == server_url
4145
assert api_helper.api_root == api_endpoint
@@ -49,11 +53,26 @@ def test_resolve_resource_ep():
4953

5054

5155
def test_user_auth():
52-
server_url = 'http://localhost:8181'
53-
api_endpoint = 'api'
56+
# server_url = 'http://localhost:8181'
57+
# api_endpoint = 'api'
5458
uname = 'user'
5559
pword = 'pass'
5660
api_helper = helpers.APIHelper(server_url, api_endpoint, uname, pword, True)
5761
assert api_helper.get_helper_auth() == ('user', 'pass')
5862
api_helper.user_auth = False
5963
assert api_helper.get_helper_auth() is None
64+
65+
66+
def test_create_resource_system():
67+
system_obj = GeoJSONBody(type='Feature', id='',
68+
properties={
69+
"featureType": "http://www.w3.org/ns/ssn/System",
70+
"name": f'Test System - APIHelper',
71+
"uid": f'urn:test:client:apihelper',
72+
"description": "A Test System inserted using the APIHelper class"
73+
})
74+
api_helper = helpers.APIHelper(server_url, api_endpoint)
75+
result = api_helper.create_resource(res_type=APIResourceTypes.SYSTEM,
76+
json_data=system_obj.model_dump_json(exclude_none=True, by_alias=True),
77+
req_headers=geojson_headers)
78+
print(result)

0 commit comments

Comments
 (0)