Skip to content

Commit 19a8191

Browse files
prep for testing commands and update the helpers
1 parent f249957 commit 19a8191

3 files changed

Lines changed: 203 additions & 47 deletions

File tree

conSys/datamodels/commands.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from datetime import datetime
2+
from typing import Union
3+
4+
from pydantic import BaseModel, Field
5+
6+
7+
class CommandJSON(BaseModel):
8+
"""
9+
A class to represent a command in JSON format
10+
"""
11+
control_id: str = Field(None, serialization_alias="control@id")
12+
issue_time: Union[str, float] = Field(datetime.now().isoformat(), serialization_alias="issueTime")
13+
sender: str = Field(None)
14+
params: Union[dict, list, int, float, str] = Field(None)

conSys/part_2/commands.py

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import requests
1+
from typing import Union
2+
23
from pydantic import HttpUrl
34

45
from conSys.con_sys_api import ConnectedSystemsRequestBuilder
56
from conSys.constants import APITerms
67

78

8-
def list_all_commands(server_addr: HttpUrl, api_root: str = APITerms.API.value):
9+
def list_all_commands(server_addr: HttpUrl, api_root: str = APITerms.API.value, headers: dict = None):
910
"""
1011
Lists all commands
1112
:return:
@@ -15,12 +16,15 @@ def list_all_commands(server_addr: HttpUrl, api_root: str = APITerms.API.value):
1516
.with_api_root(api_root)
1617
.for_resource_type(APITerms.COMMANDS.value)
1718
.build_url_from_base()
19+
.with_headers(headers)
20+
.with_request_method('GET')
1821
.build())
19-
resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers)
20-
return resp.json()
22+
23+
return api_request.make_request()
2124

2225

23-
def list_commands_of_control_channel(server_addr: HttpUrl, control_channel_id: str, api_root: str = APITerms.API.value):
26+
def list_commands_of_control_channel(server_addr: HttpUrl, control_channel_id: str, api_root: str = APITerms.API.value,
27+
headers=None):
2428
"""
2529
Lists all commands of a control channel
2630
:return:
@@ -30,15 +34,18 @@ def list_commands_of_control_channel(server_addr: HttpUrl, control_channel_id: s
3034
.with_api_root(api_root)
3135
.for_resource_type(APITerms.CONTROL_STREAMS.value)
3236
.with_resource_id(control_channel_id)
33-
.for_resource_type(APITerms.COMMANDS.value)
37+
.for_sub_resource_type(APITerms.COMMANDS.value)
3438
.build_url_from_base()
39+
.with_headers(headers)
40+
.with_request_method('GET')
3541
.build())
36-
resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers)
37-
return resp.json()
42+
43+
return api_request.make_request()
3844

3945

40-
def send_commands_to_specific_control_stream(server_addr: HttpUrl, control_stream_id: str, request_body: dict,
41-
api_root: str = APITerms.API.value):
46+
def send_commands_to_specific_control_stream(server_addr: HttpUrl, control_stream_id: str,
47+
request_body: Union[dict, str],
48+
api_root: str = APITerms.API.value, headers=None):
4249
"""
4350
Sends a command to a control stream by its id
4451
:return:
@@ -48,15 +55,17 @@ def send_commands_to_specific_control_stream(server_addr: HttpUrl, control_strea
4855
.with_api_root(api_root)
4956
.for_resource_type(APITerms.CONTROL_STREAMS.value)
5057
.with_resource_id(control_stream_id)
51-
.for_resource_type(APITerms.COMMANDS.value)
58+
.for_sub_resource_type(APITerms.COMMANDS.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()
64+
65+
return api_request.make_request()
5766

5867

59-
def retrieve_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value):
68+
def retrieve_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value, headers=None):
6069
"""
6170
Retrieves a command by its id
6271
:return:
@@ -67,13 +76,15 @@ def retrieve_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str
6776
.for_resource_type(APITerms.COMMANDS.value)
6877
.with_resource_id(command_id)
6978
.build_url_from_base()
79+
.with_headers(headers)
80+
.with_request_method('GET')
7081
.build())
71-
resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers)
72-
return resp.json()
82+
83+
return api_request.make_request()
7384

7485

75-
def update_command_description(server_addr: HttpUrl, command_id: str, request_body: dict,
76-
api_root: str = APITerms.API.value):
86+
def update_command_description(server_addr: HttpUrl, command_id: str, request_body: Union[dict, str],
87+
api_root: str = APITerms.API.value, headers=None):
7788
"""
7889
Updates a command's description by its id
7990
:return:
@@ -85,12 +96,14 @@ def update_command_description(server_addr: HttpUrl, command_id: str, request_bo
8596
.with_resource_id(command_id)
8697
.with_request_body(request_body)
8798
.build_url_from_base()
99+
.with_headers(headers)
100+
.with_request_method('PUT')
88101
.build())
89-
resp = requests.put(api_request.url, params=api_request.body, headers=api_request.headers)
90-
return resp.json()
91102

103+
return api_request.make_request()
92104

93-
def delete_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value):
105+
106+
def delete_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value, headers=None):
94107
"""
95108
Deletes a command by its id
96109
:return:
@@ -101,12 +114,15 @@ def delete_command_by_id(server_addr: HttpUrl, command_id: str, api_root: str =
101114
.for_resource_type(APITerms.COMMANDS.value)
102115
.with_resource_id(command_id)
103116
.build_url_from_base()
117+
.with_headers(headers)
118+
.with_request_method('DELETE')
104119
.build())
105-
resp = requests.delete(api_request.url, params=api_request.body, headers=api_request.headers)
106-
return resp.json()
120+
121+
return api_request.make_request()
107122

