Skip to content

Commit

Permalink
compatiblity with REST API 3.0 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur authored Dec 23, 2023
1 parent 5487878 commit e84db08
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 133 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Publish pacakge to pypi

on:
push:
tags:
- '*'

jobs:
publish-to-pypi:
name: Publish to Pypi
uses: papermerge/papermerge-core/.github/workflows/publish-to-pypi.yml@master
secrets:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
66 changes: 0 additions & 66 deletions .github/workflows/version_bump.yml

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ can be found in [changelog.d folder](https://github.com/papermerge/papermerge-cl

<!-- towncrier release notes start -->

## 0.7.0 - 2023-12-22

- pydantic dependency upgraded from 1.x to v2.5
- `import` command fixed to work with REST API version 3.0 (Papermerge DMS 3.0)
- `server-version` command added (returns version of server REST API)

## 0.3.3 - 2022-12-24


Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ interpreter. In order to install `pip` on Ubuntu use following command:

$ sudo apt install python3-pip


## REST API/CLI Version Compatibility

REST API column - is version of Papermerge REST API server. This value
you can get from:

$ papermerge-cli server-version

CLI column - is version of papermege-cli command line utility. This value
you can get from:

$ papermerge-cli --version


| REST API | CLI |
|----------|------|
| =3.0 | =0.7 |
| =2.1 | = 0.3.3|

## Usage

Get you REST API authentication token from your instance:
Expand Down
22 changes: 20 additions & 2 deletions papermerge_cli/api_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from mimetypes import guess_type
from pathlib import Path
from typing import Generic, TypeVar

import requests

from papermerge_cli.exceptions import FileMimeTypeUnknown

T = TypeVar('T')


Expand Down Expand Up @@ -34,9 +37,13 @@ def post(
json,
response_model=None
):
headers = {
'Content-Type': 'application/json',
**self.headers
}
response = requests.post(
f"{self.host}{url}",
headers=self.headers,
headers=headers,
json=json
)

Expand Down Expand Up @@ -81,12 +88,23 @@ def upload(
file_path: Path,
response_model
) -> T:
mime_type, _ = guess_type(file_path)

if mime_type is None:
msg = f"{file_path} mime type cannot be guessed"
raise FileMimeTypeUnknown(msg)

data = open(file_path, 'rb').read()
# https://stackoverflow.com/a/35974071/381116
multipart_form_data = {
'file': (file_path.name, data, mime_type),
}
response = requests.post(
f"{self.host}{url}",
headers=self.headers,
files=dict(file=data)
files=multipart_form_data
)

if response.status_code != 200:
raise ValueError(response.text)

Expand Down
2 changes: 2 additions & 0 deletions papermerge_cli/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class FileMimeTypeUnknown(Exception):
"""Raised when mime of type to be uploaded cannot be guessed"""
13 changes: 6 additions & 7 deletions papermerge_cli/lib/nodes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import uuid
from typing import List

from papermerge_cli.rest import (get_me, get_nodes, node_add_tags,
node_assign_tags, node_remove_tags)
from papermerge_cli import rest
from papermerge_cli.schema import Node, Paginator, User
from papermerge_cli.types import NodeActionEnum

Expand All @@ -17,7 +16,7 @@ def list_nodes(
order_by: str = '-title'
) -> Paginator[Node]:

user: User = get_me(host=host, token=token)
user: User = rest.get_me(host=host, token=token)

if parent_id is None:
# in case no specific parent uuid is requested
Expand All @@ -37,7 +36,7 @@ def list_nodes(
'order_by': order_by
}

data: Paginator[Node] = get_nodes(
data: Paginator[Node] = rest.get_nodes(
node_id=node_id,
host=host,
token=token,
Expand All @@ -56,21 +55,21 @@ def perform_node_command(
tags: List[str]
):
if action in (NodeActionEnum.assign_tags, NodeActionEnum.replace_tags):
node_assign_tags(
rest.node_assign_tags(
host=host,
token=token,
node_id=node_id,
tags=tags
)
elif action in (NodeActionEnum.add_tags, NodeActionEnum.append_tags):
node_add_tags(
rest.node_add_tags(
host=host,
token=token,
node_id=node_id,
tags=tags
)
elif action in (NodeActionEnum.remove_tags, NodeActionEnum.delete_tags):
node_remove_tags(
rest.node_remove_tags(
host=host,
token=token,
node_id=node_id,
Expand Down
8 changes: 8 additions & 0 deletions papermerge_cli/lib/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from papermerge_cli import rest


def perform_server_version_command(
host: str,
token: str
):
return rest.get_server_version(host=host, token=token)
12 changes: 12 additions & 0 deletions papermerge_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from papermerge_cli.lib.importer import upload_file_or_folder
from papermerge_cli.lib.nodes import list_nodes, perform_node_command
from papermerge_cli.lib.users import me as perform_me
from papermerge_cli.lib.version import perform_server_version_command
from papermerge_cli.schema import Node, Paginator, User
from papermerge_cli.types import NodeActionEnum

Expand Down Expand Up @@ -217,6 +218,17 @@ def node_command(
)


@app.command(name="server-version")
def server_version_command(ctx: typer.Context):
"""Get REST API version used on server side"""
output = perform_server_version_command(
host=ctx.obj['HOST'],
token=ctx.obj['TOKEN'],
)

console.print(output)


"""
@click.command
@click.option(
Expand Down
4 changes: 3 additions & 1 deletion papermerge_cli/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .nodes import (create_folder, get_nodes, node_add_tags, node_assign_tags,
node_remove_tags)
from .users import get_me
from .version import get_server_version

__all__ = [
get_me,
Expand All @@ -10,5 +11,6 @@
node_remove_tags,
node_assign_tags,
upload_document,
create_folder
create_folder,
get_server_version
]
2 changes: 1 addition & 1 deletion papermerge_cli/rest/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def upload(
response_doc: Document = api_client.post(
'/api/nodes/',
response_model=Document,
json=doc_to_create.json()
json=doc_to_create.model_dump(mode='json')
)

result_doc: Document = api_client.upload(
Expand Down
2 changes: 1 addition & 1 deletion papermerge_cli/rest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_folder(
response_folder: Folder = api_client.post(
'/api/nodes/',
response_model=Folder,
json=folder_to_create.json()
json=folder_to_create.model_dump(mode='json')
)

return response_folder
Expand Down
16 changes: 16 additions & 0 deletions papermerge_cli/rest/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from papermerge_cli.api_client import ApiClient
from papermerge_cli.schema.version import Version
from papermerge_cli.utils import host_required, token_required


@host_required
@token_required
def get_server_version(
host: str,
token: str
) -> Version:
"""Returns current user instance"""
api_client = ApiClient[Version](token=token, host=host)
version = api_client.get('/api/version/', response_model=Version)

return version
10 changes: 5 additions & 5 deletions papermerge_cli/schema/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional
from uuid import UUID

from pydantic import BaseModel, ValidationError, validator
from pydantic import BaseModel, ValidationError, field_validator

from papermerge_cli.types import OCRStatusEnum

Expand All @@ -17,7 +17,7 @@ class UpdateNode(BaseModel):
title: Optional[str]
parent_id: Optional[UUID]

@validator('parent_id')
@field_validator('parent_id')
def parent_id_is_not_none(cls, value):
if value is None:
raise ValidationError('Cannot set parent_id to None')
Expand All @@ -43,13 +43,13 @@ class Node(BaseModel):
tags: List[Tag]
created_at: datetime
updated_at: datetime
parent_id: UUID | None
parent_id: UUID | None = None
user_id: UUID
document: DocumentNode | None = None

@validator('document', pre=True)
@field_validator('document', mode='before')
def document_validator(cls, value, values):
if values['ctype'] == NodeType.document:
if values.data['ctype'] == NodeType.document:
return DocumentNode(
ocr_status=value['ocr_status'],
ocr=value['ocr']
Expand Down
5 changes: 5 additions & 0 deletions papermerge_cli/schema/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pydantic import BaseModel


class Version(BaseModel):
version: str
Loading

0 comments on commit e84db08

Please sign in to comment.