Ejemplo n.º 1
0
    def _paginate(self, request, collection):
        """Method to paginate through collection result lists.

        Use this to return only a slice of a collection, specified in
        the request itself.  The request should use query parameters
        `count` and `page` to specify the slice they want.  The slice
        will start at index ``(page - 1) * count`` and end (exclusive)
        at ``(page * count)``.
        """
        # Allow falcon's HTTPBadRequest exceptions to percolate up.  They'll
        # get turned into HTTP 400 errors.
        count = request.get_param_as_int('count')
        page = request.get_param_as_int('page')
        total_size = len(collection)
        if count is None and page is None:
            return 0, total_size, collection
        # TODO(maxking): Count and page should be positive integers. Once
        # falcon 2.0.0 is out and we can jump to it, we can remove this logic
        # and use `min_value` parameter in request.get_param_as_int.
        if count < 0:
            raise falcon.HTTPInvalidParam(
                count, 'count should be a positive integer.')
        if page < 1:
            raise falcon.HTTPInvalidParam(
                page, 'page should be greater than 0.')
        list_start = (page - 1) * count
        list_end = page * count
        return list_start, total_size, collection[list_start:list_end]
Ejemplo n.º 2
0
def before_get_it2(req, resp, name, id):
    if len(name) < 6:
        raise falcon.HTTPInvalidParam('invalid param',
                                      name + ':' + 'name less than 6')
    if int(id) < 10:
        raise falcon.HTTPInvalidParam('invalid param',
                                      name + ':' + 'id less than 10')
Ejemplo n.º 3
0
    def on_get(self, req, resp):
        """Handles GET requests"""

        if 'min' not in req.params:
            raise falcon.HTTPMissingParam('min')

        if 'max' not in req.params:
            raise falcon.HTTPMissingParam('max')

        min_n = req.params['min']
        max_n = req.params['max']

        if min_n.isnumeric() == False:
            raise falcon.HTTPInvalidParam('min must be number', min_n)

        if max_n.isnumeric() == False:
            raise falcon.HTTPInvalidParam('max must be number', max_n)

        min_n = int(min_n)
        max_n = int(max_n)

        if min_n > max_n:
            raise falcon.HTTPInvalidParam('min is bigger than max',
                                          (min_n, max_n))

        number = random.randint(min_n, max_n)
        result = {'lowerLimit': min_n, 'higherLimit': max_n, 'number': number}
        resp.media = result
Ejemplo n.º 4
0
    def parse_request(request, attr):
        """ returns mongo compatible query object, based on the query params provided """

        if 'lastN' in request.params.keys():
            try:
                limit_val = int(request.params['lastN'])
            except ValueError as e:
                raise falcon.HTTPInvalidParam('Must be integer.', 'lastN')
        elif 'hLimit' in request.params.keys():
            try:
                limit_val = int(request.params['hLimit'])
            except ValueError as e:
                raise falcon.HTTPInvalidParam('Must be integer.', 'hLimit')
        else:
            raise falcon.HTTPMissingParam('lastN')

        query = {'attr': attr, 'value': {'$ne': ' '}}
        ts_filter = {}
        if 'dateFrom' in request.params.keys():
            ts_filter['$gte'] = dateutil.parser.parse(
                request.params['dateFrom'])
        if 'dateTo' in request.params.keys():
            ts_filter['$lte'] = dateutil.parser.parse(request.params['dateTo'])
        if len(ts_filter.keys()) > 0:
            query['ts'] = ts_filter

        ls_filter = {"_id": False, '@timestamp': False, '@version': False}
        sort = [('ts', pymongo.DESCENDING)]

        return {
            'query': query,
            'limit': limit_val,
            'filter': ls_filter,
            'sort': sort
        }
    def __init__(self, server_state):
        """
            Initialization method

            Args:
                server_state (bool): True if server is under maintance.

            Raises:
                falcon.HTTPInvalidParam
            
            Returns:
                None
        """
        if server_state is None:
            raise falcon.HTTPInvalidParam(
                title="Invalid Param",
                description="Server State should be passed.")
        if type(server_state) is not bool:
            raise falcon.HTTPInvalidParam(
                title="Invalid Param",
                description=
                "Invalid datatype. Server Maintaince manager accepts bool.",
            )
        else:
            self.server_state = server_state
