__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*-coding:utf-8-*-
  2. #
  3. # Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Huawei Admin SDK for Python."""
  17. import threading
  18. from Service.VSeesHuaweiPushService.push_admin import _app
  19. _apps = {}
  20. _apps_lock = threading.RLock()
  21. _DEFAULT_APP_NAME = 'DEFAULT'
  22. def initialize_app(appid_at, appsecret_at, appid_push=None, token_server='https://oauth-login.cloud.huawei.com/oauth2/v3/token',
  23. push_open_url='https://push-api.cloud.huawei.com'):
  24. """
  25. Initializes and returns a new App instance.
  26. :param appid_at: appid parameters obtained by developer alliance applying for Push service
  27. :param appsecret_at: appsecret parameters obtained by developer alliance applying for Push service
  28. :param appid_push: the application Id in the URL
  29. :param token_server: Oauth server URL
  30. :param push_open_url: push open API URL
  31. """
  32. app = _app.App(appid_at, appsecret_at, appid_push, token_server=token_server, push_open_url=push_open_url)
  33. with _apps_lock:
  34. if appid_at not in _apps:
  35. _apps[appid_at] = app
  36. """set default app instance"""
  37. if _apps.get(_DEFAULT_APP_NAME) is None:
  38. _apps[_DEFAULT_APP_NAME] = app
  39. def get_app(appid=None):
  40. """
  41. get app instance
  42. :param appid: appid parameters obtained by developer alliance applying for Push service
  43. :return: app instance
  44. Raise: ValueError
  45. """
  46. if appid is None:
  47. with _apps_lock:
  48. app = _apps.get(_DEFAULT_APP_NAME)
  49. if app is None:
  50. raise ValueError('The default Huawei app is not exists. '
  51. 'This means you need to call initialize_app() it.')
  52. return app
  53. with _apps_lock:
  54. if appid not in _apps:
  55. raise ValueError('Huawei app id[{0}] is not exists. '
  56. 'This means you need to call initialize_app() it.'.format(appid))
  57. app = _apps.get(appid)
  58. if app is None:
  59. raise ValueError('The app id[{0}] is None.'.format(appid))
  60. return app