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