example.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright 2009 Canonical Ltd. All rights reserved.
  2. #
  3. # This file is part of lazr.restfulclient
  4. #
  5. # lazr.restfulclient 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
  7. # the Free Software Foundation, version 3 of the License.
  8. #
  9. # lazr.restfulclient is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. # License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public License
  15. # along with lazr.restfulclient. If not, see <http://www.gnu.org/licenses/>.
  16. "Test client for the lazr.restful example web service."
  17. __metaclass__ = type
  18. __all__ = [
  19. "CookbookWebServiceClient",
  20. ]
  21. try:
  22. # Python 3.
  23. from urllib.parse import quote
  24. except ImportError:
  25. from urllib import quote
  26. from lazr.restfulclient.resource import (
  27. CollectionWithKeyBasedLookup,
  28. ServiceRoot,
  29. )
  30. class CookbookSet(CollectionWithKeyBasedLookup):
  31. """A custom subclass capable of cookbook lookup by cookbook name."""
  32. def _get_url_from_id(self, id):
  33. """Transform a cookbook name into the URL to a cookbook resource."""
  34. return (
  35. str(self._root._root_uri.ensureSlash())
  36. + "cookbooks/"
  37. + quote(str(id))
  38. )
  39. collection_of = "cookbook"
  40. class RecipeSet(CollectionWithKeyBasedLookup):
  41. """A custom subclass capable of recipe lookup by recipe ID."""
  42. def _get_url_from_id(self, id):
  43. """Transform a recipe ID into the URL to a recipe resource."""
  44. return str(self._root._root_uri.ensureSlash()) + "recipes/" + str(id)
  45. collection_of = "recipe"
  46. class CookbookWebServiceClient(ServiceRoot):
  47. RESOURCE_TYPE_CLASSES = dict(ServiceRoot.RESOURCE_TYPE_CLASSES)
  48. RESOURCE_TYPE_CLASSES["recipes"] = RecipeSet
  49. RESOURCE_TYPE_CLASSES["cookbooks"] = CookbookSet
  50. DEFAULT_SERVICE_ROOT = "http://cookbooks.dev/"
  51. DEFAULT_VERSION = "1.0"
  52. def __init__(
  53. self,
  54. service_root=DEFAULT_SERVICE_ROOT,
  55. version=DEFAULT_VERSION,
  56. cache=None,
  57. ):
  58. super(CookbookWebServiceClient, self).__init__(
  59. None, service_root, cache=cache, version=version
  60. )