1+ #pragma once
2+
3+ #include < string>
4+
5+ #include " APIRequest.h"
6+ #include " APIResponse.h"
7+ #include " DataModels/Command.h"
8+ #include " Query/CommandsQuery.h"
9+ #include " Query/CommandsOfControlStreamQuery.h"
10+
11+ namespace ConnectedSystemsAPI {
12+ // / <summary>
13+ // / API for interacting with commands in the Connected Systems API.
14+ // / </summary>
15+ class CommandsAPI {
16+ private:
17+ std::string apiRoot;
18+ std::string authHeader;
19+
20+ public:
21+ CommandsAPI () = default ;
22+ CommandsAPI (const std::string& apiRoot, const std::string& authHeader)
23+ : apiRoot(apiRoot), authHeader(authHeader) {
24+ }
25+
26+ // / <summary>
27+ // / List or search all commands available from this server endpoint.
28+ // / </summary>
29+ // / <param name="query">The query string parameters.</param>
30+ // / <returns>A response object containing a list of commands.</returns>
31+ APIResponse<DataModels::Command> fetchCommands (const Query::CommandsQuery& query) const {
32+ return fetchCommands (query.toString ());
33+ }
34+
35+ // / <summary>
36+ // / List or search all commands available from this server endpoint.
37+ // / </summary>
38+ // / <param name="queryString">The query string.</param>
39+ // / <returns>A response object containing a list of commands.</returns>
40+ APIResponse<DataModels::Command> fetchCommands (const std::string& queryString = " " ) const {
41+ auto response = APIRequest::Builder ()
42+ .setApiRoot (apiRoot)
43+ .setMethod (" GET" )
44+ .setAuthHeader (authHeader)
45+ .setResourcePath (" commands" )
46+ .setQueryString (queryString)
47+ .build ()
48+ .execute <DataModels::Command>();
49+ return response;
50+ }
51+
52+ // / <summary>
53+ // / List or search all commands available from a control stream.
54+ // / </summary>
55+ // / <param name="controlStreamId">The local identifier of the control stream.</param>
56+ // / <param name="query">The query string parameters.</param>
57+ // / <returns>A response object containing a list of commands.</returns>
58+ APIResponse<DataModels::Command> fetchCommandsOfControlStream (const std::string& controlStreamId, const Query::CommandsOfControlStreamQuery& query) const {
59+ return fetchCommandsOfControlStream (controlStreamId, query.toString ());
60+ }
61+
62+ // / <summary>
63+ // / List or search all commands available from a control stream.
64+ // / </summary>
65+ // / <param name="controlStreamId">The local identifier of the control stream.</param>
66+ // / <param name="queryString">The query string.</param>
67+ // / <returns>A response object containing a list of commands.</returns>
68+ APIResponse<DataModels::Command> fetchCommandsOfControlStream (const std::string& controlStreamId, const std::string& queryString = " " ) const {
69+ if (controlStreamId.empty ())
70+ return APIResponse<DataModels::Command>(400 , " Invalid controlStreamId" , " " , {});
71+
72+ auto response = APIRequest::Builder ()
73+ .setApiRoot (apiRoot)
74+ .setMethod (" GET" )
75+ .setAuthHeader (authHeader)
76+ .setResourcePath (" controlstreams" )
77+ .setResourceId (controlStreamId)
78+ .setSubResourcePath (" commands" )
79+ .setQueryString (queryString)
80+ .build ()
81+ .execute <DataModels::Command>();
82+ return response;
83+ }
84+
85+ // / <summary>
86+ // / Get a specific command by its ID.
87+ // / </summary>
88+ // / <param name="commandId">The ID of the command to retrieve.</param>
89+ // / <returns>A response object containing the requested command.</returns>
90+ APIResponse<DataModels::Command> fetchCommandById (const std::string& commandId) const {
91+ if (commandId.empty ())
92+ return APIResponse<DataModels::Command>(400 , " Invalid commandId" , " " , {});
93+
94+ auto response = APIRequest::Builder ()
95+ .setApiRoot (apiRoot)
96+ .setMethod (" GET" )
97+ .setAuthHeader (authHeader)
98+ .setResourcePath (" commands" )
99+ .setResourceId (commandId)
100+ .build ()
101+ .execute <DataModels::Command>();
102+ return response;
103+ }
104+
105+ // / <summary>
106+ // / Add a new command to an existing control stream.
107+ // / </summary>
108+ // / <param name="controlStreamId">The local identifier of the control stream.</param>
109+ // / <param name="command">The command to create.</param>
110+ // / <returns>A response object indicating success or failure.</returns>
111+ APIResponse<void > createCommand (const std::string& controlStreamId, const DataModels::Command& command) const {
112+ if (controlStreamId.empty ())
113+ return APIResponse<void >(400 , " Invalid controlStreamId" , " " , {});
114+
115+ auto response = APIRequest::Builder ()
116+ .setApiRoot (apiRoot)
117+ .setMethod (" POST" )
118+ .setAuthHeader (authHeader)
119+ .addHeader (" Content-Type" , " application/json" )
120+ .setResourcePath (" controlstreams" )
121+ .setResourceId (controlStreamId)
122+ .setSubResourcePath (" commands" )
123+ .setBody (command.toJson ().dump ())
124+ .build ()
125+ .execute <void >();
126+ return response;
127+ }
128+
129+ // / <summary>
130+ // / Update an existing command.
131+ // / </summary>
132+ // / <param name="commandId">The ID of the command to update.</param>
133+ // / <param name="command">The updated command.</param>
134+ // / <returns>A response object indicating success or failure.</returns>
135+ APIResponse<void > updateCommand (const std::string& commandId, const DataModels::Command& command) const {
136+ if (commandId.empty ())
137+ return APIResponse<void >(400 , " Invalid commandId" , " " , {});
138+
139+ auto response = APIRequest::Builder ()
140+ .setApiRoot (apiRoot)
141+ .setMethod (" PUT" )
142+ .setAuthHeader (authHeader)
143+ .addHeader (" Content-Type" , " application/json" )
144+ .setResourcePath (" commands" )
145+ .setResourceId (commandId)
146+ .setBody (command.toJson ().dump ())
147+ .build ()
148+ .execute <void >();
149+ return response;
150+ }
151+
152+ // / <summary>
153+ // / Delete a command by its ID.
154+ // / </summary>
155+ // / <param name="commandId">The ID of the command to delete.</param>
156+ // / <returns>A response object indicating success or failure.</returns>
157+ APIResponse<void > deleteCommand (const std::string& commandId) const {
158+ if (commandId.empty ())
159+ return APIResponse<void >(400 , " Invalid commandId" , " " , {});
160+
161+ auto response = APIRequest::Builder ()
162+ .setApiRoot (apiRoot)
163+ .setMethod (" DELETE" )
164+ .setAuthHeader (authHeader)
165+ .setResourcePath (" commands" )
166+ .setResourceId (commandId)
167+ .build ()
168+ .execute <void >();
169+ return response;
170+ }
171+ };
172+ }
0 commit comments