versions.py

当rest api请求是版本号时候,调用该模块中的类Versions进行处理。
绑定也是在api-paste.ini文件中。

  1. /: neutronversions
  2. [app:neutronversions]
  3. paste.app_factory = neutron.api.versions:Versions.factory

调用的是Versions类中的factory()方法,该方法返回一个类的实体。该类是一个callable对象,主要函数就是__call__()方法。

  1. @webob.dec.wsgify(RequestClass=wsgi.Request)
  2. def __call__(self, req):
  3. """Respond to a request for all Neutron API versions."""
  4. version_objs = [
  5. {
  6. "id": "v2.0",
  7. "status": "CURRENT",
  8. },
  9. ]
  10. if req.path != '/':
  11. language = req.best_match_language()
  12. msg = _('Unknown API version specified')
  13. msg = gettextutils.translate(msg, language)
  14. return webob.exc.HTTPNotFound(explanation=msg)
  15. builder = versions_view.get_view_builder(req)
  16. versions = [builder.build(version) for version in version_objs]
  17. response = dict(versions=versions)
  18. metadata = {
  19. "application/xml": {
  20. "attributes": {
  21. "version": ["status", "id"],
  22. "link": ["rel", "href"],
  23. }
  24. }
  25. }
  26. content_type = req.best_match_content_type()
  27. body = (wsgi.Serializer(metadata=metadata).
  28. serialize(response, content_type))
  29. response = webob.Response()
  30. response.content_type = content_type
  31. response.body = body
  32. return response