GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3d6413...7ed725 )
by Daniel
01:08
created

ConnectorDB.ping()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
from __future__ import absolute_import
2
3
from ._connection import DatabaseConnection
4
5
from ._device import Device
6
from ._user import User
7
from ._stream import Stream
8
9
CONNECTORDB_URL = "https://connectordb.com"
10
11
12
class ConnectorDB(Device):
13
    """ConnectorDB is the main entry point for any application that uses the python API.
14
    The class accepts both a username and password in order to log in as a user, and accepts an apikey
15
    when logging in directly from a device::
16
17
        import connectordb
18
        cdb = connectordb.ConnectorDB("myusername","mypassword")
19
20
        #prints "myusername/user" - logging in by username/password combo
21
        #logs in as the user device.
22
        print cdb.path
23
24
    """
25
    def __init__(self, user_or_apikey=None, user_password=None, url=CONNECTORDB_URL):
26
27
        db = DatabaseConnection(user_or_apikey, user_password, url)
28
29
        # ConnectorDB uses bcrypt by default for password hashing. While great for security
30
        # of passwords, it is extremely expensive, so it slows down queries. So, if we logged in
31
        # as a user with password, attempt to get the user device apikey to use for future authentication
32
        # so that queries are fast
33
        if user_password is not None:
34
            # Logins happen as a user device
35
            Device.__init__(self, db, user_or_apikey + "/user")
36
37
            if self.apikey is not None:
38
                # Reset the auth to be apikey
39
                db.setauth(self.apikey)
40
        else:
41
            # We logged in as a device - we have to ping the server to get our name
42
            Device.__init__(self, db, db.ping())
43
44
    def __call__(self, path):
45
        """Enables getting arbitrary users/devices/streams in a simple way. Just call the object
46
        with the u/d/s uri
47
            cdb = ConnectorDB("myapikey")
48
            cdb("user1") -> user1 object
49
            cdb("user1/device1") -> user1/device1 object
50
            cdb("user1/device1/stream1") -> user1/device1/stream1 object
51
        """
52
        n = path.count("/")
53
        if n == 0:
54
            return User(self.db, path)
55
        elif n == 1:
56
            return Device(self.db, path)
57
        else:
58
            return Stream(self.db, path)
59
60
    def close(self):
61
        """shuts down all active connections to ConnectorDB"""
62
        self.db.close()
63
64
    def reset_apikey(self):
65
        """invalidates the device's current api key, and generates a new one. Resets current auth to use the new apikey,
66
        since the change would have future queries fail if they use the old api key."""
67
        apikey = Device.reset_apikey(self)
68
        self.db.setauth(apikey)
69
        return apikey
70
71
    def count_users(self):
72
        """Gets the total number of users registered with the database. Only available to administrator."""
73
        return int(self.db.get("", {"q": "countusers"}).text)
74
75
    def count_devices(self):
76
        """Gets the total number of devices registered with the database. Only available to administrator."""
77
        return int(self.db.get("", {"q": "countdevices"}).text)
78
79
    def count_streams(self):
80
        """Gets the total number of streams registered with the database. Only available to administrator."""
81
        return int(self.db.get("", {"q": "countstreams"}).text)
82
83
    def info(self):
84
        """returns a dictionary of information about the database, including the database version, the transforms
85
        and the interpolators supported::
86
87
            >>>cdb = connectordb.ConnectorDB(apikey)
88
            >>>cdb.info()
89
            {
90
                "version": "0.3.0",
91
                "transforms": {
92
                    "sum": {"description": "Returns the sum of all the datapoints that go through the transform"}
93
                    ...
94
                },
95
                "interpolators": {
96
                    "closest": {"description": "Uses the datapoint closest to the interpolation timestamp"}
97
                    ...
98
                }
99
            }
100
101
        """
102
        return {
103
            "version": self.db.get("meta/version").text,
104
            "transforms": self.db.get("meta/transforms").json(),
105
            "interpolators": self.db.get("meta/interpolators").json()
106
        }
107
108
    def __repr__(self):
109
        return "[ConnectorDB:%s]" % (self.path, )
110
111
    def users(self):
112
            """Returns the list of users in the database"""
113
            result = self.db.read("", {"q": "ls"})
114
115
            if result is None or result.json() is None:
116
                return []
117
            users = []
118
            for u in result.json():
119
                usr = self(u["name"])
120
                usr.metadata = u
121
                users.append(usr)
122
            return users
123
124
    def ping(self):
125
        """Pings the ConnectorDB server. Useful for checking if the connection is valid"""
126
        return self.db.ping()
127