usos-api 0.1.1__tar.gz → 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {usos_api-0.1.1 → usos_api-0.1.3}/PKG-INFO +1 -1
- {usos_api-0.1.1 → usos_api-0.1.3}/pyproject.toml +1 -1
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/auth.py +39 -13
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/client.py +24 -8
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/services/users.py +1 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/README.md +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/__init__.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/connection.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/exceptions.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/logger.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/__init__.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/consumer.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/course.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/grade.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/group.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/lang_dict.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/programme.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/term.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/models/user.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/services/__init__.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/services/api_server.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/services/groups.py +0 -0
- {usos_api-0.1.1 → usos_api-0.1.3}/usos_api/utils/__init__.py +0 -0
|
@@ -18,6 +18,7 @@ class AuthManager:
|
|
|
18
18
|
AUTHORIZE_SUFFIX = "services/oauth/authorize"
|
|
19
19
|
ACCESS_TOKEN_SUFFIX = "services/oauth/access_token"
|
|
20
20
|
REVOKE_TOKEN_SUFFIX = "services/oauth/revoke_token"
|
|
21
|
+
# List of available scopes can be found at https://apps.usos.edu.pl/developers/api/authorization/#scopes
|
|
21
22
|
SCOPES = "|".join(["offline_access", "studies"])
|
|
22
23
|
|
|
23
24
|
def __init__(self, api_base_address: str, consumer_key: str, consumer_secret: str):
|
|
@@ -94,33 +95,52 @@ class AuthManager:
|
|
|
94
95
|
self._oauth_client.resource_owner_secret = self._request_token_secret
|
|
95
96
|
_LOGGER.info(f"New request token generated: {self._request_token}")
|
|
96
97
|
|
|
97
|
-
async def get_authorization_url(
|
|
98
|
+
async def get_authorization_url(
|
|
99
|
+
self, callback_url: str, confirm_user: bool = False
|
|
100
|
+
) -> str:
|
|
98
101
|
"""
|
|
99
102
|
Get the authorization URL.
|
|
100
103
|
|
|
101
104
|
:param callback_url: The callback URL.
|
|
105
|
+
:param confirm_user: Whether to confirm the user.
|
|
106
|
+
:return: The authorization URL.
|
|
102
107
|
"""
|
|
103
108
|
await self._generate_request_token(callback_url)
|
|
109
|
+
if confirm_user:
|
|
110
|
+
return f"{self.base_address}{self.AUTHORIZE_SUFFIX}?oauth_token={self._request_token}&interactivity=confirm_user"
|
|
104
111
|
return f"{self.base_address}{self.AUTHORIZE_SUFFIX}?oauth_token={self._request_token}"
|
|
105
112
|
|
|
106
|
-
async def
|
|
113
|
+
async def authorize(self, verifier: str, request_token, request_token_secret):
|
|
107
114
|
"""
|
|
108
|
-
Authorize the client with
|
|
115
|
+
Authorize the client with verifier and optionally token and token secret.
|
|
109
116
|
|
|
110
|
-
:param
|
|
117
|
+
:param verifier: The verifier to authorize the client with.
|
|
118
|
+
:param token: The OAuth token obtained from the previous step.
|
|
119
|
+
:param token_secret: The OAuth token secret obtained from the previous step.
|
|
111
120
|
:return: The access token and secret.
|
|
112
121
|
"""
|
|
113
|
-
self._oauth_client.verifier =
|
|
122
|
+
self._oauth_client.verifier = verifier
|
|
123
|
+
if request_token:
|
|
124
|
+
self._oauth_client.resource_owner_key = request_token
|
|
125
|
+
if request_token_secret:
|
|
126
|
+
self._oauth_client.resource_owner_secret = request_token_secret
|
|
114
127
|
url = f"{self.base_address}{self.ACCESS_TOKEN_SUFFIX}"
|
|
115
128
|
url, headers, body = self._oauth_client.sign(url, http_method="POST")
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
129
|
+
try:
|
|
130
|
+
async with self._session.post(url, data=body, headers=headers) as response:
|
|
131
|
+
await self._handle_response_errors(response)
|
|
132
|
+
data = dict(urllib.parse.parse_qsl(await response.text()))
|
|
133
|
+
self.load_access_token(data["oauth_token"], data["oauth_token_secret"])
|
|
134
|
+
_LOGGER.info(
|
|
135
|
+
f"Authorization successful, received access token: {self.access_token}"
|
|
136
|
+
)
|
|
137
|
+
return self.access_token, self.access_token_secret
|
|
138
|
+
except AttributeError as e:
|
|
139
|
+
if e.args[0] == "'NoneType' object has no attribute 'post'":
|
|
140
|
+
raise USOSAPIException(
|
|
141
|
+
"Authorization failed. Did you forget to open the manager?"
|
|
142
|
+
)
|
|
143
|
+
raise
|
|
124
144
|
|
|
125
145
|
def load_access_token(self, access_token: str, access_token_secret: str):
|
|
126
146
|
"""
|
|
@@ -138,6 +158,12 @@ class AuthManager:
|
|
|
138
158
|
resource_owner_secret=self.access_token_secret,
|
|
139
159
|
)
|
|
140
160
|
|
|
161
|
+
def get_access_token(self):
|
|
162
|
+
return self.access_token, self.access_token_secret
|
|
163
|
+
|
|
164
|
+
def get_request_token(self):
|
|
165
|
+
return self._request_token, self._request_token_secret
|
|
166
|
+
|
|
141
167
|
def sign_request(
|
|
142
168
|
self, url: str, http_method: str = "GET", **kwargs
|
|
143
169
|
) -> tuple[str, dict, dict]:
|
|
@@ -94,23 +94,36 @@ class USOSClient:
|
|
|
94
94
|
[s for s in self.connection.auth_manager.SCOPES.split("|") if s != scope]
|
|
95
95
|
)
|
|
96
96
|
|
|
97
|
-
async def authorize(
|
|
97
|
+
async def authorize(
|
|
98
|
+
self, verifier: str, request_token: str = None, request_token_secret: str = None
|
|
99
|
+
) -> tuple[str, str]:
|
|
98
100
|
"""
|
|
99
|
-
Authorize the client with
|
|
101
|
+
Authorize the client with verifier and optionally token and token secret.
|
|
100
102
|
|
|
101
|
-
|
|
103
|
+
Parameters `token` and `token_secret` can be useful when you create a new client instance and want to authorize it with the token and secret obtained from another client instance.
|
|
104
|
+
|
|
105
|
+
:param verifier: The verifier to authorize the client with.
|
|
106
|
+
:param request_token: The OAuth token obtained from the previous step.
|
|
107
|
+
:param request_token_secret: The OAuth token secret obtained from the previous step.
|
|
102
108
|
:return: The access token and secret.
|
|
103
109
|
"""
|
|
104
|
-
return await self.connection.auth_manager.
|
|
110
|
+
return await self.connection.auth_manager.authorize(
|
|
111
|
+
verifier, request_token, request_token_secret
|
|
112
|
+
)
|
|
105
113
|
|
|
106
|
-
async def get_authorization_url(
|
|
114
|
+
async def get_authorization_url(
|
|
115
|
+
self, callback_url: str = "oob", confirm_user: bool = False
|
|
116
|
+
) -> str:
|
|
107
117
|
"""
|
|
108
118
|
Get the URL to authorize the client.
|
|
109
119
|
|
|
110
120
|
:param callback_url: The URL to redirect to after authorization, leave as "oob" for pin-based authorization.
|
|
121
|
+
:param confirm_user: Whether to confirm the user before authorizing the client.
|
|
111
122
|
:return: The URL to authorize the client.
|
|
112
123
|
"""
|
|
113
|
-
return await self.connection.auth_manager.get_authorization_url(
|
|
124
|
+
return await self.connection.auth_manager.get_authorization_url(
|
|
125
|
+
callback_url, confirm_user
|
|
126
|
+
)
|
|
114
127
|
|
|
115
128
|
def load_access_token(self, access_token: str, access_token_secret: str):
|
|
116
129
|
"""
|
|
@@ -160,9 +173,12 @@ class USOSClient:
|
|
|
160
173
|
"""
|
|
161
174
|
if not file_path.endswith(".json"):
|
|
162
175
|
raise ValueError("File must be a JSON file.")
|
|
176
|
+
access_token, access_token_secret = (
|
|
177
|
+
self.connection.auth_manager.get_access_token()
|
|
178
|
+
)
|
|
163
179
|
json_data = {
|
|
164
|
-
"access_token":
|
|
165
|
-
"access_token_secret":
|
|
180
|
+
"access_token": access_token,
|
|
181
|
+
"access_token_secret": access_token_secret,
|
|
166
182
|
}
|
|
167
183
|
with open(file_path, "w") as file:
|
|
168
184
|
json.dump(json_data, file)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|