uris.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2009 Canonical Ltd.
  2. # This file is part of launchpadlib.
  3. #
  4. # launchpadlib is free software: you can redistribute it and/or modify it
  5. # under the terms of the GNU Lesser General Public License as published by the
  6. # Free Software Foundation, version 3 of the License.
  7. #
  8. # launchpadlib is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  11. # for more details.
  12. #
  13. # You should have received a copy of the GNU Lesser General Public License
  14. # along with launchpadlib. If not, see <http://www.gnu.org/licenses/>.
  15. """Launchpad-specific URIs and convenience lookup functions.
  16. The code in this module lets users say "staging" when they mean
  17. "https://api.staging.launchpad.net/".
  18. """
  19. __metaclass__ = type
  20. __all__ = [
  21. "lookup_service_root",
  22. "lookup_web_root",
  23. "web_root_for_service_root",
  24. ]
  25. try:
  26. from urllib.parse import urlparse
  27. except ImportError:
  28. from urlparse import urlparse
  29. import warnings
  30. from lazr.uri import URI
  31. LPNET_SERVICE_ROOT = "https://api.launchpad.net/"
  32. QASTAGING_SERVICE_ROOT = "https://api.qastaging.launchpad.net/"
  33. STAGING_SERVICE_ROOT = "https://api.staging.launchpad.net/"
  34. DEV_SERVICE_ROOT = "https://api.launchpad.test/"
  35. DOGFOOD_SERVICE_ROOT = "https://api.dogfood.paddev.net/"
  36. TEST_DEV_SERVICE_ROOT = "http://api.launchpad.test:8085/"
  37. LPNET_WEB_ROOT = "https://launchpad.net/"
  38. QASTAGING_WEB_ROOT = "https://qastaging.launchpad.net/"
  39. STAGING_WEB_ROOT = "https://staging.launchpad.net/"
  40. DEV_WEB_ROOT = "https://launchpad.test/"
  41. DOGFOOD_WEB_ROOT = "https://dogfood.paddev.net/"
  42. TEST_DEV_WEB_ROOT = "http://launchpad.test:8085/"
  43. # If you use EDGE_SERVICE_ROOT, or its alias, or the equivalent
  44. # string, launchpadlib will issue a deprecation warning and use
  45. # PRODUCTION_SERVICE_ROOT instead. Similarly for EDGE_WEB_ROOT.
  46. EDGE_SERVICE_ROOT = "https://api.edge.launchpad.net/"
  47. EDGE_WEB_ROOT = "https://edge.launchpad.net/"
  48. service_roots = dict(
  49. production=LPNET_SERVICE_ROOT,
  50. edge=LPNET_SERVICE_ROOT,
  51. qastaging=QASTAGING_SERVICE_ROOT,
  52. staging=STAGING_SERVICE_ROOT,
  53. dogfood=DOGFOOD_SERVICE_ROOT,
  54. dev=DEV_SERVICE_ROOT,
  55. test_dev=TEST_DEV_SERVICE_ROOT,
  56. )
  57. web_roots = dict(
  58. production=LPNET_WEB_ROOT,
  59. edge=LPNET_WEB_ROOT,
  60. qastaging=QASTAGING_WEB_ROOT,
  61. staging=STAGING_WEB_ROOT,
  62. dogfood=DOGFOOD_WEB_ROOT,
  63. dev=DEV_WEB_ROOT,
  64. test_dev=TEST_DEV_WEB_ROOT,
  65. )
  66. def _dereference_alias(root, aliases):
  67. """Dereference what might a URL or an alias for a URL."""
  68. if root == "edge":
  69. warnings.warn(
  70. (
  71. "Launchpad edge server no longer exists. "
  72. "Using 'production' instead."
  73. ),
  74. DeprecationWarning,
  75. )
  76. if root in aliases:
  77. return aliases[root]
  78. # It's not an alias. Is it a valid URL?
  79. (scheme, netloc, path, parameters, query, fragment) = urlparse(root)
  80. if scheme != "" and netloc != "":
  81. return root
  82. # It's not an alias or a valid URL.
  83. raise ValueError(
  84. "%s is not a valid URL or an alias for any Launchpad " "server" % root
  85. )
  86. def lookup_service_root(service_root):
  87. """Dereference an alias to a service root.
  88. A recognized server alias such as "staging" gets turned into the
  89. appropriate URI. A URI gets returned as is. Any other string raises a
  90. ValueError.
  91. """
  92. if service_root == EDGE_SERVICE_ROOT:
  93. # This will trigger a deprecation warning and use production instead.
  94. service_root = "edge"
  95. return _dereference_alias(service_root, service_roots)
  96. def lookup_web_root(web_root):
  97. """Dereference an alias to a website root.
  98. A recognized server alias such as "staging" gets turned into the
  99. appropriate URI. A URI gets returned as is. Any other string raises a
  100. ValueError.
  101. """
  102. if web_root == EDGE_WEB_ROOT:
  103. # This will trigger a deprecation warning and use production instead.
  104. web_root = "edge"
  105. return _dereference_alias(web_root, web_roots)
  106. def web_root_for_service_root(service_root):
  107. """Turn a service root URL into a web root URL.
  108. This is done heuristically, not with a lookup.
  109. """
  110. service_root = lookup_service_root(service_root)
  111. web_root_uri = URI(service_root)
  112. web_root_uri.path = ""
  113. web_root_uri.host = web_root_uri.host.replace("api.", "", 1)
  114. web_root = str(web_root_uri.ensureSlash())
  115. return web_root