Skip to content

Commit

Permalink
fix: refactor file structures (#66)
Browse files Browse the repository at this point in the history
* feat: add all missing APIs

* feat: add all missing APIs

* feat: add all missing APIs

* fix: refactor file structures
  • Loading branch information
palp1tate authored Sep 6, 2023
1 parent aea46c4 commit db43b96
Show file tree
Hide file tree
Showing 22 changed files with 1,418 additions and 1,272 deletions.
66 changes: 66 additions & 0 deletions src/casdoor/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import Dict, List

import requests

from src.casdoor import CasdoorSDK


class Adapter:
Expand All @@ -34,3 +40,63 @@ def __str__(self):

def to_dict(self) -> dict:
return self.__dict__


class AdapterSDK(CasdoorSDK):
def get_adapters(self) -> List[Dict]:
"""
Get the adapters from Casdoor.
:return: a list of dicts containing adapter info
"""
url = self.endpoint + "/api/get-adapters"
params = {
"owner": self.org_name,
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
adapters = r.json()
return adapters

def get_adapter(self, adapter_id: str) -> Dict:
"""
Get the adapter from Casdoor providing the adapter_id.
:param adapter_id: the id of the adapter
:return: a dict that contains adapter's info
"""
url = self.endpoint + "/api/get-adapter"
params = {
"id": f"{self.org_name}/{adapter_id}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
adapter = r.json()
return adapter

def modify_adapter(self, method: str, adapter: Adapter) -> Dict:
url = self.endpoint + f"/api/{method}"
adapter.owner = self.org_name
params = {
"id": f"{adapter.owner}/{adapter.name}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
adapter_info = json.dumps(adapter.to_dict())
r = requests.post(url, params=params, data=adapter_info)
response = r.json()
return response

def add_adapter(self, adapter: Adapter) -> Dict:
response = self.modify_adapter("add-adapter", adapter)
return response

def update_adapter(self, adapter: Adapter) -> Dict:
response = self.modify_adapter("update-adapter", adapter)
return response

def delete_adapter(self, adapter: Adapter) -> Dict:
response = self.modify_adapter("delete-adapter", adapter)
return response
66 changes: 66 additions & 0 deletions src/casdoor/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import Dict, List

import requests

from src.casdoor import CasdoorSDK

from .organization import Organization, ThemeData
from .provider import Provider
Expand Down Expand Up @@ -96,3 +102,63 @@ def __str__(self):

def to_dict(self) -> dict:
return self.__dict__


class ApplicationSDK(CasdoorSDK):
def get_applications(self) -> List[Dict]:
"""
Get the applications from Casdoor.
:return: a list of dicts containing application info
"""
url = self.endpoint + "/api/get-applications"
params = {
"owner": "admin",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
applications = r.json()
return applications

def get_application(self, application_id: str) -> Dict:
"""
Get the application from Casdoor providing the application_id.
:param application_id: the id of the application
:return: a dict that contains application's info
"""
url = self.endpoint + "/api/get-application"
params = {
"id": f"{self.org_name}/{application_id}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
application = r.json()
return application

def modify_application(self, method: str, application: Application) -> Dict:
url = self.endpoint + f"/api/{method}"
application.owner = self.org_name
params = {
"id": f"{application.owner}/{application.name}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
application_info = json.dumps(application.to_dict())
r = requests.post(url, params=params, data=application_info)
response = r.json()
return response

def add_application(self, application: Application) -> Dict:
response = self.modify_application("add-application", application)
return response

def update_application(self, application: Application) -> Dict:
response = self.modify_application("update-application", application)
return response

def delete_application(self, application: Application) -> Dict:
response = self.modify_application("delete-application", application)
return response
67 changes: 67 additions & 0 deletions src/casdoor/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
from typing import Dict, List

import requests

from src.casdoor import CasdoorSDK


class Cert:
def __init__(self):
Expand All @@ -34,3 +41,63 @@ def __str__(self):

def to_dict(self) -> dict:
return self.__dict__


class CertSDK(CasdoorSDK):
def get_certs(self) -> List[Dict]:
"""
Get the certs from Casdoor.
:return: a list of dicts containing cert info
"""
url = self.endpoint + "/api/get-certs"
params = {
"owner": self.org_name,
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
certs = r.json()
return certs

def get_cert(self, cert_id: str) -> Dict:
"""
Get the cert from Casdoor providing the cert_id.
:param cert_id: the id of the cert
:return: a dict that contains cert's info
"""
url = self.endpoint + "/api/get-cert"
params = {
"id": f"{self.org_name}/{cert_id}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
cert = r.json()
return cert

def modify_cert(self, method: str, cert: Cert) -> Dict:
url = self.endpoint + f"/api/{method}"
cert.owner = self.org_name
params = {
"id": f"{cert.owner}/{cert.name}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
cert_info = json.dumps(cert.to_dict())
r = requests.post(url, params=params, data=cert_info)
response = r.json()
return response

def add_cert(self, cert: Cert) -> Dict:
response = self.modify_cert("add-cert", cert)
return response

def update_cert(self, cert: Cert) -> Dict:
response = self.modify_cert("update-cert", cert)
return response

def delete_cert(self, cert: Cert) -> Dict:
response = self.modify_cert("delete-cert", cert)
return response
66 changes: 66 additions & 0 deletions src/casdoor/enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import Dict, List

import requests

from src.casdoor import CasdoorSDK


class Enforcer:
Expand All @@ -30,3 +36,63 @@ def __str__(self):

def to_dict(self) -> dict:
return self.__dict__


class EnforcerSDK(CasdoorSDK):
def get_enforcers(self) -> List[Dict]:
"""
Get the enforcers from Casdoor.
:return: a list of dicts containing enforcer info
"""
url = self.endpoint + "/api/get-enforcers"
params = {
"owner": self.org_name,
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
enforcers = r.json()
return enforcers

def get_enforcer(self, enforcer_id: str) -> Dict:
"""
Get the enforcer from Casdoor providing the enforcer_id.
:param enforcer_id: the id of the enforcer
:return: a dict that contains enforcer's info
"""
url = self.endpoint + "/api/get-enforcer"
params = {
"id": f"{self.org_name}/{enforcer_id}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
enforcer = r.json()
return enforcer

def modify_enforcer(self, method: str, enforcer: Enforcer) -> Dict:
url = self.endpoint + f"/api/{method}"
enforcer.owner = self.org_name
params = {
"id": f"{enforcer.owner}/{enforcer.name}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
enforcer_info = json.dumps(enforcer.to_dict())
r = requests.post(url, params=params, data=enforcer_info)
response = r.json()
return response

def add_enforcer(self, enforcer: Enforcer) -> Dict:
response = self.modify_enforcer("add-enforcer", enforcer)
return response

def update_enforcer(self, enforcer: Enforcer) -> Dict:
response = self.modify_enforcer("update-enforcer", enforcer)
return response

def delete_enforcer(self, enforcer: Enforcer) -> Dict:
response = self.modify_enforcer("delete-enforcer", enforcer)
return response
66 changes: 66 additions & 0 deletions src/casdoor/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from typing import Dict, List

import requests

from src.casdoor import CasdoorSDK

from .user import User

Expand Down Expand Up @@ -38,3 +44,63 @@ def __str__(self):

def to_dict(self) -> dict:
return self.__dict__


class GroupSDK(CasdoorSDK):
def get_groups(self) -> List[Dict]:
"""
Get the groups from Casdoor.
:return: a list of dicts containing group info
"""
url = self.endpoint + "/api/get-groups"
params = {
"owner": self.org_name,
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
groups = r.json()
return groups

def get_group(self, group_id: str) -> Dict:
"""
Get the group from Casdoor providing the group_id.
:param group_id: the id of the group
:return: a dict that contains group's info
"""
url = self.endpoint + "/api/get-group"
params = {
"id": f"{self.org_name}/{group_id}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
group = r.json()
return group

def modify_group(self, method: str, group: Group) -> Dict:
url = self.endpoint + f"/api/{method}"
group.owner = self.org_name
params = {
"id": f"{group.owner}/{group.name}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
group_info = json.dumps(group.to_dict())
r = requests.post(url, params=params, data=group_info)
response = r.json()
return response

def add_group(self, group: Group) -> Dict:
response = self.modify_group("add-group", group)
return response

def update_group(self, group: Group) -> Dict:
response = self.modify_group("update-group", group)
return response

def delete_group(self, group: Group) -> Dict:
response = self.modify_group("delete-group", group)
return response
Loading

0 comments on commit db43b96

Please sign in to comment.