UltimakerCloudScope.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. from PyQt5.QtNetwork import QNetworkRequest
  2. from UM.Logger import Logger
  3. from UM.TaskManagement.HttpRequestScope import DefaultUserAgentScope
  4. from cura.API import Account
  5. from cura.CuraApplication import CuraApplication
  6. class UltimakerCloudScope(DefaultUserAgentScope):
  7. """Add an Authorization header to the request for Ultimaker Cloud Api requests.
  8. When the user is not logged in or a token is not available, a warning will be logged
  9. Also add the user agent headers (see DefaultUserAgentScope)
  10. """
  11. def __init__(self, application: CuraApplication):
  12. super().__init__(application)
  13. api = application.getCuraAPI()
  14. self._account = api.account # type: Account
  15. def requestHook(self, request: QNetworkRequest):
  16. super().requestHook(request)
  17. token = self._account.accessToken
  18. if not self._account.isLoggedIn or token is None:
  19. Logger.warning("Cannot add authorization to Cloud Api request")
  20. return
  21. header_dict = {
  22. "Authorization": "Bearer {}".format(token)
  23. }
  24. self.addHeaders(request, header_dict)