forked from badstreff/git2jss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiojss.py
101 lines (84 loc) · 3.41 KB
/
aiojss.py
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
# pylint: disable=invalid-name,redefined-builtin
import asyncio
import aiohttp
from .etree import ElementTree
class NotFound(Exception):
pass
class JSS(object):
def __init__(self, url, username, password):
self.url = url
self.username = username
self.password = password
self.auth = aiohttp.BasicAuth(username, password)
self.session = aiohttp.ClientSession(loop=asyncio.get_event_loop())
def __del__(self):
self.session.close()
async def _get_endpoint(self, endpoint, id=None, name=None):
base_url = self.url + f'/JSSResource/{endpoint}'
if id:
url = base_url + f'/id/{id}'
async with self.session.get(url, auth=self.auth) as resp:
if resp.status != 200:
raise NotFound
return await resp.text()
elif name:
url = base_url + f'/name/{name}'
async with self.session.get(url, auth=self.auth) as resp:
if resp.status != 200:
raise NotFound
return await resp.text()
else:
async with self.session.get(base_url, auth=self.auth) as resp:
if resp.status != 200:
raise NotFound
return await resp.text()
async def _post_endpoint(self, endpoint, jss_object):
base_url = self.url + f'/JSSResource/{endpoint}/name'
base_url += f'/{jss_object.name.text}'
headers = {'content-type': 'application/xml'}
try:
await self._get_endpoint(endpoint, name=jss_object.name.text)
await self.session.put(base_url,
auth=self.auth,
data=jss_object.raw_xml(),
headers=headers)
except NotFound:
await self.session.post(base_url,
auth=self.auth,
data=jss_object.raw_xml(),
headers=headers)
async def scripts(self, id=None, name=None):
data = await self._get_endpoint('scripts', id, name)
return Script(data, self)
async def computer_extension_attributes(self, id=None, name=None):
data = await self._get_endpoint('computerextensionattributes',
id,
name)
return ExtensionAttribute(data, self)
class JSSObject(object):
def __init__(self, xml, delegate=None):
self._root = ElementTree.fromstring(xml)
self.delegate = delegate
def __getattr__(self, attr):
return self._root.__getattr__(attr)
def save(self):
raise NotImplementedError
def delete(self):
raise NotImplementedError
def raw_xml(self):
return ElementTree.tostring(self._root).decode("utf-8")
class Script(JSSObject):
def __init__(self, xml, delegate):
super().__init__(xml, delegate)
async def save(self):
await self.delegate._post_endpoint('scripts', self)
def delete(self):
raise NotImplementedError
class ExtensionAttribute(JSSObject):
def __init__(self, xml, delegate):
super().__init__(xml, delegate)
async def save(self):
await self.delegate._post_endpoint('computerextensionattributes',
self)
def delete(self):
raise NotImplementedError