예제 #1
0
    def cget_handler(self, query):
        response = None

        data = self._search_events(query)
        if data:
            if str(request.accept_mimetypes) == 'text/csv':
                # all are the same - in other words, i don't care
                csv = [EventHandler._generate_csv_header(data[0]['_source'])]
                for event in data:
                    csv.append(
                        self._generate_csv_body(event['_source'],
                                                UUID(event['_id']).int))
                response = make_response(request.headers, HTTPStatus.OK,
                                         EventHandler._join_list(csv, '\n'),
                                         True, 'text/csv')
            else:
                dict_list = list()
                for event in data:
                    dict_list.append(
                        self._generate_dict_body(event['_source'],
                                                 UUID(event['_id']).int))
                response = make_response(request.headers, HTTPStatus.OK,
                                         json.dumps({'events': dict_list}),
                                         True)
        elif data is not None:
            response = make_response(
                request.headers, HTTPStatus.NOT_FOUND,
                json.dumps(http_message(HTTPStatus.NOT_FOUND)))
        else:
            response = make_response(
                request.headers, HTTPStatus.INTERNAL_SERVER_ERROR,
                json.dumps(http_message(HTTPStatus.INTERNAL_SERVER_ERROR)))

        return response
예제 #2
0
    def get_handler(self, event_id: UUID):
        response = None

        data = self._get_event_by_id(event_id)
        if data:
            if str(request.accept_mimetypes) == 'text/csv':
                response = make_response(
                    request.headers, HTTPStatus.OK,
                    EventHandler._join_list([
                        EventHandler._generate_csv_header(data),
                        self._generate_csv_body(data, event_id.int)
                    ], '\n'), True, 'text/csv')
            else:  # default to 'application/json'
                response = make_response(
                    request.headers, HTTPStatus.OK,
                    json.dumps(self._generate_dict_body(data, event_id.int)),
                    True)
        elif data is not None:
            response = make_response(
                request.headers, HTTPStatus.NOT_FOUND,
                json.dumps(http_message(HTTPStatus.NOT_FOUND)))
        else:
            response = make_response(
                request.headers, HTTPStatus.INTERNAL_SERVER_ERROR,
                json.dumps(http_message(HTTPStatus.INTERNAL_SERVER_ERROR)))

        return response
예제 #3
0
 def validate_instance(*args, **kwargs):
     instance = None
     if request.is_json:  # or request.mimetype == '*/*': # application/json or */* are accepted
         try:
             instance = request.json
             InstanceValidator(schema).validate(instance)
         except:
             abort(make_response(request.headers, HTTPStatus.UNPROCESSABLE_ENTITY, json.dumps(
                 http_message(HTTPStatus.UNPROCESSABLE_ENTITY))))
     else:
         abort(make_response(request.headers, HTTPStatus.UNPROCESSABLE_ENTITY, json.dumps(
             http_message(HTTPStatus.UNPROCESSABLE_ENTITY))))
     return f(instance, *args, **kwargs)
예제 #4
0
 def validate_uuid(event_id, *args, **kwargs):
     try:
         event_id = UUID(int=int(event_id))
     except:
         abort(make_response(request.headers, HTTPStatus.UNPROCESSABLE_ENTITY, json.dumps(
             http_message(HTTPStatus.UNPROCESSABLE_ENTITY))))
     return f(event_id, *args, **kwargs)
예제 #5
0
    def post_handler(self, data: dict, schema_order: list):
        response = make_response(
            request.headers, HTTPStatus.INTERNAL_SERVER_ERROR,
            json.dumps(http_message(HTTPStatus.INTERNAL_SERVER_ERROR)))
        retries = 0

        while retries <= 3:
            try:
                body = EventHandler._order_data(data, schema_order)
                body['date'] = rfc3339_to_timestamp(body['date'])
                event_id = uuid1()
                self.es.create(index=self.index, id=event_id, body=body)
                body = self._generate_dict_body(body, event_id.int)
                response = make_response(request.headers, HTTPStatus.CREATED,
                                         json.dumps(body))
                response.headers.add("Location",
                                     body["_links"]["self"]["href"])
                break
            except ConflictError:  # it's pretty impossible an uuid collision, but who knows...
                retries += 1
                continue
            except Exception:  # here is if something really bad happens, so let's exit and thow a 500 error
                break

        return response
예제 #6
0
 def validate_query(*args, **kwargs):
     try:
         query = dict(request.args.items())
         ArgsValidator(schema).validate(query)
     except:
         abort(make_response(request.headers, HTTPStatus.UNPROCESSABLE_ENTITY, json.dumps(
             http_message(HTTPStatus.UNPROCESSABLE_ENTITY))))
     return f(query, *args, **kwargs)
예제 #7
0
    def cget_handler(self, query):
        response = None

        data = self._search_events(query)
        if data:
            data = [self._generate_dict_body(
                event['_source'], UUID(event['_id']).int) for event in data]
            if str(request.accept_mimetypes) == 'text/csv':
                response = make_response(
                    request.headers, HTTPStatus.OK, EventHandler._generate_csv(data), True, 'text/csv')
            else:
                response = make_response(
                    request.headers, HTTPStatus.OK, json.dumps({'events': data}), True)
        elif data is not None:
            response = make_response(request.headers, HTTPStatus.NOT_FOUND, json.dumps(
                http_message(HTTPStatus.NOT_FOUND)))
        else:
            response = make_response(request.headers, HTTPStatus.INTERNAL_SERVER_ERROR, json.dumps(
                http_message(HTTPStatus.INTERNAL_SERVER_ERROR)))

        return response
예제 #8
0
 def check_authorization(*args, **kwargs):
     if "X-API-Key" not in request.headers:
         abort(make_response(request.headers, HTTPStatus.UNAUTHORIZED,
                             json.dumps(http_message(HTTPStatus.UNAUTHORIZED))))
     return f(*args, **kwargs)