Ejemplo n.º 1
0
def _get_pretty(request):
    pretty = False
    get_params = MultiDict(urllib.parse.parse_qsl(request.query_string.lower()))
    if get_params.get('pretty'):
        if str(get_params.get('pretty', None)) in ['1', 'true']:
            pretty = True
    # Assume pretty if html is requested and pretty is not disabled
    elif 'text/html' in request.headers.get('ACCEPT', ''):
        pretty = True
    return pretty
Ejemplo n.º 2
0
def _get_pretty(request):
    pretty = False
    get_params = MultiDict(urllib.parse.parse_qsl(
        request.query_string.lower()))
    if get_params.get('pretty'):
        if str(get_params.get('pretty', None)) in ['1', 'true']:
            pretty = True
    # Assume pretty if html is requested and pretty is not disabled
    elif 'text/html' in request.headers.get('ACCEPT', ''):
        pretty = True
    return pretty
Ejemplo n.º 3
0
 async def handle_request(self, message, payload):
     path = urlparse(message.path).path.lower()
     if path.startswith(u'/api/v1/limit/'):
         get_params = MultiDict(parse_qsl(urlparse(message.path).query))
         domain = get_params.get('d', None)
         gen_time = get_params.get('g', None)
         if domain is None or gen_time is None:
             await super().handle_error(
                 message, payload, reason='domain or gen_time not provided')
         else:
             await self.handle_limit(message, domain, gen_time)
     else:
         await super().handle_request(message, payload)
Ejemplo n.º 4
0
def get_multi_dict_from_python_dict(resp_headers_dict: dict) -> MultiDictProxy:
    """Construct an :class:`aiohttp.MultiDictProxy` instance from a Python dictionary.

    Note: For now, this method is used for test only.

    .. note::

        Neither Python dictionary nor JSON supports multi-value key.  The response headers returned
        by `aiohttp` is of immutable type :class:`aiohttp.MultiDictProxy` while the one returned by
        `aiohttpretty` is of :class:`aiohttp.MultiDict`.

        WB tests use the :class:`aiohttp.MultiDict` type for both files and folders during modification
        and returns the :class:`aiohttp.MultiDictProxy` type to imitate the behavior of `aiohttp`.

    :param dict resp_headers_dict: the raw response headers dictionary
    :rtype: :class:`aiohttp.MultiDictProxy`
    """

    resp_headers = MultiDict(resp_headers_dict)
    google_hash = resp_headers.get('x-goog-hash', None)
    if google_hash:
        assert verify_raw_google_hash_header(google_hash)
        resp_headers.pop('x-goog-hash')
        google_hash_list = google_hash.split(',')
        for google_hash in google_hash_list:
            resp_headers.add('x-goog-hash', google_hash)

    return MultiDictProxy(resp_headers)
Ejemplo n.º 5
0
def process_generic_get(query_string, allowed_fields):
    # 1- retrieve query parameters
    get_params = MultiDict(parse_qsl(query_string))
    r_range = get_params.get('range', "0-" + str(RANGE_DEFAULT))
    r_fields = get_params.get('fields', None)
    r_order = get_params.get('order_by', None)
    r_sort = get_params.get('order_sort', None)
    r_filter = get_params.get('filter', None)

    # 2- fields to extract
    fields = allowed_fields
    if r_fields is not None:
        fields = []
        for f in r_fields.split(','):
            f = f.strip().lower()
            if f in allowed_fields:
                fields.append(f)
    if len(fields) == 0:
        return rest_error("No valid fields provided : " +
                          get_params.get('fields'))

    # 3- Build json query for mongoengine
    query = {}
    if r_filter is not None:
        query = {"$or": []}
        for k in fields:
            query["$or"].append({k: {'$regex': r_filter}})

    # 4- Order
    order = "name"
    # if r_sort is not None and r_order is not None:
    #     r_sort = r_sort.split(',')
    #     r_order = r_order.split(',')
    #     if len(r_sort) == len(r_order):
    #         order = []
    #         for i in range(0, len(r_sort)):
    #             f = r_sort[i].strip().lower()
    #             if f in allowed_fields:
    #                 if r_order[i] == "desc":
    #                     f = "-" + f
    #                 order.append(f)
    # order = tuple(order)

    # 5- limit
    r_range = r_range.split("-")
    offset = 0
    limit = RANGE_DEFAULT
    try:
        offset = int(r_range[0])
        limit = int(r_range[1])
    except:
        return rest_error("No valid range provided : " +
                          get_params.get('range'))

    # 6- Return processed data
    return fields, query, order, offset, limit
Ejemplo n.º 6
0
def process_generic_get(query_string, allowed_fields):
        # 1- retrieve query parameters
        get_params = MultiDict(parse_qsl(query_string))
        r_range  = get_params.get('range', "0-" + str(RANGE_DEFAULT))
        r_fields = get_params.get('fields', None)
        r_order  = get_params.get('order_by', None)
        r_sort   = get_params.get('order_sort', None)
        r_filter = get_params.get('filter', None)

        # 2- fields to extract
        fields = allowed_fields
        if r_fields is not None:
            fields = []
            for f in r_fields.split(','):
                f = f.strip().lower()
                if f in allowed_fields:
                    fields.append(f)
        if len(fields) == 0:
            return rest_error("No valid fields provided : " + get_params.get('fields'))

        # 3- Build json query for mongoengine
        query = {}
        if r_filter is not None:
            query = {"$or" : []}
            for k in fields:
                query["$or"].append({k : {'$regex': r_filter}})

        # 4- Order
        order = "name"
        # if r_sort is not None and r_order is not None:
        #     r_sort = r_sort.split(',')
        #     r_order = r_order.split(',')
        #     if len(r_sort) == len(r_order):
        #         order = []
        #         for i in range(0, len(r_sort)):
        #             f = r_sort[i].strip().lower()
        #             if f in allowed_fields:
        #                 if r_order[i] == "desc":
        #                     f = "-" + f
        #                 order.append(f)
        # order = tuple(order)

        # 5- limit
        r_range = r_range.split("-")
        offset=0
        limit=RANGE_DEFAULT
        try:
            offset = int(r_range[0])
            limit = int(r_range[1])
        except:
            return rest_error("No valid range provided : " + get_params.get('range') )

        # 6- Return processed data
        return fields, query, order, offset, limit