Ejemplo n.º 6
0
 def _validate_coef(coef, param):
     try:
         if coef is not None:
             coef = float(coef)
             if coef < 0:
                 raise falcon.HTTPInvalidParam(
                     'Invalid %s: cannot be smaller then 0' % param, param)
     except ValueError:
         raise falcon.HTTPInvalidParam('%s not numeric' % param, param)
     return coef
Ejemplo n.º 7
0
    def parse_request(request, attr):
        """ returns mongo compatible query object, based on the query params provided """
        logger.debug('DeviceHistory.parse_request [start]')

        limit_val = False
        sort = [('ts', pymongo.DESCENDING)]

        if 'firstN' in request.params.keys():
            try:
                limit_val = int(request.params['firstN'])
                sort = [('ts', pymongo.ASCENDING)]
            except ValueError as e:
                logger.error(e)
                raise falcon.HTTPInvalidParam('Must be integer.', 'firstN')
        elif 'lastN' in request.params.keys():
            try:
                limit_val = int(request.params['lastN'])
            except ValueError as e:
                logger.error(e)
                raise falcon.HTTPInvalidParam('Must be integer.', 'lastN')
        elif 'hLimit' in request.params.keys():
            try:
                limit_val = int(request.params['hLimit'])
            except ValueError as e:
                logger.error(e)
                raise falcon.HTTPInvalidParam('Must be integer.', 'hLimit')

        if attr:
            query = {'attr': attr, 'value': {'$ne': ' '}}

        ts_filter = {}
        if 'dateFrom' in request.params.keys():
            ts_filter['$gte'] = dateutil.parser.parse(
                request.params['dateFrom'])
        if 'dateTo' in request.params.keys():
            ts_filter['$lte'] = dateutil.parser.parse(request.params['dateTo'])
        if len(ts_filter.keys()) > 0:
            query['ts'] = ts_filter

        ls_filter = {"_id": False, '@timestamp': False, '@version': False}
        req = {
            'query': query,
            'limit': limit_val,
            'filter': ls_filter,
            'sort': sort
        }

        logger.debug('DeviceHistory.parse_request [return]')
        logger.debug(req)

        return req
Ejemplo n.º 8
0
    def on_post(self, request, response):
        """Validates request and calls pig latin translation library

        Required Body Properties:
            text -- string of length > 0

        Success Response:
            Code -- 200
            Content -- { translation : string }

        Error Response:
            Code -- 400 or 500
            Content -- { title : string[, description : string]}
        """

        if request.content_length in (None, 0):
            raise falcon.HTTPBadRequest(
                'Request body is empty.',
                'The request body must contain JSON.'
            )
        doc = request.context['doc']

        if 'text' not in doc:
            raise falcon.HTTPMissingParam('text')
        elif not isinstance(doc['text'], basestring):
            raise falcon.HTTPInvalidParam(
                'The value is not a string.',
                'text'
            )
        elif len(doc['text']) == 0:
            raise falcon.HTTPInvalidParam(
                'The value is empty.',
                'text'
            )
        text = doc['text']

        try:
            translation = piglatin.to_pig_latin(text)
        except Exception,e:
            """API-specific input validation was handled before calling piglatin.to_pig_latin().
            So any errors are unexpected and given generic 500 code.
            Error message is retained, if possible"""
            description = None
            if (len(e.args) > 0):
                description = e.args[0]

            raise falcon.HTTPInternalServerError(
                'Unexpected error',
                description
            )
Ejemplo n.º 9
0
 def on_get(self, req, resp, orbit_number):
     '''GET /t5/orbit/<orbit_number>'''
     try:
         orbit = CalcOrbit(orbit_number)
     except TypeError as err:
         raise falcon.HTTPInvalidParam(
             msg='orbit_number: {}'.format(orbit_number),
             param_name=str(err))
     except ValueError as err:
         raise falcon.HTTPInvalidParam(
             msg='orbit_number: {}'.format(orbit_number),
             param_name=str(err))
     resp.body = orbit.json()
     resp.status = falcon.HTTP_200
Ejemplo n.º 10
0
    def on_post(self, req: falcon.Request, resp: falcon.Response):
        """
        post /predict

        Predicts the sentiment of an input text using a model trained on movie reviews.
        Return value is a score beteen 0 and 1.
        Above 0.5 is considered positive sentiment.

        Inputs:
            text (string)
        Outputs:
            score (float)

        Example input:
        {
            "text": "this movie really sucked"
        }

        Example output:
        {
            "score": 0.1
        }
        """

        text = req.media.get("text")

        if text is None:
            raise falcon.HTTPMissingParam("text")

        if not isinstance(text, str):
            raise falcon.HTTPInvalidParam(
                f"expected a string, got a {type(text).__name__}", "text")

        resp.media = {"score": predict(text)}
Ejemplo n.º 11
0
    def on_post(self, req: falcon.Request, resp: falcon.Response):
        """
        POST /api/translate/fren

        Translate french sentence to english (70 word limit).

        Input:  French text (UTF-8 string)
        Output: English text (UTF-8 string)

        Example input:
        {
            "fr": "Bonjour le monde!"
        }
        Example output:
        {
            "en": "Hello world!"
        }
        """

        fr_text = req.media.get("fr")

        if fr_text is None:
            raise falcon.HTTPMissingParam("fr")

        if not isinstance(fr_text, str):
            raise falcon.HTTPInvalidParam(
                f"expected a string, got a {type(fr_text).__name__}", "fr")

        resp.media = {"en": translate_fren(fr_text)}
Ejemplo n.º 12
0
 def on_get(self, req, resp):
     """
     ---
     description: Send an email to the given address
     parameters:
     - name: email
       in: query
       description: Email you want to send to
       required: true
       schema:
         type: string
     responses:
       200:
         description: Email was successfully sent
         content:
           application/json:
             schema:
               type: string
     """
     try:
         email = req.params["email"]
         send_email_to(email)
         resp.status = falcon.HTTP_200
         resp.media = {"email": "success"}
     except KeyError:
         raise falcon.HTTPInvalidParam("You are missing an email parameter",
                                       "Missing Parameter")
Ejemplo n.º 13
0
    def on_get(self, request, response, **kwargs):
        """
        Falcon resource method for handling the HTTP request GET method

        Falcon API provides: parameters embedded in URL via a keyword
        args dict, as well as convenience class variables falcon.HTTP_*
        """
        with warehouse.get_source_model_session() as current_model:
            #get Source dataset object, referenced in URL path
            sources = source.SourceUtil.get_list_of_data_sources(
                request.url, auth.get_user_id(request), current_model)
            dataset_id = source_parameters.get_requested_dataset_id(
                sources, request, response, kwargs)
            if warehouse.is_warehouse(dataset_id):
                msg = 'Metadata.xml not available for source ID: ' + dataset_id
                raise falcon.HTTPInvalidParam(msg, 'source')
            dataset = None
            for table in current_model['tables']:
                table_id = '{}.{}'.format(table['project'], table['name'])
                if table_id == dataset_id:
                    dataset = table
                    break
            iso_xml = geographic_metadata(dataset, current_model)
            #return generated XML
            response.content_type = 'text/xml'
            response.body = iso_xml
Ejemplo n.º 14
0
    def on_get(self, req, resp):


        # Validate data
        # TODO: Finer error logging and handling,
        #       differentiate between title or data errors
        #       and invalid or missing parameter.
        try:
            title = req.params['title']
            data  = req.params['data']
            data  = tuple(map(float, data))
        except ValueError:
            # TODO: Better error logging message
            # Error logs and metrics
            req_err.inc()
            logger.exception('Invalid query string')
            raise falcon.HTTPInvalidParam('Invalid query string',
                                          'title or data')
        
        # Logs and metrics
        logger.debug("Received new request")
        requests.inc()

        # Send image as response
        resp.data = plot_it(title, data)
        resp.content_type = falcon.MEDIA_PNG
        resp.status = falcon.HTTP_200
