• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hivesolutions / pushi / 313

pending completion
313

push

travis-ci-com

joamag
feat: dashed based dependencies

654 of 1694 relevant lines covered (38.61%)

1.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.15
/src/pushi/api/base.py
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3

4
# Hive Pushi System
5
# Copyright (c) 2008-2020 Hive Solutions Lda.
6
#
7
# This file is part of Hive Pushi System.
8
#
9
# Hive Pushi System is free software: you can redistribute it and/or modify
10
# it under the terms of the Apache License as published by the Apache
11
# Foundation, either version 2.0 of the License, or (at your option) any
12
# later version.
13
#
14
# Hive Pushi System is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# Apache License for more details.
18
#
19
# You should have received a copy of the Apache License along with
20
# Hive Pushi System. If not, see <http://www.apache.org/licenses/>.
21

22
__author__ = "João Magalhães <joamag@hive.pt>"
5✔
23
""" The author(s) of the module """
24

25
__version__ = "1.0.0"
5✔
26
""" The version of the module """
27

28
__revision__ = "$LastChangedRevision$"
5✔
29
""" The revision number of the module """
30

31
__date__ = "$LastChangedDate$"
5✔
32
""" The last change date of the module """
33

34
__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda."
5✔
35
""" The copyright for the module """
36

37
__license__ = "Apache License, Version 2.0"
5✔
38
""" The license for the module """
39

40
import hmac
5✔
41
import hashlib
5✔
42

43
import appier
5✔
44

45
from . import apn
5✔
46
from . import app
5✔
47
from . import web
5✔
48
from . import event
5✔
49
from . import subscription
5✔
50

51
BASE_URL = "https://puxiapp.com:9090/"
5✔
52
""" The base URL to be used by the API to access
53
the remote endpoints, should not be changed """
54

55
BASE_WS_URL = "wss://puxiapp.com/"
5✔
56
""" The default base websockets URL that is going
57
to be used in case no other value is specified """
58

59
class API(
5✔
60
    appier.API,
61
    apn.APNAPI,
62
    app.AppAPI,
63
    web.WebAPI,
64
    event.EventAPI,
65
    subscription.SubscriptionAPI
66
):
67
    """
68
    Base class for the construction of the pushi
69
    proxy object for interaction with the server
70
    side of the pushi system.
71

72
    Should provide the various methods that enable
73
    the developer to make operations for both read
74
    and write (create and read).
75
    """
76

77
    def __init__(self, *args, **kwargs):
5✔
78
        appier.API.__init__(self, *args, **kwargs)
×
79
        self.app_id = appier.conf("PUSHI_ID", None)
×
80
        self.app_key = appier.conf("PUSHI_KEY", None)
×
81
        self.app_secret = appier.conf("PUSHI_SECRET", None)
×
82
        self.base_url = appier.conf("PUSHI_URL", BASE_URL)
×
83
        self.app_id = kwargs.get("app_id", self.app_id)
×
84
        self.app_key = kwargs.get("app_key", self.app_key)
×
85
        self.app_secret = kwargs.get("app_secret", self.app_secret)
×
86
        self.base_url = kwargs.get("base_url", self.base_url)
×
87
        self.token = None
×
88

89
    def build(
5✔
90
        self,
91
        method,
92
        url,
93
        data = None,
94
        data_j = None,
95
        data_m = None,
96
        headers = None,
97
        params = None,
98
        mime = None,
99
        kwargs = None
100
    ):
101
        auth = kwargs.pop("auth", True)
×
102
        if auth: kwargs["sid"] = self.get_token()
×
103

104
    def get_token(self):
5✔
105
        if self.token: return self.token
×
106
        return self.login()
×
107

108
    def auth_callback(self, params, headers):
5✔
109
        token = self.login()
×
110
        params["sid"] = token
×
111

112
    def login(self):
5✔
113
        # tries to login in the pushi infra-structure using the
114
        # login route together with the full set of auth info
115
        # retrieving the result map that should contain the
116
        # session token, to be used in further calls
117
        result = self.get(
×
118
            self.base_url + "login",
119
            callback = False,
120
            auth = False,
121
            app_id = self.app_id,
122
            app_key = self.app_key,
123
            app_secret = self.app_secret
124
        )
125

126
        # unpacks the token value from the result map and then
127
        # returns the token to the caller method
128
        self.token = result["token"]
×
129
        return self.token
×
130

131
    def logout(self):
5✔
132
        # runs the "simplistic" call to the logout operation so
133
        # that the session is invalidated from the server side
134
        self.get(
×
135
            self.base_url + "logout"
136
        )
137

138
        # invalidates the currently set token so that it's no longer
139
        # going to be used for any kind of operation
140
        self.token = None
×
141

142
    def authenticate(self, channel, socket_id):
5✔
143
        # in case the app key is not defined for the current
144
        # instance an exception must be raised as it's not possible
145
        # to run the authentication process without an app key
146
        if not self.app_key: raise RuntimeError("No app key defined")
×
147

148
        # creates the string to hashed using both the provided
149
        # socket id and channel (concatenation)
150
        string = "%s:%s" % (socket_id, channel)
×
151
        string = appier.legacy.bytes(string)
×
152

153
        # runs the HMAC encryption in the provided secret and
154
        # the constructed string and returns a string containing
155
        # both the key and the hexadecimal digest
156
        app_secret = appier.legacy.bytes(str(self.app_secret))
×
157
        structure = hmac.new(app_secret, string, hashlib.sha256)
×
158
        digest = structure.hexdigest()
×
159
        return "%s:%s" % (self.app_key, digest)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc