Beispiel #1
0
    def on_get(self, req, resp):
        """
        REST endpoint exposing the predict API.
        """
        predict = NLPDisasterPredictor()

        qs = uri.parse_query_string(req.query_string)
        text = qs.get('text')

        resonse_body = {'text': text, 'prediction': predict.predict(text)}

        resp.media = resonse_body
        resp.status = falcon.HTTP_200
Beispiel #2
0
def parse_multiple_parameters(req, resp, resource, params):
    """
    Parse query string and split into search_by and filter.
    Search by applies the criteria search
    Filter applies the fields to be replied
    :return: Dict in request parameter set as query_context
    """
    query_params = parse_query_string(req.query_string)
    req.query_context = dict()

    req.query_context['search_by'] = \
        __parse_search__(query_params.get('search_by')) if 'search_by' in query_params else dict()

    req.query_context['filter'] = __parse_filter__(query_params.get('filter')) if 'filter' in query_params else []
Beispiel #3
0
def pagination_links(raw_obj, uri, req):
    """
    Writes the pagination with the new URI
    :param raw_obj: The object containing the pagination objects
    :param uri: The URI string that originated the request
    """

    if 'links' not in raw_obj:
        return

    for link in raw_obj.get('links'):

        if link.get('rel') == 'next':
            qs = parse_query_string(link.get('href').rsplit('?')[-1])
            # Collect offset and limit
            offset = qs.get('offset', None)
            limit = qs.get('limit', None)

            # Collect base uri
            if '?' in uri:
                uri = req.uri.split('?')[0]
            else:
                uri = req.uri

            # Insert limit and offset for pagination
            uri = uri + '?'
            if offset:
                uri = uri + 'offset=' + offset + '&'
            if limit:
                uri = uri + 'limit=' + limit + '&'

            # Remove old limit and offset
            req.context.get('query_parameters').pop('offset', None)
            req.context.get('query_parameters').pop('limit', None)

            # Append remaining parameters
            query_string = '&'.join([
                '{}={}'.format(key, value)
                for key, value in req.context.get('query_parameters').items()
            ])

            uri = uri + query_string
            link['href'] = uri
            print(uri)

        elif link.get('rel') == 'self':
            link['href'] = req.uri
Beispiel #4
0
    async def on_get(self, req, res, table, id_):
        db_session = req.context['db_session']
        depth = 0

        if req.query_string:
            query = parse_query_string(req.query_string)

            if 'depth' in query:
                try:
                    depth = int(query['depth'])
                except ValueError:
                    pass

        result = await self.select_data(db_session, table, id_, depth=depth)

        if result and "success" in result:
            if result["success"]:
                res.status = HTTP_200
                res.body = json.dumps(result)
            else: raise HTTPBadRequest(description=result["description"])
        else: raise HTTPInternalServerError()
Beispiel #5
0
    async def on_get(self, req, res, id):
        db_session = req.context['db_session']
        user_dbs = db_session.query(self.model).filter(self.model.id == id).first()
        depth = 0

        if req.query_string:
            query = parse_query_string(req.query_string)

            if 'depth' in query:
                try:
                    depth = int(query['depth'])
                except ValueError:
                    pass

        if user_dbs:
            res.status = HTTP_200
            res.body = json.dumps({
                'result' : 'OK',
                'description' : '',
                'data' : json.loads(row.get_data(self.attrs, depth=depth)),
            })