/usr/share/pyshared/lazr/restful/docs/absoluteurl.txt is in python-lazr.restful 0.19.3-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | URL generation
**************
lazr.restful includes some classes that make it easy to generate URLs
for your objects.
RootResourceAbsoluteURL
=======================
It's easy to have an object implement ILocation and define its
URL recursively, in terms of its parent's URL. But the recursion has
to bottom out somewhere, so you need at least one real implementation
of IAbsoluteURL. lazr.simple.RootResourceAbsoluteURL is designed to
build a URL for your service root resource out of information you put
into your IWebServiceConfiguration implementation.
First, RootResourceAbsoluteURL needs to be registered.
>>> from zope.component import getSiteManager
>>> from lazr.restful.simple import RootResourceAbsoluteURL
>>> sm = getSiteManager()
>>> sm.registerAdapter(RootResourceAbsoluteURL)
Next, you need a configuration object.
>>> from zope.interface import implements
>>> from lazr.restful.interfaces import IWebServiceConfiguration
>>> class SimpleWebServiceConfiguration:
... implements(IWebServiceConfiguration)
... hostname = "hostname"
... service_root_uri_prefix = "root_uri_prefix/"
... active_versions = ['active_version', 'latest_version']
... last_version_with_mutator_named_operations = None
... port = 1000
... use_https = True
As always, the configuration object needs to be registered as the
utility for IWebServiceConfiguration.
>>> configuration = SimpleWebServiceConfiguration()
>>> sm.registerUtility(configuration)
Now we can get a RootResourceAbsoluteURL object, given a root resource
and a request.
>>> from zope.component import getMultiAdapter
>>> from lazr.restful.simple import RootResource, Request
>>> from zope.traversing.browser.interfaces import IAbsoluteURL
>>> resource = RootResource()
>>> request = Request("", {})
>>> request.annotations[request.VERSION_ANNOTATION] = (
... 'active_version')
>>> adapter = getMultiAdapter((resource, request), IAbsoluteURL)
Calling the RootResourceAbsoluteURL will give the service root's
absolute URL.
>>> print adapter()
https://hostname:1000/root_uri_prefix/active_version/
Converting the adapter to a string will give the same result, but
without the trailing slash.
>>> print str(adapter)
https://hostname:1000/root_uri_prefix/active_version
(This is useful for the recursive case. When finding the URL to a
subordinate resource, Zope's AbsoluteURL implementation calls str() on
this object and joins that string to the subordinate object's name
with a slash. Putting a slash here would result in subordinate objects
having two consecutive slashes in their URLs.)
Changing the web service configuration changes the generated URLs.
>>> configuration.use_https = False
>>> print getMultiAdapter((resource, request), IAbsoluteURL)()
http://hostname:1000/root_uri_prefix/active_version/
>>> configuration.port = None
>>> print getMultiAdapter((resource, request), IAbsoluteURL)()
http://hostname/root_uri_prefix/active_version/
The URL generated includes a version identifier taken from the
value of the 'lazr.restful.version' annotation.
>>> request.annotations[request.VERSION_ANNOTATION] = (
... 'latest_version')
>>> adapter = getMultiAdapter((resource, request), IAbsoluteURL)
>>> print adapter()
http://hostname/root_uri_prefix/latest_version/
For purposes of URL generation, the annotation doesn't have to be a
real version defined by the web service. Any string will do.
>>> request.annotations[request.VERSION_ANNOTATION] = (
... 'no_such_version')
>>> adapter = getMultiAdapter((resource, request), IAbsoluteURL)
>>> print adapter()
http://hostname/root_uri_prefix/no_such_version/
(However, the lazr.restful traversal code will reject an invalid
version, and so in a real application lazr.restful.version will not be
set. See examples/base/tests/service.txt for that test.)
Cleanup.
>>> request.annotations[request.VERSION_ANNOTATION] = (
... 'active_version')
MultiplePathPartAbsoluteURL
===========================
Zope has an AbsoluteURL class that makes it easy to turn an object
tree into a set of treelike URL paths:
http://top-level/
http://top-level/child
http://top-level/child/grandchild
And so on. It assumes that each object provides one part of the URL's
path. But what if a single object in the tree provides multiple URL's
path parts?
http://top-level/child-part1/child-part2/grandchild
AbsoluteURL won't work, but lazr.restful's MultiplePathPartAbsoluteURL
will.
To test this, we'll start with a DummyRootResource and its absolute
URL generator, DummyRootResourceURL.
>>> from lazr.restful.testing.webservice import (
... DummyRootResource, DummyRootResourceURL)
>>> from zope.component import getSiteManager
>>> sm = getSiteManager()
>>> sm.registerAdapter(DummyRootResourceURL)
We'll load the basic lazr.restful site configuration.
>>> from zope.configuration import xmlconfig
>>> def load_config():
... xmlconfig.string("""
... <configure xmlns="http://namespaces.zope.org/zope">
... <include package="lazr.restful" file="basic-site.zcml" />
... <adapter
... for="zope.location.interfaces.ILocation
... lazr.restful.publisher.WebServiceRequestTraversal"
... provides="zope.traversing.browser.interfaces.IAbsoluteURL"
... factory="zope.traversing.browser.absoluteurl.AbsoluteURL"
... />
... </configure>
... """)
>>> load_config()
Here's a child of DummyRootResource that implements
IMultiplePathPartLocation.
>>> from zope.interface import implements
>>> from lazr.restful.simple import IMultiplePathPartLocation
>>> class ChildResource:
... implements(IMultiplePathPartLocation)
...
... __parent__ = DummyRootResource()
... __path_parts__ = ["child-part1", "child-part2"]
The ChildResource's URL includes one URL part from the root resource,
followed by two from the ChildResource itself.
>>> resource = ChildResource()
>>> print str(getMultiAdapter((resource, request), IAbsoluteURL))
http://dummyurl/child-part1/child-part2
Now let's put an object underneath the child resource that implements
ILocation, as most resources will.
>>> from zope.location.interfaces import ILocation
>>> class GrandchildResource:
... implements(ILocation)
... __parent__ = ChildResource()
... __name__ = "grandchild"
The GrandchildResource's URL contains the URL part from the root
resource, the two from the ChildResource, and one from the
GrandchildResource itself.
>>> print str(getMultiAdapter(
... (GrandchildResource(), request), IAbsoluteURL))
http://dummyurl/child-part1/child-part2/grandchild
Edge cases and error handling
=============================
MultiplePathPartAbsoluteURL escapes the same characters as AbsoluteURL.
It even escapes slashes, if a slash shows up inside a path part.
>>> resource.__path_parts__ = ["!foo!", "bar/baz"]
>>> print str(getMultiAdapter((resource, request), IAbsoluteURL))
http://dummyurl/%21foo%21/bar%2Fbaz
If the __path_parts__ is not iterable, an attempt to get the URL
raises an exception:
>>> resource.__path_parts__ = "foobar"
>>> str(getMultiAdapter((resource, request), IAbsoluteURL))
Traceback (most recent call last):
...
TypeError: Expected an iterable for __path_parts__.
If the __parent__ or __path_parts__ is missing or None, an attempt to
get the URL raises the same exception as AbsoluteURL does.
>>> resource.__path_parts__ = None
>>> str(getMultiAdapter((resource, request), IAbsoluteURL))
Traceback (most recent call last):
...
TypeError: There isn't enough context to get URL information...
>>> resource.__path_parts__ = ["foo", "bar"]
>>> resource.__parent__ = None
>>> str(getMultiAdapter((resource, request), IAbsoluteURL))
Traceback (most recent call last):
...
TypeError: There isn't enough context to get URL information...
|