108123

109-
def list_command_status_reports(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value):
124+
def list_command_status_reports(server_addr: HttpUrl, command_id: str, api_root: str = APITerms.API.value,
125+
headers=None):
110126
"""
111127
Lists all status reports of a command by its id
112128
:return:
@@ -116,15 +132,17 @@ def list_command_status_reports(server_addr: HttpUrl, command_id: str, api_root:
116132
.with_api_root(api_root)
117133
.for_resource_type(APITerms.COMMANDS.value)
118134
.with_resource_id(command_id)
119-
.for_resource_type(APITerms.STATUS.value)
135+
.for_sub_resource_type(APITerms.STATUS.value)
120136
.build_url_from_base()
137+
.with_headers(headers)
138+
.with_request_method('GET')
121139
.build())
122-
resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers)
123-
return resp.json()
124140

141+
return api_request.make_request()
125142

126-
def add_command_status_reports(server_addr: HttpUrl, command_id: str, request_body: dict,
127-
api_root: str = APITerms.API.value):
143+
144+
def add_command_status_reports(server_addr: HttpUrl, command_id: str, request_body: Union[dict, str],
145+
api_root: str = APITerms.API.value, headers=None):
128146
"""
129147
Adds a status report to a command by its id
130148
:return:
@@ -134,16 +152,18 @@ def add_command_status_reports(server_addr: HttpUrl, command_id: str, request_bo
134152
.with_api_root(api_root)
135153
.for_resource_type(APITerms.COMMANDS.value)
136154
.with_resource_id(command_id)
137-
.for_resource_type(APITerms.STATUS.value)
155+
.for_sub_resource_type(APITerms.STATUS.value)
138156
.with_request_body(request_body)
139157
.build_url_from_base()
158+
.with_headers(headers)
159+
.with_request_method('POST')
140160
.build())
141-
resp = requests.post(api_request.url, params=api_request.body, headers=api_request.headers)
142-
return resp.json()
161+
162+
return api_request.make_request()
143163

144164

145165
def retrieve_command_status_report_by_id(server_addr: HttpUrl, command_id: str, status_report_id: str,
146-
api_root: str = APITerms.API.value):
166+
api_root: str = APITerms.API.value, headers=None):
147167
"""
148168
Retrieves a status report of a command by its id and status report id
149169
:return:
@@ -153,16 +173,19 @@ def retrieve_command_status_report_by_id(server_addr: HttpUrl, command_id: str,
153173
.with_api_root(api_root)
154174
.for_resource_type(APITerms.COMMANDS.value)
155175
.with_resource_id(command_id)
156-
.for_resource_type(APITerms.STATUS.value)
176+
.for_sub_resource_type(APITerms.STATUS.value)
157177
.with_secondary_resource_id(status_report_id)
158178
.build_url_from_base()
179+
.with_headers(headers)
180+
.with_request_method('GET')
159181
.build())
160-
resp = requests.get(api_request.url, params=api_request.body, headers=api_request.headers)
161-
return resp.json()
182+
183+
return api_request.make_request()
162184

163185

164186
def update_command_status_report_by_id(server_addr: HttpUrl, command_id: str, status_report_id: str,
165-
request_body: dict, api_root: str = APITerms.API.value):
187+
request_body: Union[dict, str], api_root: str = APITerms.API.value,
188+
headers=None):
166189
"""
167190
Updates a status report of a command by its id and status report id
168191
:return:
@@ -172,17 +195,19 @@ def update_command_status_report_by_id(server_addr: HttpUrl, command_id: str, st
172195
.with_api_root(api_root)
173196
.for_resource_type(APITerms.COMMANDS.value)
174197
.with_resource_id(command_id)
175-
.for_resource_type(APITerms.STATUS.value)
198+
.for_sub_resource_type(APITerms.STATUS.value)
176199
.with_secondary_resource_id(status_report_id)
177200
.with_request_body(request_body)
178201
.build_url_from_base()
202+
.with_headers(headers)
203+
.with_request_method('PUT')
179204
.build())
180-
resp = requests.put(api_request.url, params=api_request.body, headers=api_request.headers)
181-
return resp.json()
205+
206+
return api_request.make_request()
182207

183208

184209
def delete_command_status_report_by_id(server_addr: HttpUrl, command_id: str, status_report_id: str,
185-
api_root: str = APITerms.API.value):
210+
api_root: str = APITerms.API.value, headers=None):
186211
"""
187212
Deletes a status report of a command by its id and status report id
188213
:return:
@@ -192,9 +217,11 @@ def delete_command_status_report_by_id(server_addr: HttpUrl, command_id: str, st
192217
.with_api_root(api_root)
193218
.for_resource_type(APITerms.COMMANDS.value)
194219
.with_resource_id(command_id)
195-
.for_resource_type(APITerms.STATUS.value)
220+
.for_sub_resource_type(APITerms.STATUS.value)
196221
.with_secondary_resource_id(status_report_id)
197222
.build_url_from_base()
223+
.with_headers(headers)
224+
.with_request_method('DELETE')
198225
.build())
199-
resp = requests.delete(api_request.url, params=api_request.body, headers=api_request.headers)
200-
return resp.json()
226+
227+
return api_request.make_request()

0 commit comments

Comments
 (0)