Пример #1
0
def index():
    # if not authorized show login/auth view
    if not auth.get_token():
        return render_template('index.html')

    # show application view
    return render_template('application.html', has_token=True)
Пример #2
0
def thermostat_hist(thermostat_id=None, cache_ok=True):
    token = auth.get_token()
    if token == "":
        print "missing token, return 400"
        return "", 400

    if not thermostat_id:
        return jsonify({"error": 'Missing parameters'})

    device = None
    if cache_ok:
        print 'thermostat_hist: try to get thermostat from cache (session) from previous poll request'
        device = data_store.get_device('thermostats', thermostat_id)

    if not cache_ok or not device:
        print 'thermostat_hist: get thermostat from new api request'
        try:
            device = api.get_device(token, 'thermostats', thermostat_id)
        except APIError as err:
            return process_api_err(err)

    if not device:
        return jsonify({"error": "The thermostat was not found"})

    contents = views.get_thermostat_hist(thermostat_id, device)
    return jsonify(contents)
Пример #3
0
def apicontent():
    """  Calls Nest API to get data.
    Checks for changes to current data to determine whether any cached user data should be deleted.
    Returns content to be presented in client.
    """
    token = auth.get_token()
    if not token:
        print "missing token, return 400"
        return "", 400
    try:
        data = api.get_data(token)
    except APIError as err:
        return process_api_err(err)

    nestData = models.NestData(data)

    # compare data from last request, store device data for views, and delete data when required
    diff_list = data_store.process_data_changes(nestData)
    print "difference = ", diff_list

    # Verify the Nest account has at least one authorized structure in case it got deleted after authorizing it
    # (applies to new multi-structure authorization)
    if not nestData.has_structures():
        return jsonify(
            {"error": "No authorized structures found.  Please re-authorize."})

    apicontent = views.get_api_content(request, nestData, diff_list)
    return jsonify(apicontent)
Пример #4
0
    def test_callback(self):
        # get authorization code to exchange for an access token
        authorization_code = dummy_authcode_val  # request.args.get("code")
        token = None
        try:
            auth.get_access(authorization_code)
            token = auth.get_token()
        except urllib2.HTTPError as err:
            # expecting error message in JSON format, such as
            # {"error":"oauth2_error","error_description":"authorization code not found","instance_id":"65671693-1037-4d4e-bdab-f4852fe8aed0"}
            json_err = err.read()
            print "get_data error occurred: ", json_err

        self.assertIsNone(token,
                          "Token not returned using dummy authorization code")
Пример #5
0
def apiupdate(update_path=None):
    token = auth.get_token()
    if token == "":
        print "missing token, return 400"
        return "", 400

    data = views.convert_form(request)
    try:
        result = api.update(token, update_path, data)
    except APIError as err:
        return process_api_err(err)

    if result == True:
        return jsonify({"info": "API data was successfully updated."})
    else:
        return jsonify({"error": "Unknown error. API data was not updated."})
Пример #6
0
    def get_event_stream():
        token = auth.get_token()
        if not token:
            print "missing token, return 400"
            yield 'data: %s\n\n' % json.dumps({"error": "400 Missing token"})

        client = api.get_event_stream(token)
        print "got event stream client"
        for event in client.events():  # returns a generator
            event_type = event.event
            print "event: ", event_type
            if event_type == 'open':  # not always received here
                print "The event stream has been opened"
            elif event_type == 'put':
                print "The data has changed (or initial data sent)"
                print "data: ", event.data
            elif event_type == 'keep-alive':
                print "No data updates. Receiving an HTTP header to keep the connection open."
            elif event_type == 'auth_revoked' or event_type == 'cancel':
                print "revoked token: ", event
                auth.remove_access(auth_revoked=True)
                err = api.apierr_from_msg(401, 'Auth revoked')
                yield 'data: %s\n\n' % json.dumps(err.result)
            elif event_type == 'error':
                print "error message: ", event.data  # check if contains error code
                yield 'data: %s\n\n' % json.dumps({"error": event.data})
            else:
                print "Unknown event, no handler for it."

            if event_type == 'put':
                data = event.data
                dict_json = json.loads(event.data)
                nestData = models.NestData({"results": dict_json.get("data")})
                # compare data from last request, store device data for views, and delete data when required
                diff_list = data_store.process_data_changes(nestData)
                print "difference = ", diff_list
                # Verify the Nest account has at least one authorized structure in case it got deleted after authorizing it
                # (applies to new multi-structure authorization)
                if not nestData.has_structures():
                    yield 'data: %s\n\n' % json.dumps({
                        "error":
                        "No authorized structures found.  Please re-authorize."
                    })
                apicontent = views.get_api_content(request, nestData,
                                                   diff_list)
                yield 'data: %s\n\n' % json.dumps(apicontent)