Skip to content

Commit a83e81d

Browse files
committed
reorganize python modules
1 parent c311ede commit a83e81d

File tree

10 files changed

+169
-140
lines changed

10 files changed

+169
-140
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,24 @@ For full documentation and tutorials, trigger:
1313
python setup.py build_sphinx
1414
```
1515

16-
## Build from Source
16+
## Requirements
17+
- Python 3.8 or higher
18+
- virutalenv
19+
20+
## Prepare development setup
21+
```
22+
python3 -m venv env
23+
source env/bin/activate
24+
pip install -r requirement.txt
25+
pip install -e .
26+
```
27+
28+
## Run tests
29+
```
30+
python setup.py test
31+
```
32+
33+
## Build from source
1734
```
1835
python setup.py test
1936
python setup.py build_sphinx

client/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
from .httpclient import *
2-
from .resourceclient import *
1+
from .http_client import *
2+
from .resource_client import *
3+
from .crud_client import *
4+
from .request_logger import *

client/airlock_session.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from .http_client import HttpClient
2+
from requests import Response
3+
4+
class AirlockSession(HttpClient):
5+
def __init__(self, client: type(HttpClient)):
6+
self.client = client
7+
8+
def get(self, path: str, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
9+
return self.client.get(path, headers)
10+
11+
def post(self, path: str, data=None, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
12+
return self.client.post(path, data, headers)
13+
14+
def put(self, path: str, data=None, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
15+
return self.client.put(path, data, headers)
16+
17+
def patch(self, path: str, data=None, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
18+
return self.client.patch(path, data, headers)
19+
20+
def delete(self, path, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
21+
return self.client.delete(path, headers)
22+
23+
def terminate(self):
24+
return self.post(path="/airlock/rest/session/terminate")

client/crud_client.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
class ResourceRUClient:
2+
def __init__(self, session: type(AirlockSession), base_path: str):
3+
self.client = session
4+
self.base_path = base_path
5+
6+
def get(self):
7+
return self.client.get(self.base_path)
8+
9+
def update(self, data):
10+
return self.client.patch(
11+
path=self.base_path,
12+
data=data
13+
)
14+
15+
16+
class ResourceCRUDClient:
17+
def __init__(self, session: type(AirlockSession), base_path: str):
18+
self.client = session
19+
self.base_path = base_path
20+
21+
def all(self):
22+
return self.client.get(self.base_path)
23+
24+
def get(self, resource_id: str):
25+
return self.client.get(f"{self.base_path}/{resource_id}")
26+
27+
def create(self, data):
28+
return self.client.post(
29+
path=self.base_path,
30+
data=data
31+
)
32+
33+
def update(self, resource_id: str, data):
34+
return self.client.patch(
35+
path=f"{self.base_path}/{resource_id}",
36+
data=data
37+
)
38+
39+
def delete(self, resource_id: str):
40+
return self.client.delete(f"{self.base_path}/{resource_id}")
41+
42+
def connect_to_resources(self, resource_id: str, relationship_id: str, relationship_type: str):
43+
return self.connect_to_resources(resource_id, relationship_id, relationship_type + "s", relationship_type)
44+
45+
def connect_to_resources(self, resource_id: str, relationship_id: str, path: str, relationship_type: str):
46+
return self.client.patch(
47+
path=f"{self.base_path}/{resource_id}/relationships/{path}",
48+
data=(
49+
"{"
50+
"\"data\" : [{"
51+
f"\"type\" : \"{relationship_type}\","
52+
f"\"id\" : \"{relationship_id}\""
53+
"}]"
54+
"}"
55+
)
56+
)
57+
58+
def connect_to_resource(self, resource_id: str, relationship_id: str, relationship_type: str):
59+
return self.client.patch(
60+
path=f"{self.base_path}/{resource_id}/relationships/{relationship_type}",
61+
data=(
62+
"{"
63+
"\"data\" : {"
64+
f"\"type\" : \"{relationship_type}\","
65+
f"\"id\" : \"{relationship_id}\""
66+
"}"
67+
"}"
68+
)
69+
)
70+
71+
def disconnect_to_resources(self, resource_id: str, relationship_id: str, relationship_type: str):
72+
return self.disconnect_to_resource(resource_id, relationship_id, relationship_type + "s", relationship_type)
73+
74+
def disconnect_to_resources(self, resource_id: str, relationship_id: str, path: str, relationship_type: str):
75+
return self.client.delete(
76+
path=f"{self.base_path}/{resource_id}/relationships/{path}",
77+
data=(
78+
"{"
79+
"\"data\" : [{"
80+
f"\"type\" : \"{relationship_type}\","
81+
f"\"id\" : \"{relationship_id}\""
82+
"}]"
83+
"}"
84+
)
85+
)
86+
87+
def disconnect_to_resource(self, resource_id: str, relationship_id: str, relationship_type: str):
88+
return self.client.delete(
89+
path=f"{self.base_path}/{resource_id}/relationships/{relationship_type}",
90+
data=(
91+
"{"
92+
"\"data\" : {"
93+
f"\"type\" : \"{relationship_type}\","
94+
f"\"id\" : \"{relationship_id}\""
95+
"}"
96+
"}"
97+
)
98+
)

client/httpclient.py renamed to client/http_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import requests
2-
from .requestlogger import RequestLogger
2+
from .request_logger import RequestLogger
33
from typing import Final
44
from abc import ABCMeta
55

File renamed without changes.

client/resourceclient.py renamed to client/resource_client.py

Lines changed: 3 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
1-
from .httpclient import HttpClient
2-
from requests import Response
3-
4-
5-
class AirlockSession(HttpClient):
6-
def __init__(self, client: type(HttpClient)):
7-
self.client = client
8-
9-
def get(self, path: str, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
10-
return self.client.get(path, headers)
11-
12-
def post(self, path: str, data=None, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
13-
return self.client.post(path, data, headers)
14-
15-
def put(self, path: str, data=None, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
16-
return self.client.put(path, data, headers)
17-
18-
def patch(self, path: str, data=None, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
19-
return self.client.patch(path, data, headers)
20-
21-
def delete(self, path, headers: dict = HttpClient.DEFAULT_HEADERS) -> type(Response):
22-
return self.client.delete(path, headers)
23-
24-
def terminate(self):
25-
return self.post(path="/airlock/rest/session/terminate")
26-
1+
from .http_client import HttpClient
2+
from .airlock_session import AirlockSession
3+
from .crud_client import ResourceCRUDClient, ResourceRUClient
274

285
class AuthenticationClient:
296
def __init__(self, token: str, http_client: type(HttpClient)):
@@ -42,107 +19,6 @@ def create(self):
4219
return AirlockSession(client=self.client)
4320
raise Exception("Authentication failed.")
4421

45-
46-
class ResourceRUClient:
47-
def __init__(self, session: type(AirlockSession), base_path: str):
48-
self.client = session
49-
self.base_path = base_path
50-
51-
def get(self):
52-
return self.client.get(self.base_path)
53-
54-
def update(self, data):
55-
return self.client.patch(
56-
path=self.base_path,
57-
data=data
58-
)
59-
60-
61-
class ResourceCRUDClient:
62-
def __init__(self, session: type(AirlockSession), base_path: str):
63-
self.client = session
64-
self.base_path = base_path
65-
66-
def all(self):
67-
return self.client.get(self.base_path)
68-
69-
def get(self, resource_id: str):
70-
return self.client.get(f"{self.base_path}/{resource_id}")
71-
72-
def create(self, data):
73-
return self.client.post(
74-
path=self.base_path,
75-
data=data
76-
)
77-
78-
def update(self, resource_id: str, data):
79-
return self.client.patch(
80-
path=f"{self.base_path}/{resource_id}",
81-
data=data
82-
)
83-
84-
def delete(self, resource_id: str):
85-
return self.client.delete(f"{self.base_path}/{resource_id}")
86-
87-
def connect_to_resources(self, resource_id: str, relationship_id: str, relationship_type: str):
88-
return self.connect_to_resources(resource_id, relationship_id, relationship_type + "s", relationship_type)
89-
90-
def connect_to_resources(self, resource_id: str, relationship_id: str, path: str, relationship_type: str):
91-
return self.client.patch(
92-
path=f"{self.base_path}/{resource_id}/relationships/{path}",
93-
data=(
94-
"{"
95-
"\"data\" : [{"
96-
f"\"type\" : \"{relationship_type}\","
97-
f"\"id\" : \"{relationship_id}\""
98-
"}]"
99-
"}"
100-
)
101-
)
102-
103-
def connect_to_resource(self, resource_id: str, relationship_id: str, relationship_type: str):
104-
return self.client.patch(
105-
path=f"{self.base_path}/{resource_id}/relationships/{relationship_type}",
106-
data=(
107-
"{"
108-
"\"data\" : {"
109-
f"\"type\" : \"{relationship_type}\","
110-
f"\"id\" : \"{relationship_id}\""
111-
"}"
112-
"}"
113-
)
114-
)
115-
116-
def disconnect_to_resources(self, resource_id: str, relationship_id: str, relationship_type: str):
117-
return self.disconnect_to_resource(resource_id, relationship_id, relationship_type + "s", relationship_type)
118-
119-
def disconnect_to_resources(self, resource_id: str, relationship_id: str, path: str, relationship_type: str):
120-
return self.client.delete(
121-
path=f"{self.base_path}/{resource_id}/relationships/{path}",
122-
data=(
123-
"{"
124-
"\"data\" : [{"
125-
f"\"type\" : \"{relationship_type}\","
126-
f"\"id\" : \"{relationship_id}\""
127-
"}]"
128-
"}"
129-
)
130-
)
131-
132-
def disconnect_to_resource(self, resource_id: str, relationship_id: str, relationship_type: str):
133-
return self.client.delete(
134-
path=f"{self.base_path}/{resource_id}/relationships/{relationship_type}",
135-
data=(
136-
"{"
137-
"\"data\" : {"
138-
f"\"type\" : \"{relationship_type}\","
139-
f"\"id\" : \"{relationship_id}\""
140-
"}"
141-
"}"
142-
)
143-
)
144-
145-
14622
class ConfigurationClient:
14723
def __init__(self, session: type(AirlockSession)):
14824
self.client = session

sample/sample.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from client.resourceclient import VirtualHostClient, MappingClient, BackendGroupClient, ConfigurationClient, \
1+
from client.resource_client import VirtualHostClient, MappingClient, BackendGroupClient, ConfigurationClient, \
22
AuthenticationClient
3-
from client.httpclient import DefaultHttpClient
3+
from client.http_client import DefaultHttpClient
44

55
token = "...."
66
host_url = "http://localhost:8080"

tests/test_resourceclient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from client.httpclient import HttpClient
2-
from client.resourceclient import AirlockSession, AuthenticationClient
1+
from client.http_client import HttpClient
2+
from client.resource_client import AirlockSession, AuthenticationClient
33
from requests import Response
44
from pytest import raises
55

workspace/host.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from client.httpclient import DefaultHttpClient
2-
from client.resourceclient import AuthenticationClient
3-
from client.resourceclient import AirlockSession
4-
from client.resourceclient import ConfigurationClient
1+
from client.http_client import DefaultHttpClient
2+
from client.resource_client import AuthenticationClient
3+
from client.resource_client import AirlockSession
4+
from client.resource_client import ConfigurationClient
55

66

77
class Host:
88
def __init__(self, host_name: str, token: str):
9-
self.auth_client = AuthenticationClient(http_client=DefaultHttpClient(host_name), token=token)
9+
http_client = DefaultHttpClient(host_name)
10+
self.auth_client = AuthenticationClient(http_client=http_client, token=token)
1011
self.session = None
1112

1213
def configurations(self):
@@ -39,6 +40,17 @@ def activate(self, comment: str = ""):
3940
client.activate(comment)
4041

4142

43+
class Mappings:
44+
def all(self):
45+
raise Exception
46+
47+
def find(self):
48+
raise Exception
49+
50+
def add(self):
51+
raise Exception
52+
53+
4254
class ResourceObject:
4355
def attributes(self) -> str:
4456
raise Exception()

0 commit comments

Comments
 (0)