usos-api 0.1.1__tar.gz → 0.1.2__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.
Files changed (23) hide show
  1. {usos_api-0.1.1 → usos_api-0.1.2}/PKG-INFO +1 -1
  2. {usos_api-0.1.1 → usos_api-0.1.2}/pyproject.toml +1 -1
  3. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/auth.py +31 -12
  4. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/client.py +11 -6
  5. {usos_api-0.1.1 → usos_api-0.1.2}/README.md +0 -0
  6. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/__init__.py +0 -0
  7. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/connection.py +0 -0
  8. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/exceptions.py +0 -0
  9. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/logger.py +0 -0
  10. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/__init__.py +0 -0
  11. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/consumer.py +0 -0
  12. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/course.py +0 -0
  13. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/grade.py +0 -0
  14. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/group.py +0 -0
  15. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/lang_dict.py +0 -0
  16. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/programme.py +0 -0
  17. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/term.py +0 -0
  18. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/models/user.py +0 -0
  19. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/services/__init__.py +0 -0
  20. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/services/api_server.py +0 -0
  21. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/services/groups.py +0 -0
  22. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/services/users.py +0 -0
  23. {usos_api-0.1.1 → usos_api-0.1.2}/usos_api/utils/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: usos-api
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Asynchronous USOS API for Python
5
5
  Home-page: https://github.com/Antoni-Czaplicki/usos-api
6
6
  Author: Antoni-Czaplicki
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "usos-api"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "Asynchronous USOS API for Python"
5
5
  authors = ["Antoni-Czaplicki <56671347+Antoni-Czaplicki@users.noreply.github.com>"]
6
6
  readme = "README.md"
@@ -103,24 +103,37 @@ class AuthManager:
103
103
  await self._generate_request_token(callback_url)
104
104
  return f"{self.base_address}{self.AUTHORIZE_SUFFIX}?oauth_token={self._request_token}"
105
105
 
106
- async def authorize_with_token(self, token: str) -> tuple[str, str]:
106
+ async def authorize(self, verifier: str, request_token, request_token_secret):
107
107
  """
108
- Authorize the client with a token.
108
+ Authorize the client with verifier and optionally token and token secret.
109
109
 
110
- :param token: The token to authorize the client with.
110
+ :param verifier: The verifier to authorize the client with.
111
+ :param token: The OAuth token obtained from the previous step.
112
+ :param token_secret: The OAuth token secret obtained from the previous step.
111
113
  :return: The access token and secret.
112
114
  """
113
- self._oauth_client.verifier = token
115
+ self._oauth_client.verifier = verifier
116
+ if request_token:
117
+ self._oauth_client.resource_owner_key = request_token
118
+ if request_token_secret:
119
+ self._oauth_client.resource_owner_secret = request_token_secret
114
120
  url = f"{self.base_address}{self.ACCESS_TOKEN_SUFFIX}"
115
121
  url, headers, body = self._oauth_client.sign(url, http_method="POST")
116
- async with self._session.post(url, data=body, headers=headers) as response:
117
- await self._handle_response_errors(response)
118
- data = dict(urllib.parse.parse_qsl(await response.text()))
119
- self.load_access_token(data["oauth_token"], data["oauth_token_secret"])
120
- _LOGGER.info(
121
- f"Authorization successful, received access token: {self.access_token}"
122
- )
123
- return self.access_token, self.access_token_secret
122
+ try:
123
+ async with self._session.post(url, data=body, headers=headers) as response:
124
+ await self._handle_response_errors(response)
125
+ data = dict(urllib.parse.parse_qsl(await response.text()))
126
+ self.load_access_token(data["oauth_token"], data["oauth_token_secret"])
127
+ _LOGGER.info(
128
+ f"Authorization successful, received access token: {self.access_token}"
129
+ )
130
+ return self.access_token, self.access_token_secret
131
+ except AttributeError as e:
132
+ if e.args[0] == "'NoneType' object has no attribute 'post'":
133
+ raise USOSAPIException(
134
+ "Authorization failed. Did you forget to open the manager?"
135
+ )
136
+ raise
124
137
 
125
138
  def load_access_token(self, access_token: str, access_token_secret: str):
126
139
  """
@@ -138,6 +151,12 @@ class AuthManager:
138
151
  resource_owner_secret=self.access_token_secret,
139
152
  )
140
153
 
154
+ def get_access_token(self):
155
+ return self.access_token, self.access_token_secret
156
+
157
+ def get_request_token(self):
158
+ return self._request_token, self._request_token_secret
159
+
141
160
  def sign_request(
142
161
  self, url: str, http_method: str = "GET", **kwargs
143
162
  ) -> tuple[str, dict, dict]:
@@ -94,14 +94,18 @@ 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(self, token: str) -> tuple[str, str]:
97
+ async def authorize(self, verifier: str, request_token: str = None, request_token_secret: str = None) -> tuple[str, str]:
98
98
  """
99
- Authorize the client with a token.
99
+ Authorize the client with verifier and optionally token and token secret.
100
100
 
101
- :param token: The token to authorize the client with.
101
+ 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.
102
+
103
+ :param verifier: The verifier to authorize the client with.
104
+ :param request_token: The OAuth token obtained from the previous step.
105
+ :param request_token_secret: The OAuth token secret obtained from the previous step.
102
106
  :return: The access token and secret.
103
107
  """
104
- return await self.connection.auth_manager.authorize_with_token(token)
108
+ return await self.connection.auth_manager.authorize(verifier, request_token, request_token_secret)
105
109
 
106
110
  async def get_authorization_url(self, callback_url: str = "oob"):
107
111
  """
@@ -160,9 +164,10 @@ class USOSClient:
160
164
  """
161
165
  if not file_path.endswith(".json"):
162
166
  raise ValueError("File must be a JSON file.")
167
+ access_token, access_token_secret = self.connection.auth_manager.get_access_token()
163
168
  json_data = {
164
- "access_token": self.connection.auth_manager.access_token,
165
- "access_token_secret": self.connection.auth_manager.access_token_secret,
169
+ "access_token": access_token,
170
+ "access_token_secret": access_token_secret,
166
171
  }
167
172
  with open(file_path, "w") as file:
168
173
  json.dump(json_data, file)
File without changes
File without changes
File without changes