UltimakerCloudScope.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. from PyQt6.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. """
  8. Add an Authorization header to the request for Ultimaker Cloud Api requests, if available.
  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.debug("User is not logged in for Cloud API request to {url}".format(url = request.url().toDisplayString()))
  20. return
  21. header_dict = {
  22. "Authorization": "Bearer {}".format(token)
  23. }
  24. self.addHeaders(request, header_dict)