@@ 142-175 (lines=34) @@ | ||
139 | _ = req |
|
140 | resp.status = falcon.HTTP_200 |
|
141 | ||
142 | @staticmethod |
|
143 | def on_get(req, resp, id_): |
|
144 | if 'API-KEY' not in req.headers or \ |
|
145 | not isinstance(req.headers['API-KEY'], str) or \ |
|
146 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
147 | access_control(req) |
|
148 | else: |
|
149 | api_key_control(req) |
|
150 | if not id_.isdigit() or int(id_) <= 0: |
|
151 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
152 | description='API.INVALID_CONTACT_ID') |
|
153 | ||
154 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
155 | cursor = cnx.cursor() |
|
156 | ||
157 | query = (" SELECT id, name, uuid, email, phone, description " |
|
158 | " FROM tbl_contacts " |
|
159 | " WHERE id = %s ") |
|
160 | cursor.execute(query, (id_,)) |
|
161 | row = cursor.fetchone() |
|
162 | cursor.close() |
|
163 | cnx.close() |
|
164 | ||
165 | if row is None: |
|
166 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
167 | description='API.CONTACT_NOT_FOUND') |
|
168 | ||
169 | result = {"id": row[0], |
|
170 | "name": row[1], |
|
171 | "uuid": row[2], |
|
172 | "email": row[3], |
|
173 | "phone": row[4], |
|
174 | "description": row[5]} |
|
175 | resp.text = json.dumps(result) |
|
176 | ||
177 | @staticmethod |
|
178 | @user_logger |
@@ 537-571 (lines=35) @@ | ||
534 | _=req |
|
535 | resp.status = falcon.HTTP_200 |
|
536 | _=id_ |
|
537 | @staticmethod |
|
538 | def on_get(req, resp, id_): |
|
539 | if 'API-KEY' not in req.headers or \ |
|
540 | not isinstance(req.headers['API-KEY'], str) or \ |
|
541 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
542 | access_control(req) |
|
543 | else: |
|
544 | api_key_control(req) |
|
545 | if not id_.isdigit() or int(id_) <= 0: |
|
546 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
547 | description='API.INVALID_COMMAND_ID') |
|
548 | ||
549 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
550 | cursor = cnx.cursor() |
|
551 | ||
552 | query = (" SELECT id, name, uuid, topic, payload, set_value, description " |
|
553 | " FROM tbl_commands " |
|
554 | " WHERE id = %s ") |
|
555 | cursor.execute(query, (id_,)) |
|
556 | row = cursor.fetchone() |
|
557 | cursor.close() |
|
558 | cnx.close() |
|
559 | ||
560 | if row is None: |
|
561 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
562 | description='API.COMMAND_NOT_FOUND') |
|
563 | ||
564 | result = {"id": row[0], |
|
565 | "name": row[1], |
|
566 | "uuid": row[2], |
|
567 | "topic": row[3], |
|
568 | "payload": row[4], |
|
569 | "set_value": row[5], |
|
570 | "description": row[6]} |
|
571 | resp.text = json.dumps(result) |
|
572 | ||
573 | ||
574 | class CommandImport: |
|
@@ 150-184 (lines=35) @@ | ||
147 | _=req |
|
148 | resp.status = falcon.HTTP_200 |
|
149 | _=id_ |
|
150 | @staticmethod |
|
151 | def on_get(req, resp, id_): |
|
152 | if 'API-KEY' not in req.headers or \ |
|
153 | not isinstance(req.headers['API-KEY'], str) or \ |
|
154 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
155 | access_control(req) |
|
156 | else: |
|
157 | api_key_control(req) |
|
158 | if not id_.isdigit() or int(id_) <= 0: |
|
159 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
160 | description='API.INVALID_COMMAND_ID') |
|
161 | ||
162 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
163 | cursor = cnx.cursor() |
|
164 | ||
165 | query = (" SELECT id, name, uuid, topic, payload, set_value, description " |
|
166 | " FROM tbl_commands " |
|
167 | " WHERE id = %s ") |
|
168 | cursor.execute(query, (id_,)) |
|
169 | row = cursor.fetchone() |
|
170 | cursor.close() |
|
171 | cnx.close() |
|
172 | ||
173 | if row is None: |
|
174 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
175 | description='API.COMMAND_NOT_FOUND') |
|
176 | ||
177 | result = {"id": row[0], |
|
178 | "name": row[1], |
|
179 | "uuid": row[2], |
|
180 | "topic": row[3], |
|
181 | "payload": row[4], |
|
182 | "set_value": row[5], |
|
183 | "description": row[6]} |
|
184 | resp.text = json.dumps(result) |
|
185 | ||
186 | @staticmethod |
|
187 | @user_logger |
@@ 131-163 (lines=33) @@ | ||
128 | _=req |
|
129 | resp.status = falcon.HTTP_200 |
|
130 | _=id_ |
|
131 | @staticmethod |
|
132 | def on_get(req, resp, id_): |
|
133 | if 'API-KEY' not in req.headers or \ |
|
134 | not isinstance(req.headers['API-KEY'], str) or \ |
|
135 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
136 | access_control(req) |
|
137 | else: |
|
138 | api_key_control(req) |
|
139 | if not id_.isdigit() or int(id_) <= 0: |
|
140 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
141 | description='API.INVALID_ENERGY_CATEGORY_ID') |
|
142 | ||
143 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
144 | cursor = cnx.cursor() |
|
145 | ||
146 | query = (" SELECT id, name, uuid, unit_of_measure, kgce, kgco2e " |
|
147 | " FROM tbl_energy_categories " |
|
148 | " WHERE id = %s ") |
|
149 | cursor.execute(query, (id_,)) |
|
150 | row = cursor.fetchone() |
|
151 | cursor.close() |
|
152 | cnx.close() |
|
153 | if row is None: |
|
154 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
155 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
|
156 | ||
157 | result = {"id": row[0], |
|
158 | "name": row[1], |
|
159 | "uuid": row[2], |
|
160 | "unit_of_measure": row[3], |
|
161 | "kgce": row[4], |
|
162 | "kgco2e": row[5]} |
|
163 | resp.text = json.dumps(result) |
|
164 | ||
165 | @staticmethod |
|
166 | @user_logger |