Example #1
0
    async def api_vlob_create(self, client_ctx, msg):
        msg = vlob_create_serializer.req_load(msg)

        now = pendulum.now()
        if not timestamps_in_the_ballpark(msg["timestamp"], now):
            return {
                "status": "bad_timestamp",
                "reason": f"Timestamp is out of date."
            }

        try:
            await self.create(client_ctx.organization_id, client_ctx.device_id,
                              **msg)

        except VlobAlreadyExistsError as exc:
            return vlob_create_serializer.rep_dump({
                "status": "already_exists",
                "reason": str(exc)
            })

        except VlobAccessError:
            return vlob_create_serializer.rep_dump({"status": "not_allowed"})

        except VlobEncryptionRevisionError:
            return vlob_create_serializer.rep_dump(
                {"status": "bad_encryption_revision"})

        except VlobInMaintenanceError:
            return vlob_create_serializer.rep_dump(
                {"status": "in_maintenance"})

        return vlob_create_serializer.rep_dump({"status": "ok"})
Example #2
0
    async def api_vlob_create(self, client_ctx, msg):
        """
        This API call, when successful, performs the writing of a new vlob version to the database.
        Before adding new entries, extra care should be taken in order to guarantee the consistency in
        the ordering of the different timestamps stored in the database.

        See the `api_vlob_update` docstring for more information about the checks performed and the
        error returned in case those checks failed.
        """
        msg = vlob_create_serializer.req_load(msg)

        now = pendulum_now()
        if not timestamps_in_the_ballpark(msg["timestamp"], now):
            return vlob_create_serializer.timestamp_out_of_ballpark_rep_dump(
                backend_timestamp=now, client_timestamp=msg["timestamp"])

        try:
            await self.create(client_ctx.organization_id, client_ctx.device_id,
                              **msg)

        except VlobAlreadyExistsError as exc:
            return vlob_create_serializer.rep_dump({
                "status": "already_exists",
                "reason": str(exc)
            })

        except (VlobAccessError, VlobRealmNotFoundError):
            return vlob_create_serializer.rep_dump({"status": "not_allowed"})

        except VlobRequireGreaterTimestampError as exc:
            return vlob_create_serializer.require_greater_timestamp_rep_dump(
                exc.strictly_greater_than)

        except VlobEncryptionRevisionError:
            return vlob_create_serializer.rep_dump(
                {"status": "bad_encryption_revision"})

        except VlobInMaintenanceError:
            return vlob_create_serializer.rep_dump(
                {"status": "in_maintenance"})

        return vlob_create_serializer.rep_dump({"status": "ok"})