| Conditions | 13 |
| Total Lines | 89 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like core.timezone.TimezoneItem.on_get() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import falcon |
||
| 180 | @staticmethod |
||
| 181 | def on_get(req, resp, id_): |
||
| 182 | """ |
||
| 183 | Handle GET requests to retrieve a specific timezone by ID |
||
| 184 | |||
| 185 | Retrieves a single timezone with its metadata including: |
||
| 186 | - Timezone ID |
||
| 187 | - Timezone name |
||
| 188 | - Description |
||
| 189 | - UTC offset |
||
| 190 | |||
| 191 | Args: |
||
| 192 | req: Falcon request object |
||
| 193 | resp: Falcon response object |
||
| 194 | id_: Timezone ID to retrieve |
||
| 195 | """ |
||
| 196 | # Check authentication method (API key or session) |
||
| 197 | if 'API-KEY' not in req.headers or \ |
||
| 198 | not isinstance(req.headers['API-KEY'], str) or \ |
||
| 199 | len(str.strip(req.headers['API-KEY'])) == 0: |
||
| 200 | access_control(req) |
||
| 201 | else: |
||
| 202 | api_key_control(req) |
||
| 203 | |||
| 204 | if not id_.isdigit() or int(id_) <= 0: |
||
| 205 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 206 | description='API.INVALID_TIMEZONE_ID') |
||
| 207 | |||
| 208 | # Redis cache key |
||
| 209 | cache_key = f'timezone:item:{id_}' |
||
| 210 | cache_expire = 28800 # 8 hours in seconds (long-term cache) |
||
| 211 | |||
| 212 | # Try to get from Redis cache (only if Redis is enabled) |
||
| 213 | redis_client = None |
||
| 214 | if config.redis.get('is_enabled', False): |
||
| 215 | try: |
||
| 216 | redis_client = redis.Redis( |
||
| 217 | host=config.redis['host'], |
||
| 218 | port=config.redis['port'], |
||
| 219 | password=config.redis['password'] if config.redis['password'] else None, |
||
| 220 | db=config.redis['db'], |
||
| 221 | decode_responses=True, |
||
| 222 | socket_connect_timeout=2, |
||
| 223 | socket_timeout=2 |
||
| 224 | ) |
||
| 225 | redis_client.ping() |
||
| 226 | cached_result = redis_client.get(cache_key) |
||
| 227 | if cached_result: |
||
| 228 | resp.text = cached_result |
||
| 229 | return |
||
| 230 | except Exception: |
||
| 231 | # If Redis connection fails, continue to database query |
||
| 232 | pass |
||
| 233 | |||
| 234 | # Cache miss or Redis error - query database |
||
| 235 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 236 | cursor = cnx.cursor() |
||
| 237 | |||
| 238 | # Query to retrieve specific timezone by ID |
||
| 239 | query = (" SELECT id, name, description, utc_offset " |
||
| 240 | " FROM tbl_timezones " |
||
| 241 | " WHERE id = %s ") |
||
| 242 | cursor.execute(query, (id_,)) |
||
| 243 | row = cursor.fetchone() |
||
| 244 | if row is None: |
||
| 245 | cursor.close() |
||
| 246 | cnx.close() |
||
| 247 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 248 | description='API.TIMEZONE_NOT_FOUND') |
||
| 249 | |||
| 250 | # Build result object |
||
| 251 | result = {"id": row[0], |
||
| 252 | "name": row[1], |
||
| 253 | "description": row[2], |
||
| 254 | "utc_offset": row[3]} |
||
| 255 | |||
| 256 | cursor.close() |
||
| 257 | cnx.close() |
||
| 258 | |||
| 259 | # Store result in Redis cache |
||
| 260 | result_json = json.dumps(result) |
||
| 261 | if redis_client: |
||
| 262 | try: |
||
| 263 | redis_client.setex(cache_key, cache_expire, result_json) |
||
| 264 | except Exception: |
||
| 265 | # If cache set fails, ignore and continue |
||
| 266 | pass |
||
| 267 | |||
| 268 | resp.text = result_json |
||
| 269 | |||
| 334 |