Skip to content

Commit

Permalink
compatiblity with REST API 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Dec 22, 2023
1 parent 5487878 commit 874e082
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 58 deletions.
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"""
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()
)

return response_folder
Expand Down
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
189 changes: 142 additions & 47 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ python = "^3.10"
click = "^8.1.3"
backoff = "^2.2.1"
rich = "^13.4.2"
pydantic = {extras = ["email"], version = "^1.10.9"}
pydantic = {extras = ["email"], version = "^2.5"}
requests = "^2.31.0"
laconiq = "^0.2.0"
laconiq = "^0.3.0"
typer = {extras = ["all"], version = "^0.9.0"}

[tool.poetry.scripts]
Expand Down

0 comments on commit 874e082

Please sign in to comment.