Python falcon.HTTP_202 Examples

The following are 15 code examples of falcon.HTTP_202(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module falcon , or try the search function .
Example #1
Source File: sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def on_post(self, req, resp, session_id):
        # POST /v1/sessions/{session_id}/action
        # executes an action on the specified session

        user_id = req.get_header('X-User-ID') or ''
        doc = self.json_body(req)

        try:
            action, params = next(iter(doc.items()))
        except Exception:
            raise freezer_api_exc.BadDataFormat("Bad action request format")

        session_doc = self.db.get_session(user_id=user_id,
                                          session_id=session_id)
        session = Session(session_doc)
        session.execute_action(action, params)

        if session.need_update:
            self.db.update_session(user_id=user_id,
                                   session_id=session_id,
                                   patch_doc=session.doc)
        resp.status = falcon.HTTP_202
        resp.body = {'result': session.action_result,
                     'session_tag': session.session_tag} 
Example #2
Source File: jobs.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def on_post(self, req, resp, job_id):
        # POST /v1/jobs/{job_id}/event
        # requests an event on the specified job

        user_id = req.get_header('X-User-ID') or ''
        doc = self.json_body(req)

        try:
            event, params = next(iter(doc.items()))
        except Exception:
            raise freezer_api_exc.BadDataFormat("Bad event request format")

        job_doc = self.db.get_job(user_id=user_id,
                                  job_id=job_id)
        job = Job(job_doc)
        result = job.execute_event(event, params)

        if job.need_update:
            self.db.replace_job(user_id=user_id,
                                job_id=job_id,
                                doc=job.doc)
        resp.status = falcon.HTTP_202
        resp.body = {'result': result} 
Example #3
Source File: jobs.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def on_post(self, req, resp, project_id, job_id):
        # POST /v2/{project_id}/jobs/{job_id}/event
        # requests an event on the specified job

        user_id = req.get_header('X-User-ID') or ''
        doc = self.json_body(req)

        try:
            event, params = next(iter(doc.items()))
        except Exception:
            raise freezer_api_exc.BadDataFormat("Bad event request format")

        job_doc = self.db.get_job(project_id=project_id,
                                  user_id=user_id,
                                  job_id=job_id)
        job = Job(job_doc)
        result = job.execute_event(event, params)

        if job.need_update:
            self.db.replace_job(project_id=project_id,
                                user_id=user_id,
                                job_id=job_id,
                                doc=job.doc)
        resp.status = falcon.HTTP_202
        resp.body = {'result': result} 
Example #4
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_action_ok(self):
        new_version = random.randint(0, 99)
        self.mock_db.get_session.return_value = common.get_fake_session_0()
        self.mock_db.update_session.return_value = new_version

        action = {"start": {
            "job_id": 'job_id_2',
            "current_tag": 5
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'success',
                           'session_tag': 6}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #5
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_succeeds_in_holdoff_if_tag_needs_not_increment(
            self, mock_time):
        mock_time.time.return_value = 1000
        new_version = random.randint(0, 99)
        session_doc = common.get_fake_session_0()
        session_doc['time_start'] = 999
        self.mock_db.get_session.return_value = session_doc
        self.mock_db.update_session.return_value = new_version
        action = {"start": {
            "job_id": 'job_id_2',
            "current_tag": 4
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'success',
                           'session_tag': 5}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #6
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_replies_holdoff_if_tag_would_increment(self,
                                                                  mock_time):
        mock_time.time.return_value = 1000
        new_version = random.randint(0, 99)
        session_doc = common.get_fake_session_0()
        session_doc['time_start'] = 999
        self.mock_db.get_session.return_value = session_doc
        self.mock_db.update_session.return_value = new_version
        action = {"start": {
            "job_id": 'job_id_2',
            "current_tag": 5
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'hold-off',
                           'session_tag': 5}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #7
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_outofholdoff_replies_outofsync_when_tag_too_low(
            self, mock_time):
        mock_time.time.return_value = 2000
        new_version = random.randint(0, 99)
        session_doc = common.get_fake_session_0()
        session_doc['time_start'] = 999
        self.mock_db.get_session.return_value = session_doc
        self.mock_db.update_session.return_value = new_version
        action = {"start": {
            "job_id": 'job_id_2',
            "current_tag": 2
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'out-of-sync',
                           'session_tag': 5}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #8
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_action_ok(self):
        new_version = random.randint(0, 99)
        self.mock_db.get_session.return_value = common.get_fake_session_0()
        self.mock_db.update_session.return_value = new_version
        action = {"start": {
            "job_id": 'job_id_2',
            "current_tag": 5
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'success',
                           'session_tag': 6}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['project_id'],
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #9
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_succeeds_in_holdoff_if_tag_needs_not_increment(
            self, mock_time):
        mock_time.time.return_value = 1000
        new_version = random.randint(0, 99)
        session_doc = common.get_fake_session_0()
        session_doc['time_start'] = 999
        self.mock_db.get_session.return_value = session_doc
        self.mock_db.update_session.return_value = new_version
        action = {"start": {
            "job_id": 'job_id_2',
            "current_tag": 4
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'success',
                           'session_tag': 5}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['project_id'],
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #10
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_replies_holdoff_if_tag_would_increment(self,
                                                                  mock_time):
        mock_time.time.return_value = 1000
        new_version = random.randint(0, 99)
        session_doc = common.get_fake_session_0()
        session_doc['time_start'] = 999
        self.mock_db.get_session.return_value = session_doc
        self.mock_db.update_session.return_value = new_version
        action = {"start": {
            "job_id": 'ahahahahah',
            "current_tag": 5
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'hold-off',
                           'session_tag': 5}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['project_id'],
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #11
Source File: test_sessions.py    From freezer-api with Apache License 2.0 6 votes vote down vote up
def test_on_post_start_outofholdoff_replies_outofsync_when_tag_too_low(
            self, mock_time):
        mock_time.time.return_value = 2000
        new_version = random.randint(0, 99)
        session_doc = common.get_fake_session_0()
        session_doc['time_start'] = 999
        self.mock_db.get_session.return_value = session_doc
        self.mock_db.update_session.return_value = new_version
        action = {"start": {
            "job_id": 'ahahahahah',
            "current_tag": 2
        }}
        self.mock_json_body.return_value = action
        expected_result = {'result': 'out-of-sync',
                           'session_tag': 5}
        self.resource.on_post(self.mock_req, self.mock_req,
                              common.fake_session_0['project_id'],
                              common.fake_session_0['session_id'])
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #12
Source File: sessions.py    From freezer-api with Apache License 2.0 5 votes vote down vote up
def on_post(self, req, resp, project_id, session_id):
        # POST /v2/{project_id}/sessions/{session_id}/action
        # executes an action on the specified session

        user_id = req.get_header('X-User-ID') or ''
        doc = self.json_body(req)

        try:
            action, params = next(iter(doc.items()))
        except Exception:
            raise freezer_api_exc.BadDataFormat("Bad action request format")

        session_doc = self.db.get_session(project_id=project_id,
                                          user_id=user_id,
                                          session_id=session_id)
        session = Session(session_doc)
        session.execute_action(action, params)

        if session.need_update:
            self.db.update_session(project_id=project_id,
                                   user_id=user_id,
                                   session_id=session_id,
                                   patch_doc=session.doc)
        resp.status = falcon.HTTP_202
        resp.body = {'result': session.action_result,
                     'session_tag': session.session_tag} 
Example #13
Source File: test_jobs.py    From freezer-api with Apache License 2.0 5 votes vote down vote up
def test_on_post_start_event_ok(self):
        new_version = random.randint(0, 99)
        self.mock_db.get_job.return_value = {
            'job_schedule': {
                'status': 'stop'
            }
        }
        self.mock_db.replace_job.return_value = new_version
        event = {"start": None}
        self.mock_json_body.return_value = event
        expected_result = {'result': 'success'}
        self.resource.on_post(self.mock_req, self.mock_req, 'my_job_id')
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #14
Source File: test_jobs.py    From freezer-api with Apache License 2.0 5 votes vote down vote up
def test_on_post_start_event_ok(self):
        new_version = random.randint(0, 99)
        self.mock_db.get_job.return_value = {
            'job_schedule': {
                'status': 'stop'
            }
        }
        self.mock_db.replace_job.return_value = new_version
        event = {"start": None}
        self.mock_json_body.return_value = event
        expected_result = {'result': 'success'}
        self.resource.on_post(self.mock_req, self.mock_req,
                              'my_project_id', 'my_job_id')
        self.assertEqual(falcon.HTTP_202, self.mock_req.status)
        self.assertEqual(expected_result, self.mock_req.body) 
Example #15
Source File: api.py    From cccatalog-api with MIT License 4 votes vote down vote up
def on_post(self, req, resp):
        """ Create a task. """
        raw_body = req.stream.read()
        request_error = self._validate_create_task(raw_body)
        if request_error:
            logging.warning(
                'Invalid request made. Reason: {}'.format(request_error)
            )
            resp.status = falcon.HTTP_400
            resp.media = {
                'message': request_error
            }
            return
        body = json.loads(raw_body.decode('utf-8'))
        model = body[MODEL]
        action = body[ACTION]
        callback_url = None
        if CALLBACK_URL in body:
            callback_url = body[CALLBACK_URL]
        since_date = body[SINCE_DATE] if SINCE_DATE in body else None
        task_id = str(uuid.uuid4())
        # Inject shared memory
        progress = Value('d', 0.0)
        finish_time = Value('d', 0.0)
        task = Task(
            model=model,
            task_type=TaskTypes[action],
            since_date=since_date,
            progress=progress,
            task_id=task_id,
            finish_time=finish_time,
            callback_url=callback_url
        )
        task.start()
        task_id = self.tracker \
            .add_task(task, task_id, action, progress, finish_time)
        base_url = self._get_base_url(req)
        status_url = base_url + '/task/{}'.format(task_id)
        # Give the task a moment to start so we can detect immediate failure.
        # TODO: Use IPC to detect if the job launched successfully instead
        # of giving it 100ms to crash. This is prone to race conditions.
        time.sleep(0.1)
        if task.is_alive():
            resp.status = falcon.HTTP_202
            resp.media = {
                'message': 'Successfully scheduled task',
                'task_id': task_id,
                'status_check': status_url
            }
            return
        else:
            resp.status = falcon.HTTP_500
            resp.media = {
                'message': 'Failed to schedule task due to an internal server '
                           'error. Check scheduler logs.'
            }
            return