Ejemplo n.º 15
0
 def on_delete(self, req, resp, orgid):
     """
     Remove an Organization specified by the orgid
     
     :param req:
     :param resp:
     :param appid:
     :return:
     """
     mapper = self.meta.get('mapper')
     o = mapper.organization.Organization.get_by_uid(orgid, user_flag=True)
     if o is None:
         raise falcon.HTTPInvalidParam('Organization not found', orgid)
     if o.status != 'DISABLED':
         raise falcon.HTTPConflict(falcon.HTTP_409, 'Organization must be disabled before deletion is possible')
     
     if len(o.users) > 0 or len(o.admins) > 0:
         raise falcon.HTTPConflict(
             falcon.HTTP_409,
             'Users must be deleted or updated before application deletion is possible'
         )
     
     mapper.organization.Organization.delete_from_object(o)
     resp.body = {"deleted": True}
     return True
Ejemplo n.º 16
0
    def param_in_body(self, req, spec, param):
        body = req.stream.read().decode('utf-8')
        schema = spec.get('schema')
        name = param.get('name')
        type_ = schema.get('type')
        value = body

        if param.get('required') and body is None:
            raise falcon.HTTPMissingParam(name)
        default_func = lambda v: v if type_ is not None else None

        check_funcs = {
            'string': lambda v: str(v),
            'password': lambda v: str(v),
            'byte': lambda v: base64.b64decode(str(v)),
            'integer': lambda v: int(v),
            'long': lambda v: int(v),
            'float': lambda v: float(v),
            'double': lambda v: float(v),
            'boolean': lambda v: bool(v),
            'array': json_check,
            'object': json_check,
        }
        try:
            value = check_funcs.get(type_, default_func)(value)
            self.log.debug('param in body - name: {}, type: {}, value: {}'.format(name, type_, value))
        except ValueError as e:
            self.log.error_trace(
                'value error when check param in body - name: {}, type: {}, value: {}'.format(name, type_, value))
            raise falcon.HTTPInvalidParam('invalid param in body', name)
        return value
Ejemplo n.º 17
0
    def param_in_header(self, req, spec, param):
        headers = req.headers
        name = param.get('name').upper().replace('_', '-')
        type_ = param.get('type')

        value = headers.get(name)
        if param.get('required') and value is None:
            raise falcon.HTTPMissingParam(name)

        default_func = lambda v: v if type_ is not None else None

        check_funcs = {
            'string': lambda v: str(v),
            'password': lambda v: str(v),
            'byte': lambda v: base64.b64decode(str(v)),
            'integer': lambda v: int(v),
            'long': lambda v: int(v),
            'float': lambda v: float(v),
            'double': lambda v: float(v),
            'boolean': lambda v: bool(v),
            'array': json_check,
            'object': json_check,
        }

        try:
            value = check_funcs.get(type_, default_func)(value)
            self.log.debug('param in header - name: {}, type: {}, value: {}'.format(name, type_, value))
        except ValueError as e:
            self.log.error_trace(
                'value error when check param in header - name: {}, type: {}, value: {}'.format(name, type_, value))
            raise falcon.HTTPInvalidParam('invalid param in header', name + ':' + value)
        if param.get('required') and value is None:
            raise falcon.HTTPMissingParam(name)
        return value
Ejemplo n.º 18
0
    def on_get(self, req, resp):
        '''GET /ct/lbb2/cargogen/sale?<options>'''
        self.query_parameters = {
            'cargo': None,
            'market_uwp': None,
            'market_tc': [],
            'admin': 0,
            'bribery': 0,
            'broker': 0,
            'quantity': 0
        }
        LOGGER.debug('query_string = %s', req.query_string)
        self.parse_query_string(req.query_string)

        try:
            cargo = CargoSale(
                cargo=self.query_parameters['cargo'],
                quantity=self.query_parameters['quantity'],
                admin=self.query_parameters['admin'],
                bribery=self.query_parameters['bribery'],
                broker=self.query_parameters['broker'],
                trade_codes=self.determine_trade_codes())
        except ValueError as err:
            raise falcon.HTTPInvalidParam(
                msg='query_string: {}'.format(req.query_string),
                param_name=str(err))
        resp.body = cargo.json()
        resp.status = falcon.HTTP_200
