Skip to content

Commit

Permalink
fix: Removed unused code and fixed incorrect type hints (#53)
Browse files Browse the repository at this point in the history
* chore: Removed unused code and fixed incorrect type hints

* chore: Reordered third party package imports

* style: Fixed flake8 lint errors
  • Loading branch information
brownfoxsir authored Aug 5, 2023
1 parent 2d69644 commit b5b3e01
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions src/casdoor/async_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import asyncio
import json
from typing import List, Optional
from typing import Dict, List, Optional

import aiohttp

Expand All @@ -28,14 +28,14 @@

class AsyncCasdoorSDK:
def __init__(
self,
endpoint: str,
client_id: str,
client_secret: str,
certificate: str,
org_name: str,
application_name: str,
front_endpoint: str = None
self,
endpoint: str,
client_id: str,
client_secret: str,
certificate: str,
org_name: str,
application_name: str,
front_endpoint: str = None
):
self.endpoint = endpoint
if front_endpoint:
Expand Down Expand Up @@ -84,7 +84,7 @@ async def get_oauth_token(
code: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None
) -> str:
) -> Dict:
"""
Request the Casdoor server to get OAuth token.
Must be set code or username and password for grant type.
Expand All @@ -94,19 +94,16 @@ async def get_oauth_token(
back to your server.
:param username: Casdoor username
:param password: username password
:return: token: OAuth token
:return: OAuth token
"""
response = await self.oauth_token_request(code, username, password)
token = response

return token
return await self.oauth_token_request(code, username, password)

def _get_payload_for_access_token_request(
self,
code: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None
) -> dict:
) -> Dict:
"""
Return payload for request body which was selecting by strategy.
"""
Expand All @@ -120,7 +117,7 @@ def _get_payload_for_access_token_request(
else:
return self.__get_payload_for_client_credentials()

def __get_payload_for_authorization_code(self, code: str) -> dict:
def __get_payload_for_authorization_code(self, code: str) -> Dict:
"""
Return payload for auth request with authorization code
"""
Expand All @@ -135,7 +132,7 @@ def __get_payload_for_password_credentials(
self,
username: str,
password: str
) -> dict:
) -> Dict:
"""
Return payload for auth request with resource owner password
credentials.
Expand All @@ -148,7 +145,7 @@ def __get_payload_for_password_credentials(
"password": password
}

def __get_payload_for_client_credentials(self) -> dict:
def __get_payload_for_client_credentials(self) -> Dict:
"""
Return payload for auth request with client credentials.
"""
Expand All @@ -163,7 +160,7 @@ async def oauth_token_request(
code: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None
) -> dict:
) -> Dict:
"""
Request the Casdoor server to get access_token.
Must be set code or username and password for grant type.
Expand All @@ -183,7 +180,7 @@ async def oauth_token_request(
)
return await self._oauth_token_request(payload=params)

async def _oauth_token_request(self, payload: dict) -> dict:
async def _oauth_token_request(self, payload: Dict) -> Dict:
"""
Request the Casdoor server to get access_token.
Expand All @@ -198,7 +195,7 @@ async def refresh_token_request(
self,
refresh_token: str,
scope: str = ""
) -> dict:
) -> Dict:
"""
Request the Casdoor server to get access_token.
Expand Down Expand Up @@ -234,7 +231,7 @@ async def refresh_oauth_token(

return access_token

def parse_jwt_token(self, token: str) -> dict:
def parse_jwt_token(self, token: str) -> Dict:
"""
Converts the returned access_token to real data using
jwt (JSON Web Token) algorithms.
Expand Down Expand Up @@ -311,8 +308,8 @@ async def enforce(
async def batch_enforce(
self,
permission_model_name: str,
permission_rules: list[list[str]]
) -> list[bool]:
permission_rules: List[List[str]]
) -> List[bool]:
"""
Send data to Casdoor enforce API
Expand All @@ -331,7 +328,7 @@ async def batch_enforce(
"clientSecret": self.client_secret
}

def map_rule(rule: list[str], idx) -> dict:
def map_rule(rule: List[str], idx) -> Dict:
if len(rule) < 3:
raise ValueError("Invalid permission rule[{0}]: {1}"
.format(idx, rule))
Expand All @@ -341,6 +338,7 @@ def map_rule(rule: list[str], idx) -> dict:
for i in range(0, len(rule)):
result.update({"v{0}".format(i): rule[i]})
return result

params = [map_rule(permission_rules[i], i)
for i in range(0, len(permission_rules))]
async with self._session.post(
Expand All @@ -361,7 +359,7 @@ def map_rule(rule: list[str], idx) -> dict:

return enforce_results

async def get_users(self) -> List[dict]:
async def get_users(self) -> List[Dict]:
"""
Get the users from Casdoor.
Expand All @@ -377,7 +375,7 @@ async def get_users(self) -> List[dict]:
users = await request.json()
return users

async def get_user(self, user_id: str) -> dict:
async def get_user(self, user_id: str) -> Dict:
"""
Get the user from Casdoor providing the user_id.
Expand Down Expand Up @@ -417,7 +415,7 @@ async def get_user_count(self, is_online: bool = None) -> int:
count = await request.json()
return count

async def modify_user(self, method: str, user: User) -> dict:
async def modify_user(self, method: str, user: User) -> Dict:
url = self.endpoint + f"/api/{method}"
user.owner = self.org_name
params = {
Expand All @@ -434,14 +432,14 @@ async def modify_user(self, method: str, user: User) -> dict:
response = await request.json()
return response

async def add_user(self, user: User) -> dict:
async def add_user(self, user: User) -> Dict:
response = await self.modify_user("add-user", user)
return response

async def update_user(self, user: User) -> dict:
async def update_user(self, user: User) -> Dict:
response = await self.modify_user("update-user", user)
return response

async def delete_user(self, user: User) -> dict:
async def delete_user(self, user: User) -> Dict:
response = await self.modify_user("delete-user", user)
return response

0 comments on commit b5b3e01

Please sign in to comment.