def change_command( ctx: click.Context, id_: int, name: Optional[str], schema_content: Optional[List[dict]], email_prefix: Optional[str], bounce_email: Optional[str], connector_id: Optional[int], hook_id: Optional[Tuple[int, ...]], locale: Optional[str], ) -> None: if not any([ name, schema_content, email_prefix, bounce_email, connector_id, locale, hook_id ]): return data: Dict[str, Any] = {} if name is not None: data["name"] = name if locale is not None: data["locale"] = locale with RossumClient(context=ctx.obj) as rossum: if email_prefix or bounce_email: queue_dict = rossum.get_queue(id_) if not queue_dict["inbox"]: inbox_dict = _create_inbox(rossum, queue_dict, email_prefix, bounce_email, name) else: inbox_dict = _patch_inbox(rossum, queue_dict, email_prefix, bounce_email) click.echo( f"{inbox_dict['id']}, {inbox_dict['email']}, {inbox_dict['bounce_email_to']}" ) if connector_id is not None: data["connector"] = get_json( rossum.get(f"connectors/{connector_id}"))["url"] if hook_id: hooks_urls = [] for hook in hook_id: hook_url = get_json(rossum.get(f"hooks/{hook}"))["url"] hooks_urls.append(hook_url) data["hooks"] = hooks_urls if schema_content is not None: name = name or rossum.get_queue(id_)["name"] schema_dict = rossum.create_schema(f"{name} schema", schema_content) data["schema"] = schema_dict["url"] if data: rossum.patch(f"queues/{id_}", data)
def get_data( ctx: click.Context, queue_id: int, files: Tuple[str], indent: int, ensure_ascii: bool, format_: str, output_file: Optional[IO[str]], ): annotations_to_export = list() with RossumClient(context=ctx.obj) as rossum: for file in files: json_response = rossum.upload_document(queue_id, file) annotation_id = get_id(json_response) annotations_to_export.append(annotation_id) rossum.poll_annotation(annotation_id, _is_done) export_data = rossum.export_data(queue_id, annotations_to_export, format_) if format_ == "json": output = json.dumps(get_json(export_data), indent=indent, ensure_ascii=ensure_ascii) else: output = get_text(export_data) click.echo(output.encode("utf-8"), file=output_file, nl=False)
def _create_schema(id_: str, schema_content: SchemaContent, rossum: RossumClient, name: Optional[str]) -> None: original_schema = get_json(rossum.get(f"schemas/{id_}")) new_schema = rossum.create_schema(name or original_schema["name"], schema_content) for queue_url in original_schema["queues"]: if queue_url.startswith(rossum.url): queue_url = queue_url[len(rossum.url) + 1:] rossum.patch(queue_url, data={"schema": new_schema["url"]})
def _patch_inbox(rossum: RossumClient, queue_dict: dict, email_prefix: Optional[str], bounce_email: Optional[str]) -> dict: inbox_data: Dict[str, Any] = {} if email_prefix: inbox_data["email_prefix"] = email_prefix if bounce_email: inbox_data["bounce_email_to"] = bounce_email inbox_data["bounce_unprocessable_attachments"] = True _, inbox_id = queue_dict["inbox"].rsplit("/", 1) return get_json(rossum.patch(f"inboxes/{inbox_id}", inbox_data))
def create_command( ctx: click.Context, name: str, schema_content: List[dict], email_prefix: Optional[str], bounce_email: Optional[str], workspace_id: Optional[int], connector_id: Optional[int], hook_id: Optional[Tuple[int, ...]], locale: Optional[str], ) -> None: if email_prefix is not None and bounce_email is None: raise click.ClickException( "Inbox cannot be created without specified bounce email.") with RossumClient(context=ctx.obj) as rossum: workspace_url = rossum.get_workspace(workspace_id)["url"] connector_url = (get_json( rossum.get(f"connectors/{connector_id}"))["url"] if connector_id is not None else None) hooks_urls = [] if hook_id: for hook in hook_id: hook_url = get_json(rossum.get(f"hooks/{hook}"))["url"] hooks_urls.append(hook_url) schema_dict = rossum.create_schema(f"{name} schema", schema_content) queue_dict = rossum.create_queue(name, workspace_url, schema_dict["url"], connector_url, hooks_urls, locale) inbox_dict = {"email": "no email-prefix specified"} if email_prefix is not None: inbox_dict = rossum.create_inbox(f"{name} inbox", email_prefix, bounce_email, queue_dict["url"]) click.echo(f"{queue_dict['id']}, {inbox_dict['email']}")
def download_command( ctx: click.Context, id_: str, indent: int, ensure_ascii: bool, format_: str, output_file: Optional[IO[str]], ): with APIClient(context=ctx.obj) as api_client: schema_dict = get_json(api_client.get(f"schemas/{id_}")) if format_ == "json": schema_file = json.dumps(schema_dict["content"], indent=indent, ensure_ascii=ensure_ascii, sort_keys=True).encode("utf-8") else: schema_file = SchemaToXlsx().convert(schema_dict["content"]) click.echo(schema_file, file=output_file, nl=False)