Skip to content

Commit 6112a63

Browse files
committed
squash
1 parent cf25395 commit 6112a63

File tree

8 files changed

+68
-66
lines changed

8 files changed

+68
-66
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,7 @@ dmypy.json
123123

124124
# Pyre type checker
125125
.pyre/
126+
127+
128+
.vscode
129+
.idea

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This is currently a work in progress.
66

7-
The python client will support both sync and async programs.
7+
The python client supports both sync and async programs.
88

99
The api docs can be built with the following:
1010
```bash

pysirix/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .client import SirixClient
22
from .database import Database
33
from .resource import Resource
4+
from .constants import Insert
45

56

67
def Sirix(

pysirix/asynchronous/rest.py

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ async def async_update_resource(
131131
headers={
132132
"Authorization": f"Bearer {self._auth_data.access_token}",
133133
"Content-Type": data_type,
134+
"ETag": etag,
134135
},
135136
ssl=False if self._allow_self_signed else True,
136137
data=data,

pysirix/client.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from typing import Tuple, Dict, List, Union
2-
import asyncio
1+
from typing import Dict, List, Union
32

43
from requests import Session
54
from aiohttp import ClientSession
@@ -54,6 +53,7 @@ def __init__(
5453
else:
5554
self._session = Session()
5655
if allow_self_signed:
56+
self._allow_self_signed = allow_self_signed
5757
self._session.verify = False
5858
self._auth = Auth(self._auth_data, self._instance_data, self._session, self._asynchronous, allow_self_signed)
5959

@@ -73,15 +73,7 @@ async def _async_init(self):
7373
await self._auth.authenticate()
7474
await self.get_info()
7575

76-
def __getitem__(self, key: Union[Tuple[str], str]):
77-
"""
78-
Returns a database instance. See :meth:`_get_database` for further information
79-
"""
80-
if type(key) == tuple:
81-
return self._get_database(*key)
82-
return self._get_database(key)
83-
84-
def _get_database(self, database_name: str, database_type: str = None):
76+
def database(self, database_name: str, database_type: str = None):
8577
"""Returns a database instance
8678
8779
If a database with the given name and type does not exist,
@@ -95,10 +87,6 @@ def _get_database(self, database_name: str, database_type: str = None):
9587
9688
:param database_name: the name of the database to access
9789
:param database_type: the type of the database to access
98-
99-
You shouldn't use this method directly, rather, you should use index access, as follows:
100-
>>> sirix = Sirix(*params)
101-
>>> sirix[(database_name, database_type)]
10290
"""
10391
db = Database(database_name, database_type, parent=self)
10492
db._init()

pysirix/database.py

+14-25
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,41 @@ def _init(self):
3737
database_list = [
3838
db
3939
for db in self._instance_data.database_info
40-
if db["name"] == database_name
40+
if db["name"] == self.database_name
4141
]
4242
if len(database_list) != 0:
4343
self.database_type: str = database_list[0]["type"]
4444
elif self.database_type:
4545
self.database_type: str = self.database_type.lower()
46-
self._create()
46+
if self._asynchronous:
47+
return handle_async(
48+
async_create_database, self, self.database_name, self.database_type
49+
)
50+
else:
51+
return create_database(self, self.database_name, self.database_type)
4752
else:
4853
raise Exception(
4954
"No database type specified, and database does not already exist"
5055
)
5156

52-
def _create(self):
53-
"""Creates the database. Should be called if the database does not yet exist"""
54-
if self._asynchronous:
55-
return handle_async(
56-
async_create_database, self, self.database_name, self.database_type
57-
)
58-
else:
59-
return create_database(self, self.database_type, self.database_type)
60-
61-
def __getitem__(self, key: str):
62-
"""
63-
Returns a resource instance. See :meth:`_get_resource` for further information
64-
"""
65-
return self._get_resource(key)
66-
67-
def _get_resource(self, resource_name: str, data: Union[str, ET.Element, Dict] = None):
57+
def resource(self, resource_name: str, data: Union[str, ET.Element, Dict] = None):
6858
"""Returns a resource instance
6959
7060
If a resource with the given name and type does not exist,
7161
it is created.
7262
7363
If the resource does not yet exist, it is created
7464
75-
:param database_name: the name of the database to access
76-
:param database_type: the type of the database to access
7765
:param resource_name: the name of the resource to aceess
7866
:param data: data to initialize resource with if it does
7967
yet exist
80-
81-
You shouldn't use this method directly, rather, you should use index access, as follows:
82-
>>> sirix = Sirix(params)
83-
>>> sirix[(database_name, database_type)]
8468
"""
85-
return Resource(resource_name, parent=self)
69+
resource = Resource(resource_name, parent=self)
70+
if self._asynchronous:
71+
return handle_async(resource._async_init(), data)
72+
else:
73+
resource._init(data)
74+
return resource
8675

8776
def delete(self) -> bool:
8877
if self._asynchronous:

pysirix/resource.py

+37-23
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,49 @@ def __init__(self, resource_name: str, parent):
2929
self._auth_data: AuthData = parent._auth_data
3030
self._asynchronous = parent._asynchronous
3131

32-
self.database_name = database_name
33-
self.database_type = database_type
32+
self.database_name = parent.database_name
33+
self.database_type = parent.database_type
3434
self.resource_name = resource_name
3535

3636
self._allow_self_signed = parent._allow_self_signed
3737

38-
def _create(self, data: str):
39-
"""Creates the resource. Should be called if the resource does not yet exist
40-
38+
def _init(self, data: Union[str, Dict, ET.Element, None]):
39+
"""
4140
:param data: the data to initialize the resource with
42-
"""
43-
if self._asynchronous:
44-
return handle_async(
45-
async_create_resource,
46-
self,
47-
self.database_name,
48-
self.database_type,
49-
self.resource_name,
50-
data=data,
51-
)
52-
else:
53-
return create_resource(
54-
self, self.database_type, self.database_type, self.resource_name
55-
)
41+
"""
42+
database_info = next(
43+
(db
44+
for db in self._instance_data.database_info
45+
if db["name"] == self.database_name)
46+
)
47+
if self.resource_name not in database_info.get("resources"):
48+
if not isinstance(data, str):
49+
data = (
50+
json.dumps(data)
51+
if self.database_type == "json"
52+
else ET.tostring(data)
53+
)
54+
return create_resource(self, data)
55+
56+
def _async_init(self, data: Union[str, Dict, ET.Element]):
57+
database_info = [
58+
db
59+
for db in self._instance_data.database_info
60+
if db["name"] == self.database_name
61+
][0]
62+
if self.resource_name not in database_info.get("resources"):
63+
return handle_async(async_create_resource, self, data,)
5664

5765
def read(
66+
self,
5867
node_id: Union[int, None],
5968
revision: Union[Revision, Tuple[Revision, Revision], None] = None,
6069
max_level: Union[int, None] = None,
6170
):
6271
if self._asynchronous:
63-
return handle_async(async_read_resource, revision, node_id, max_level)
72+
return handle_async(async_read_resource, self, revision, node_id, max_level)
6473
else:
65-
return read_resource(revision, node_id, max_level)
74+
return read_resource(self, revision, node_id, max_level)
6675

6776
def update(
6877
self,
@@ -84,14 +93,19 @@ def update(
8493
)
8594
if (
8695
self.resource_name
87-
not in self._instance_data.database_info[self.database_name]
96+
not in next(
97+
db
98+
for db in self._instance_data.database_info
99+
if db["name"] == self.database_name
100+
)["resources"]
88101
):
89102
return self._create(data)
90103
else:
104+
print(insert.value)
91105
if self._asynchronous:
92106
return async_update_resource(self, nodeId, data, insert)
93107
else:
94-
return update_resource(self, nodeId, data, insert)
108+
return update_resource(self, nodeId, data, insert.value)
95109

96110
def delete(self, nodeId: Union[int, None]) -> bool:
97111
if self._asynchronous:

pysirix/sync/rest.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ def update_resource(self, node_id: int, data: str, insert: str) -> bool:
115115
response = self._session.head(
116116
f"{self._instance_data.sirix_uri}/{self.database_name}/{self.resource_name}",
117117
params=params,
118-
headers={"Authorization": f"Bearer {self._auth_data.access_token}"},
118+
headers={
119+
"Authorization": f"Bearer {self._auth_data.access_token}",
120+
"Accept": data_type,
121+
},
119122
)
120123
etag = response.headers.get("ETag")
121124
# prepare to update
@@ -127,10 +130,12 @@ def update_resource(self, node_id: int, data: str, insert: str) -> bool:
127130
headers={
128131
"Authorization": f"Bearer {self._auth_data.access_token}",
129132
"Content-Type": data_type,
133+
"ETag": etag,
130134
},
131135
data=data,
132136
)
133-
if response.status_code == 201:
137+
print(response)
138+
if response.status_code == 200:
134139
return True
135140
return False
136141

0 commit comments

Comments
 (0)