forked from m42e/mayan-automatic-metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmayan.py
More file actions
144 lines (124 loc) · 4.84 KB
/
mayan.py
File metadata and controls
144 lines (124 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import requests
import re
import json
from typing import Union
import logging
_logger = logging.getLogger(__name__)
class Endpoint(object):
def __init__(self, endpoint: str, *, params: dict = {}, base: str = None):
self._paramstring = None
if "://" in endpoint:
base, endpoint = endpoint.split("api/", 1)
base += "api/"
endpoint, self._paramstring = endpoint.split("?", 1)
if base is None:
raise Exception('missing base')
if not base.endswith("/"):
base = base + "/"
if endpoint.startswith("/"):
endpoint = endpoint[1:]
if not endpoint.endswith("/"):
endpoint = endpoint + "/"
if len(params) == 0:
params = {}
self.base = base
self.params = params
self.endpoint = endpoint
@property
def paramstring(self):
if self._paramstring != None:
return self._paramstring
paramstring = "&".join(map(lambda x: f"{x[0]}={x[1]}", self.params.items()))
return paramstring
def __repr__(self):
ep_api_call = f"{self.base}{self.endpoint}?{self.paramstring}"
_logger.debug("endpoint api call %s", ep_api_call)
return ep_api_call
__str__ = __repr__
class Mayan(object):
def __init__(self, baseurl, test=False):
self.test = test
self.baseurl = baseurl
pass
def ep(self, endpoint: str, *, params: dict = {}, base: str = None):
if base is None:
base = self.baseurl
return Endpoint(endpoint, params=params, base=base)
def login(self, username, password):
self.session = requests.Session()
self.session.auth = (username, password)
auth_data = {"username": username, "password": password}
token_response = self.session.post(
self.ep("auth/token/obtain", params={"format": "json"}), data=auth_data
)
_logger.debug("Login returned status: %s", token_response.status_code)
if token_response.status_code != 200:
raise Exception("Login Failed")
_logger.debug("Response: %s", token_response.content)
token = token_response.json()["token"]
self.session.headers = {
"Content-type": "application/json",
"Accept": "application/json",
"Authorization": f"Token {token}",
}
def load(self):
self.content_types = self.all("content_types")
self.document_types = {x["label"]: x for x in self.all("document_types")}
self.metadata_types = self.all("metadata_types")
self.tags = {x["label"]: x for x in self.all("tags")}
for dt, document_type in self.document_types.items():
self.document_types[dt]["metadatas"] = self.all(
self.ep("metadata_types", base=document_type["url"])
)
def all(self, endpoint: Union[str, Endpoint]):
if isinstance(endpoint, str):
endpoint = self.ep(endpoint)
results = []
page = {"next": endpoint}
while page["next"] != None:
if isinstance(page["next"], str):
page["next"] = self.ep(page["next"])
result = self.session.get(page["next"])
page = result.json()
results += page["results"]
return results
def first(self, endpoint: Union[str, Endpoint]):
page = self.get(endpoint)
return page["results"]
def get(self, endpoint: Union[str, Endpoint]):
if endpoint is str:
endpoint = self.ep(endpoint)
result = self.session.get(endpoint)
if result.status_code != 200:
_logger.warning(json.dumps(result.json(), indent=2))
return result.json()
def post(self, endpoint: Union[str, Endpoint], json_data):
if endpoint is str:
endpoint = self.ep(endpoint)
if self.test:
print("WOULD POST", str(endpoint), json.dumps(json_data, indent=2))
return {}
result = self.session.post(endpoint, json=json_data)
if result.status_code != 200:
_logger.warning(json.dumps(result.json(), indent=2))
return result.json()
def put(self, endpoint: Union[str, Endpoint], json_data):
if endpoint is str:
endpoint = self.ep(endpoint)
if self.test:
print("WOULD PUT", str(endpoint), json.dumps(json_data, indent=2))
return {}
result = self.session.put(endpoint, json=json_data)
if result.status_code != 200:
_logger.warning(json.dumps(result.json(), indent=2))
return result.json()
def jp(self, data):
if type(data) is requests.Response:
try:
data = data.json()
except:
print(data.content)
try:
print(json.dumps(data, indent=2))
except:
print(data)