Ejemplo n.º 19
0
    def on_post(self, req, resp, queue):
        """
        Handles /collector/$queue post requests.
        Queues new data into the given queue located at the ActiveMQ instance.
        """
        # If the queue name is not an alphanumeric return error
        if not str(queue).isalnum():
            raise falcon.HTTPInvalidParam('Invalid queue name.', 'queue')

        # Create the message
        data = req.media.get('data')

        # Check if data is not empty
        if not data:
            raise falcon.HTTPMissingParam('data')

        # Send the message
        self.activemq_conn.send(queue,
                                json.dumps({
                                    'data': data
                                }).encode(), self.headers)

        # Answer the request
        resp.media = {'created': json.dumps(data), 'status': 'success'}

        # Created a new entry on the queue
        resp.status = falcon.HTTP_201
Ejemplo n.º 20
0
 def enforce(self, j, **kargs):
     for k in kargs.keys():
         if k not in j:
             raise falcon.HTTPMissingParam(k)
         if type(j[k]) != kargs[k]:
             raise falcon.HTTPInvalidParam(
                 "expected {} to be of type {}".format(k, kargs[k]), k)
Ejemplo n.º 21
0
    def on_get(self, req, resp, *args, **kwargs):
        super(ResourceGetEvents, self).on_get(req, resp, *args, **kwargs)

        request_event_status = req.get_param("status", False)
        if request_event_status is not None:
            request_event_status = request_event_status.upper()
            if (len(request_event_status) !=
                    1) or (request_event_status not in [
                        i.value for i in EventStatusEnum.__members__.values()
                    ]):
                raise falcon.HTTPInvalidParam(messages.event_status_invalid,
                                              "status")

        response_events = list()

        aux_events = self.db_session.query(Event)

        if request_event_status is not None:
            aux_events = \
                aux_events.filter(
                    Event.status == EventStatusEnum(request_event_status))

        if aux_events is not None:
            for current_event in aux_events.all():
                response_events.append(current_event.json_model)

        resp.media = response_events
        resp.status = falcon.HTTP_200
Ejemplo n.º 22
0
 def on_delete(self, req, resp, groupid):
     """
     Remove a Group specified by the groupid
     
     :param req:
     :param resp:
     :param groupid:
     :return:
     """
     mapper = self.meta.get('mapper')
     o = mapper.group.Group.get_by_uid(groupid, user_flag=True)
     if o is None:
         raise falcon.HTTPInvalidParam('Group not found', groupid)
     if o.status != 'DISABLED':
         raise falcon.HTTPConflict(falcon.HTTP_409, 'Group must be disabled before deletion is possible')
     
     if len(o.users) > 0:
         raise falcon.HTTPConflict(
             falcon.HTTP_409,
             'Users must be deleted or updated before group deletion is possible'
         )
     
     mapper.group.Group.delete_from_object(o)
     resp.body = {"deleted": True}
     return True
Ejemplo n.º 23
0
    def on_get(self, req, resp, *args, **kwargs):
        super(ResourceGetRandomQuestion, self).on_get(req, resp, *args,
                                                      **kwargs)

        category_filter = req.get_param("category", False)
        if category_filter is not None:
            if category_filter not in [
                    i.value for i in CategoryEnum.__members__.values()
            ]:
                raise falcon.HTTPInvalidParam(messages.event_status_invalid,
                                              "category")

        query = self.db_session.query(Question).filter().order_by(func.rand())
        if category_filter is not None:
            query = query.filter(
                Question.category == CategoryEnum(category_filter))

        question = query.first()
        print(question.json_model)

        query = self.db_session.query(Answer, AnswerQuestionAssiation.is_correct) \
            .join(AnswerQuestionAssiation).filter(question.id == AnswerQuestionAssiation.id_question)

        answers = query.all()
        response = question.json_model
        response["answers"] = []

        for a in answers:
            aux = a[0].json_model
            aux["is_correct"] = a[1]
            response["answers"].append(aux)

        resp.media = response
        resp.status = falcon.HTTP_200
Ejemplo n.º 24
0
 def get_country(self, country):
     try:
         if country not in self.cache:
             self.cache[country] = getattr(countries, country)()
         return self.cache[country]
     except AttributeError:
         raise falcon.HTTPInvalidParam("", "country")
Ejemplo n.º 25
0
 def on_put(self, req, resp, discordId):
     self.authorize(req)
     try:
         self.db.updateUser(discordId,req.media)
     except Exception as error:
         print(error)
         raise falcon.HTTPInvalidParam("Unknown user", "discordId")
