This commit is contained in:
43
backend/infrastructure/paperless_client.py
Normal file
43
backend/infrastructure/paperless_client.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# backend/infrastructure/paperless_client.py
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
||||
class PaperlessGateway:
|
||||
"""
|
||||
Gateway za komunikaciju s Paperless-ngx API-jem.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.base_url = settings.PAPERLESS_API_URL
|
||||
self.headers = {
|
||||
"Authorization": f"Token {settings.PAPERLESS_API_TOKEN}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
def upload_document(self, file_content, filename):
|
||||
"""Šalje dokument u Paperless."""
|
||||
url = f"{self.base_url}/documents/post_document/"
|
||||
files = {'document': (filename, file_content)}
|
||||
response = requests.post(url, headers={"Authorization": self.headers["Authorization"]}, files=files)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def get_document_status(self, task_id):
|
||||
"""Provjerava status procesiranja dokumenta."""
|
||||
url = f"{self.base_url}/tasks/{task_id}/"
|
||||
response = requests.get(url, headers=self.headers)
|
||||
return response.json()
|
||||
|
||||
def get_document_metadata(self, document_id: int):
|
||||
"""Dohvaća metapodatke dokumenta iz Paperless-a."""
|
||||
url = f"{self.base_url}/api/documents/{document_id}/"
|
||||
response = requests.get(url, headers=self.headers)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def fetch_unprocessed_documents(self):
|
||||
"""Dohvaća listu dokumenata koji čekaju obradu."""
|
||||
# Filtriramo dokumente koji nemaju naš 'processed' tag
|
||||
url = f"{self.base_url}/api/documents/?query=is_tagged:false"
|
||||
response = requests.get(url, headers=self.headers)
|
||||
return response.json().get('results', [])
|
||||
Reference in New Issue
Block a user