Debian Codegraph @

yarg/client.py

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