client.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. # (The MIT License)
  3. #
  4. # Copyright (c) 2014 Kura
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the 'Software'), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import requests
  24. from .exceptions import HTTPError
  25. from .package import json2package
  26. def get(package_name, pypi_server="https://pypi.python.org/pypi/"):
  27. """
  28. Constructs a request to the PyPI server and returns a
  29. :class:`yarg.package.Package`.
  30. :param package_name: case sensitive name of the package on the PyPI server.
  31. :param pypi_server: (option) URL to the PyPI server.
  32. >>> import yarg
  33. >>> package = yarg.get('yarg')
  34. <Package yarg>
  35. """
  36. if not pypi_server.endswith("/"):
  37. pypi_server = pypi_server + "/"
  38. response = requests.get("{0}{1}/json".format(pypi_server,
  39. package_name))
  40. if response.status_code >= 300:
  41. raise HTTPError(status_code=response.status_code,
  42. reason=response.reason)
  43. if hasattr(response.content, 'decode'):
  44. return json2package(response.content.decode())
  45. else:
  46. return json2package(response.content)