aboutsummaryrefslogtreecommitdiffstats
path: root/github3/api.py
blob: b8b7617022296b79dd8cb05f55f4ca01f1cc6250 (plain) (blame)
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
# -*- coding: utf-8 -*-
"""
github3.api
~~~~~~~~~~~

This module implements the GitHub3 API wrapper objects.

:copyright: (c) 2011 by Kenneth Reitz.
:license: ISC, see LICENSE for more details.

"""

import urllib


from .config import settings
from .helpers import is_collection
from .packages import omnijson as json


class GithubCore(object):
    """The main GitHub API Interface."""

    def __init__(self):
        self.username = None
        self._auth = None


    @staticmethod
    def _resource_serialize(o):
        """Returns JSON serialization of given object."""
        return json.dumps(o)


    @staticmethod
    def _resource_deserialize(s):
        """Returns dict deserialization of a given JSON string."""

        try:
            return json.loads(s)
        except ValueError:
            raise ResponseError('The API Response was not valid.')


    def _generate_url(self, resource, params):
        """Generates Readability API Resource URL."""

        if is_collection(resource):
            resource = map(str, resource)
            resource = '/'.join(resource)

        if params:
            resource += '?%s' % (urllib.urlencode(params))

        return settings.domain + '/' + resource


class Github(GithubCore):
    """The user-facing GitHub API Interface."""

    def __init__(self):
        super(Github, self).__init__()


# ----------
# Exceptions
# ----------

class APIError(Exception):
    """There was an API Error."""

class PermissionsError(APIError):
    """You do not have proper permission."""

class AuthenticationError(APIError):
    """Authentication failed."""

class ResponseError(APIError):
    """The API Response was unexpected."""

class MissingError(APIError):
    """The Resource does not exist."""

class BadRequestError(APIError):
    """The request could not be understood due to bad syntax. Check your request and try again."""

class ServerError(APIError):
    """The server encountered an error and was unable to complete your request."""