File: app.py

package info (click to toggle)
freenub 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: python: 10,664; makefile: 7; sh: 6
file content (107 lines) | stat: -rw-r--r-- 2,773 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import logging
import os
import sys
import time

from flask import Flask, jsonify, request

d = os.path.dirname
PUBNUB_ROOT = d(d(d(os.path.dirname(os.path.abspath(__file__)))))
sys.path.append(PUBNUB_ROOT)

import pubnub as pn
from pubnub import utils
from pubnub.exceptions import PubNubException
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

pn.set_stream_logger("pubnub", logging.DEBUG)
logger = logging.getLogger("myapp")

app = Flask(__name__)

pnconfig = PNConfiguration()
pnconfig.subscribe_request_timeout = 10
pnconfig.subscribe_key = "sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe"
pnconfig.publish_key = "pub-c-739aa0fc-3ed5-472b-af26-aca1b333ec52"
pnconfig.uuid = "pubnub-demo-api-python-backend"
DEFAULT_CHANNEL = "pubnub_demo_api_python_channel"
EVENTS_CHANNEL = "pubnub_demo_api_python_events"
APP_KEY = utils.uuid()

pubnub = PubNub(pnconfig)
logger.info("SDK Version: %s", pubnub.SDK_VERSION)


@app.route("/app_key")
def app_key():
    return {"app_key": APP_KEY}


@app.route("/subscription/add")
def subscription_add():
    channel = request.args.get("channel")

    if channel is None:
        return jsonify({"error": "Channel missing"}), 500

    pubnub.subscribe().channels(channel).execute()
    return jsonify({"subscribed_channels": pubnub.get_subscribed_channels()})


@app.route("/subscription/remove")
def subscription_remove():
    channel = request.args.get("channel")

    if channel is None:
        return jsonify({"error": "Channel missing"}), 500

    pubnub.unsubscribe().channels(channel).execute()

    return jsonify({"subscribed_channels": pubnub.get_subscribed_channels()})


@app.route("/subscription/list")
def subscription_list():
    return jsonify({"subscribed_channels": pubnub.get_subscribed_channels()})


@app.route("/publish/sync")
def publish_sync():
    channel = request.args.get("channel")

    if channel is None:
        return jsonify({"error": "Channel missing"}), 500

    try:
        envelope = (
            pubnub.publish()
            .channel(channel)
            .message("hello from yield-based publish")
            .sync()
        )
        return jsonify({"original_response": str(envelope.status.original_response)})
    except PubNubException as e:
        return jsonify({"message": str(e)}), 500


@app.route("/publish/async")
def publish_async():
    channel = request.args.get("channel")

    if channel is None:
        return jsonify({"error": "Channel missing"}), 500

    def stub(res, state):
        pass

    pubnub.publish().channel(channel).message(
        "hello from yield-based publish"
    ).pn_async(stub)

    return jsonify({"message": "Publish task scheduled"})


if __name__ == "__main__":
    app.run(host="0.0.0.0")
    time.sleep(100)