Ejemplo n.º 26
0
    def on_post(self, req, resp, body):
        if "changes" not in body:
            raise falcon.HTTPInvalidParam(
                "Compilation request requires a changes field", "changes")

        resp.body = self.validate(body["changes"])
        resp.status = falcon.HTTP_200
Ejemplo n.º 27
0
 def on_get(self, req, resp):
     try:
         key_id = req.params['id']
         if key_id == '':
             raise ImportError
     except ImportError:
         raise falcon.HTTPInvalidParam('Parameter cannot be blank', 'url')
     except:
         raise falcon.HTTPMissingParam(param_name='id (ID of video)')
     with open("yte-logs.json", "r+") as logs_file:
         logs = json.load(logs_file)
         logs_file.close
     if key_id in logs:
         resp.content_type = 'json'
         resp.status = falcon.HTTP_200
         resp.body = json.dumps(logs[key_id])
         if logs[key_id]['status'] != '200 OK':
             Process(target=self.main, args=(key_id, )).start()
     else:
         check_vid = YouTube('https://www.youtube.com/watch?v=' + key_id)
         if int(check_vid.length) > 600:
             raise falcon.HTTPFailedDependency(
                 description='Video is more than 10 mins long')
         log_create(key_id)
         Process(target=self.main, args=(key_id, )).start()
         resp.content_type = 'json'
         resp.status = falcon.HTTP_200
         body = {
             'response': 'Process started, GET request again to see status.'
         }
         resp.body = json.dumps(body)
Ejemplo n.º 28
0
    def param_in_path(self, matched_uri, param):
        type_ = param.get('type')
        name = param.get('name')

        value = matched_uri.groupdict().get(name)
        self.log.debug(value)
        default_func = lambda v: v if type_ is not None else None
        check_funcs = {
            'string': lambda v: str(v),
            'password': lambda v: str(v),
            'byte': lambda v: base64.b64decode(str(v)),
            'integer': lambda v: int(v),
            'long': lambda v: int(v),
            'float': lambda v: float(v),
            'double': lambda v: float(v),
            'boolean': lambda v: bool(v),
        }
        if param.get('required') and value is None:
            raise falcon.HTTPMissingParam(name)
        try:
            value = check_funcs.get(type_, default_func)(value)
            self.log.debug(
                'param in path - name: {}, type: {}, value: {}'.format(
                    name, type_, value))
        except ValueError as e:
            self.log.error_trace(
                'value error when check param in path - name: {}, type: {}, value: {}'
                .format(name, type_, value))
            raise falcon.HTTPInvalidParam('invalid param in path', name)
        return value
Ejemplo n.º 29
0
    def param_in_query(self, req, param):
        name = param.get('name')
        type_ = param.get('type')

        default_func = lambda p: req.get_param(p
                                               ) if type_ is not None else None

        check_funcs = {
            'string': lambda p: req.get_param(p),
            'password': lambda p: req.get_param(p),
            'byte': lambda p: base64.b64decode(req.get_param(p)),
            'integer': lambda p: req.get_param_as_int(p),
            'long': lambda p: req.get_param_as_int(p),
            'float': lambda p: float(req.get_param(p)),
            'double': lambda p: float(req.get_param(p)),
            'array': lambda p: req.get_param_as_dict(p),
            'object': lambda p: req.get_param_as_dict(p),
            'boolean': lambda p: req.get_param_as_bool(p),
        }
        value = None
        try:
            value = check_funcs.get(type_, default_func)(name)
            self.log.debug(
                'param in query - name: {}, type: {}, value: {}'.format(
                    name, type_, value))
        except ValueError as e:
            self.log.error_trace(
                'value error when check param in query - name: {}, type: {}, value: {}'
                .format(name, type_, value))
            raise falcon.HTTPInvalidParam('invalid param in query', name)
        if param.get('required') and value is None:
            raise falcon.HTTPMissingParam(name)
        return value
Ejemplo n.º 30
0
    def check_mandatory_query_param(params, field, ftype=None):
        if (field not in params.keys()) or (len(params[field]) == 0):
            raise falcon.HTTPMissingParam(field)

        if ftype is not None:
            if not ftype(params[field]):
                raise falcon.HTTPInvalidParam(
                    '%s must be of type %s' % (field, ftype.__name__), field)