Improve readability of cached_endpoint.
Switched the conditional and pulled it out into a separate function so I can return early and dedent the rest.
This commit is contained in:
		
							parent
							
								
									b9ad785f4d
								
							
						
					
					
						commit
						af73bc580f
					
				
					 1 changed files with 19 additions and 13 deletions
				
			
		|  | @ -40,21 +40,27 @@ def cached_endpoint(max_age): | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     def wrapper(function): |     def wrapper(function): | ||||||
|         @functools.wraps(function) |         def get_value(*args, **kwargs): | ||||||
|         def wrapped(*args, **kwargs): |             if state['max_age'] and (time.time() - state['last_run']) > state['max_age']: | ||||||
|             if (not state['max_age']) or (time.time() - state['last_run'] > state['max_age']): |                 return state['stored_value'] | ||||||
|  | 
 | ||||||
|             value = function(*args, **kwargs) |             value = function(*args, **kwargs) | ||||||
|             if isinstance(value, flask.Response): |             if isinstance(value, flask.Response): | ||||||
|                 if value.headers.get('Content-Type'): |                 if value.headers.get('Content-Type'): | ||||||
|                     state['headers']['Content-Type'] = value.headers.get('Content-Type') |                     state['headers']['Content-Type'] = value.headers.get('Content-Type') | ||||||
|                 value = value.response |                 value = value.response | ||||||
|  | 
 | ||||||
|             if value != state['stored_value']: |             if value != state['stored_value']: | ||||||
|                 state['stored_value'] = value |                 state['stored_value'] = value | ||||||
|                 state['stored_etag'] = passwordy.random_hex(20) |                 state['stored_etag'] = passwordy.random_hex(20) | ||||||
|                 state['headers']['ETag'] = state['stored_etag'] |                 state['headers']['ETag'] = state['stored_etag'] | ||||||
|  | 
 | ||||||
|             state['last_run'] = time.time() |             state['last_run'] = time.time() | ||||||
|             else: |             return value | ||||||
|                 value = state['stored_value'] | 
 | ||||||
|  |         @functools.wraps(function) | ||||||
|  |         def wrapped(*args, **kwargs): | ||||||
|  |             value = get_value(*args, **kwargs) | ||||||
| 
 | 
 | ||||||
|             client_etag = request.headers.get('If-None-Match', None) |             client_etag = request.headers.get('If-None-Match', None) | ||||||
|             if client_etag == state['stored_etag']: |             if client_etag == state['stored_etag']: | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue