46 lines
1.7 KiB
Python
Executable File
46 lines
1.7 KiB
Python
Executable File
# TODO: The user can use anything in the standard library, installed for paperless
|
|
# or use the custom startup scripts to install additional libraries via pip
|
|
|
|
import requests
|
|
|
|
|
|
class PaperlessAPI:
|
|
def __init__(self, api_url, auth_token, timeout=5) -> None:
|
|
self._base_api_url = api_url
|
|
self._auth_token = auth_token
|
|
self._timeout = timeout
|
|
|
|
def _get_item_by_id(self, item_type, item_id):
|
|
if item_id:
|
|
response = requests.get(f"{self._base_api_url}/{item_type}/{item_id}/",
|
|
headers = {"Authorization": f"Token {self._auth_token}"})
|
|
if response.ok:
|
|
return response.json()
|
|
|
|
return {}
|
|
|
|
def get_document_by_id(self, document_id):
|
|
return self._get_item_by_id("documents", document_id)
|
|
|
|
def get_tag_id_by_name(self, tag_name):
|
|
response = requests.get(f"{self._base_api_url}/tags/",
|
|
headers = {"Authorization": f"Token {self._auth_token}"})
|
|
if response.ok:
|
|
print("tags ok")
|
|
response_json = response.json()
|
|
tag_list = []
|
|
tag_list.extend(response_json.get("results"))
|
|
fetched_tag = [tag for tag in tag_list if tag.get("name") == tag_name]
|
|
if len(fetched_tag) > 0:
|
|
print(f'fetched = {fetched_tag}')
|
|
return fetched_tag[0].get("id")
|
|
|
|
print("not found, returning none")
|
|
return None
|
|
|
|
def patch_document(self, document_id, data):
|
|
return requests.patch(f"{self._base_api_url}/documents/{document_id}/",
|
|
headers = {"Authorization": f"Token {self._auth_token}"},
|
|
data = data,
|
|
timeout = self._timeout)
|