<commit_before>import datetime

from google.appengine.ext import deferred

from backend.firebase import create_token, fb_delete, fb_get, fb_post, fb_put

DEFAULT_DURATION_MINUTES = 120

# Firebase Channel Schema
# /channels
#   - /<channel id>
#       - /_meta
#           - /status: Status of the channel. Either "open" or "closed".
# /clients
#   - /channel_ids
#       - /<client id>: Channel id for client.

def create_channel(client_id, duration_minutes=None):
    """Creates channel
    If called multiple times with same client_id, just returns a new token.
    """
    # TODO: Check what the Channel API does when called multiple times before
    # the token has expired. (Does it close the channel?)
    if duration_minutes is None:
        duration_minutes = DEFAULT_DURATION_MINUTES
    result = fb_post("/channels/", {
      "_meta": {
        "status": "open"
      }
    })
    if "error" in result:
        print "Error in creating channel: %s" % result["error"]
        return
    channel_id = result["name"]
    auth_payload = {
        "uid": "unused",
        "channel_id": channel_id,
        "provider": "gae"
    }
    expiration = datetime.datetime.now() + \
                     datetime.timedelta(minutes=duration_minutes)
    client_token = create_token(auth_payload, {"expires": expiration})

    # Save client channel id
    fb_put("/clients/channel_ids/%s" % client_id, channel_id)
    duration_secs = int(duration_minutes * 60)<commit_msg>Add comment when pushing a new channel to Firebase<commit_after>import datetime

from google.appengine.ext import deferred

from backend.firebase import create_token, fb_delete, fb_get, fb_post, fb_put

DEFAULT_DURATION_MINUTES = 120

# Firebase Channel Schema
# /channels
#   - /<channel id>
#       - /_meta
#           - /status: Status of the channel. Either "open" or "closed".
# /clients
#   - /channel_ids
#       - /<client id>: Channel id for client.

def create_channel(client_id, duration_minutes=None):
    """Creates channel
    If called multiple times with same client_id, just returns a new token.
    """
    # TODO: Check what the Channel API does when called multiple times before
    # the token has expired. (Does it close the channel?)
    if duration_minutes is None:
        duration_minutes = DEFAULT_DURATION_MINUTES
    # Push new channel to Firebase and get the Firebase push key
    result = fb_post("/channels/", {
      "_meta": {
        "status": "open"
      }
    })
    if "error" in result:
        print "Error in creating channel: %s" % result["error"]
        return
    channel_id = result["name"]
    auth_payload = {
        "uid": "unused",
        "channel_id": channel_id,
        "provider": "gae"
    }
    expiration = datetime.datetime.now() + \
                     datetime.timedelta(minutes=duration_minutes)
    client_token = create_token(auth_payload, {"expires": expiration})

    # Save client channel id
    fb_put("/clients/channel_ids/%s" % client_id, channel_id)
    duration_secs = int(duration_minutes * 60)
{'fraction_non_alphanumeric': 0.10296052631578947, 'fraction_numerical': 0.003289473684210526, 'mean_word_length': 2.873885350318471, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '874710', 'n_tokens_mistral': 943, 'n_tokens_neox': 896, 'n_words': 272}