Пример #1
0
def addGoogleToken():
    """
    Description\n
        POST route to add a google token to an authenticated user
    Json-data
        googleToken : google token from oauth\t
        googleId : google id of user
    Headers
        Authorization : Bearer token
    Return
        200 code for succes and 400 for failure
    """
    users = db.child('Users').get()
    authorization = request.headers.get("Authorization")
    token = check_token(authorization, users)
    if token == False:
        return serialize_response('Unknown user', {}, 400)
    user = token
    data = request.json
    try:
        google_token = data["googleToken"]
        google_id = data["googleId"]
    except:
        return serialize_response('Missing parameters', {}, 400)
    user["tokens"]["googleToken"] = google_token
    user["tokens"]["googleId"] = google_id
    db.child("Users").child(user["tokens"]["area"]).set(user)
    return serialize_response('Tokens added', {}, 200)
Пример #2
0
def addTwitterToken():
    """
    Description\n
        POST route to add a twitter token to an authenticated user
    Json-data
        twitterToken : twitter token from oauth\t
        twitterSecret : twitter secret from oauth
    Headers
        Authorization : Bearer token
    Return
        200 code for succes and 400 for failure
    """
    users = db.child('Users').get()
    authorization = request.headers.get("Authorization")
    token = check_token(authorization, users)
    if token == False:
        return serialize_response('Unkown user', {}, 400)
    user = token
    data = request.json
    try:
        twitter_token = data["twitterToken"]
        twitter_secret = data["twitterSecret"]
    except:
        return serialize_response('Missing parameters', {}, 400)
    user["tokens"]["twitterToken"] = twitter_token
    user["tokens"]["twitterSecret"] = twitter_secret
    db.child("Users").child(user["tokens"]["area"]).set(user)
    return serialize_response('Tokens added', {}, 200)
Пример #3
0
def rmv_triggers():
    """
    Description\n
        POST route to remove a trigger from a user list of triggers
    Json-data
        triggerId : trigger id of authenticated user\t
    Headers
        Authorization : Bearer token
    Return
        200 code for succes and 400 for failure
    """
    users = db.child('Users').get()
    authorization = request.headers.get("Authorization")
    token = check_token(authorization, users)
    if token == False:
        return serialize_response('Unkown user', {}, 400)
    user = get_user(authorization.split(' ')[1], users)
    val = user.val()
    data = request.json
    trigger_id = int(data["triggerId"])
    job_id = val["triggers"][trigger_id]["job_id"]
    try:
        if len(val["triggers"]) == 1:
            val["triggers"] = [-1]
        else:
            val["triggers"].pop(trigger_id)
    except:
        return serialize_response('Invalid trigger id', {}, 400)
    try:
        scheduler.remove_job(job_id)
        db.child("Users").child(user.key()).set(val)
    except:
        return serialize_response('Invalid trigger id', {}, 400)
    return serialize_response('Trigger deleted', {}, 200)
Пример #4
0
def add_trigger():
    """
    Description\n
        POST route to add a new trigger
    Json-data
        action_id = array index of actions\t
        reaction_id = array index of reactions\t
        action_data = json data of action\t
        reaction_data = json data of reaction\t
    Headers
        Authorization : Bearer token
    Return
        200 code for succes and 400 for failure
    """
    users = db.child('Users').get()
    authorization = request.headers.get("Authorization")
    token = check_token(authorization, users)
    if token == False:
        return serialize_response('Unkown user', {}, 400)
    user = get_user(authorization.split(' ')[1], users)
    data = request.json
    try:
        action_id = int(data["action_id"])
        reaction_id = int(data["reaction_id"])
        action_data = data["action_data"]
        reaction_data = data["reaction_data"]
    except:
        return serialize_response('Missing parameters', {}, 400)
    action = action_array[action_id]
    reaction = reaction_array[reaction_id]
    action_res = action[2](user.val(), action_data)
    if action_res != True:
        return serialize_response(action_res, {}, 400)
    reaction_res = reaction[1](user.val(), reaction_data)
    if reaction_res != True:
        return serialize_response(reaction_res, {}, 400)
    job_id = user.key() + str(int(time.time() * 10000))
    trigger_id = add_trigger_to_db(job_id, user.val(), user.key(), action_id,
                                   reaction_id, action_data, reaction_data)
    val = user.val()
    args = [val, trigger_id, reaction[0], job_id]
    scheduler.add_job(func=action[0],
                      trigger="interval",
                      seconds=action[1],
                      args=args,
                      max_instances=100,
                      id=job_id)
    return serialize_response('Here you are', {}, 200)
Пример #5
0
def get_triggers():
    """
    Description\n
        GET route to get all user triggers
    Json-data
        twitterToken : twitter token from oauth\t
        twitterSecret : twitter secret from oauth
    Headers
        Authorization : Bearer token
    Return
        200 code for succes and 400 for failure
    """
    users = db.child('Users').get()
    authorization = request.headers.get("Authorization")
    user = check_token(authorization, users)
    if user == False:
        return serialize_response('Unkown user', {}, 400)
    return serialize_response('Here are the triggers',
                              {"triggers": user["triggers"]}, 200)