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