""" Flask Request Utils =================== Utils for parsing request values. """ from collections import namedtuple from ows_accounting.constants import header from ows_accounting.constants import pagination Pagination = namedtuple('Pagination', ['offset', 'limit']) GrassAccount = namedtuple('GrassAccount', ['type', 'id']) def get_pagination(request): """Parses pagination information. Args: request (Flask.request): The request object. Returns: Pagination (namedtuple): pagination information. """ page_offset = request.args.get( 'page_offset') or pagination.PAGE_OFFSET_DEFAULT page_offset = _get_int(page_offset, pagination.PAGE_OFFSET_DEFAULT) page_limit = request.args.get( 'page_limit') or pagination.PAGE_LIMIT_DEFAULT page_limit = _get_int(page_limit, pagination.PAGE_LIMIT_DEFAULT) return Pagination(page_offset, page_limit) def _get_int(value, default_value): """Helper method to convert a value to int. Args: value (str or int): The value to convert to int. default_value (int): The default value to use if value cannot be converted. Returns: int: value converted to int. """ try: return int(value) except: # noqa return default_value def get_account_from_grass_headers(request): """Parses Grass Account information. Args: request (Flask.request): The request object. Returns: GrassAccount (namedtuple): grass account information. """ header_account_type = request.headers.get(header.GRASS_ACCOUNT_TYPE) header_account_id = request.headers.get(header.GRASS_ACCOUNT_ID) return GrassAccount(header_account_type, header_account_id)