Exemple #1
0
def handle(req):
    """handle a request to the function
    Args:
        req (str): a JSON string with the following fields:
            int req_id (128-bit uuid4 integer)
            int post_type
    Return:
        on success, return {"status": "success", ...TBD}
        on error, return {"status": "UploadUniqueIdError", "errors": [errors from
        each function call]}
    """
    payload = json.loads(req)

    if ('req_id' not in payload or
       'post_type' not in payload):
        msg = '''Make sure the input has `req_id` and `post_type` fields'''
        ret = json.dumps({"status":"MissingFieldError", "errors":[msg]})
        sys.exit(ret)

    post_id= uuid.uuid4().int

    payload['post_id'] = post_id
    function_url = "http://gateway.openfaas:8080/function/compose-post-service-upload-unique-id"
    ret = ds_util.invoke(function_url, payload)

    if ret['http_status_code'] != 200:
        sys.exit(json.dumps({"status":"UniqueIdServiceUploadUniqueIdError",
                             "errors": [ret]}))

    return dumps({"status":"success", "post_id": post_id})
Exemple #2
0
def handle(req):
    """handle a request to the function
    Args:
        req (str): a JSON string with the following fields:
        int req_id (128-bit uuid4 integer)
        string user_id
        string username
    Return:

    """
    payload = json.loads(req)

    if ('username' not in payload or
       'user_id' not in payload or
       'req_id' not in payload):
        msg = '''Make sure the input has `username`, `user_id`, `req_id`'''
        ret = json.dumps({"status":"MissingFieldError", "message":msg})
        sys.exit(ret)

    function_url = "http://gateway.openfaas:8080/function/compose-post-service-upload-creator"
    ret = ds_util.invoke(function_url, payload)

    if ret['http_status_code'] != 200:
        sys.exit(dumps({"status":"UploadCreatorError",
                             "errors": [ret]}))

    return dumps({"status":"success",
                  "creator": {"username":payload['username'], "user_id": payload['user_id']}
                 })
Exemple #3
0
def handle(req):
    """Client API to read the timeline of a user

    Calls user-timeline-service-read-user-timeline to acquire a user's timeline,
    formats the data in the right fashion and return it back to the client
    (e.g., a browser).

    Args:
        req (str): request body
    """
    payload = json.loads(req)

    if ('user_id' not in payload or 'start' not in payload
            or 'stop' not in payload):
        msg = '''Make sure the input has `user_id`, `start`, `stop`'''
        ret = json.dumps({"status": "MissingFieldError", "message": msg})
        sys.exit(ret)

    function_url = "http://gateway.openfaas:8080/function/user-timeline-service-read-user-timeline"

    ret = ds_util.invoke(function_url, payload)

    if ret["http_status_code"] != 200:
        return dumps({
            "status": "UserTimelineReadFrontendError",
            "errors": [ret]
        })

    return dumps({"status": "success", "posts": format_posts(ret)})
Exemple #4
0
def upload_user_id(req_id, user_id, username):
    '''
    return a JSON dict
    '''
    req = {"req_id": req_id, "user_id": user_id, "username": username}
    function_url = "http://gateway.openfaas:8080/function/user-service-upload-creator-with-user-id"

    return ds_util.invoke(function_url, req)
Exemple #5
0
def upload_text(req_id, text):
    '''
    return a JSON dict
    '''
    req = {"req_id": req_id, "text": text}
    function_url = "http://gateway.openfaas:8080/function/text-service-upload-text"

    return ds_util.invoke(function_url, req)
Exemple #6
0
def upload_media(req_id, media_type, media_id):
    '''
    return a JSON dict
    '''
    req = {"req_id": req_id, "media_type": media_type, "media_id": media_id}
    function_url = "http://gateway.openfaas:8080/function/media-service-upload-media"

    return ds_util.invoke(function_url, req)
Exemple #7
0
def upload_unique_id(req_id, post_type):
    '''
    return a JSON dict
    '''
    req = {"req_id": req_id, "post_type": post_type}
    function_url = "http://gateway.openfaas:8080/function/unique-id-service-upload-unique-id"

    return ds_util.invoke(function_url, req)
Exemple #8
0
def compose_post_service_upload_urls(req_id, url_docs):
    '''
    return a JSON dict
    '''
    req = {"req_id": req_id, "url_docs": url_docs}
    function_url = "http://gateway.openfaas:8080/function/compose-post-service-upload-urls"

    return ds_util.invoke(function_url, req)
Exemple #9
0
def upload_user_mentions(req_id, usernames):
    '''
    user_mentions: a list of usernames ste each with an @ sign prefix
    '''
    req = {"req_id": req_id, "usernames": usernames}
    function_url = "http://gateway.openfaas:8080/function/user-mention-service-upload-user-mentions"

    return ds_util.invoke(function_url, req)
Exemple #10
0
def upload_post_helper(req_id, post):
    '''Call post-storage-service-store-post to write the post into DB
    '''
    req = {"req_id":req_id,
           "post":post}
    function_url = "http://gateway.openfaas:8080/function/post-storage-service-store-post"

    return ds_util.invoke(function_url, req)
Exemple #11
0
def upload_user_timeline_helper(req_id, post_id, creator_id, post_tsp):
    '''Call user-timeline-service-write-user-timeline to ...
    '''
    req = {"req_id":req_id,
           "post_id":post_id,
           "creator_id": creator_id,
           "timestamp": post_tsp}
    function_url = "http://gateway.openfaas:8080/function/user-timeline-service-write-user-timeline"

    return ds_util.invoke(function_url, req)
Exemple #12
0
def shorten_and_upload_urls(req_id, urls):
    '''
    Args:
        urls: a list of str of urls. urls can be the empty list

    return a JSON dict
    '''
    req = {"req_id": req_id, "urls": urls}
    function_url = "http://gateway.openfaas:8080/function/url-shorten-service-upload-urls"

    return ds_util.invoke(function_url, req)
Exemple #13
0
def register(req):
    values = parse_qs(req)
    req = {
        "username": values['username'][0],
        "first_name": values['first_name'][0],
        "last_name": values['last_name'][0],
        "password": values['password'][0]
    }

    function_url = "http://gateway.openfaas:8080/function/register-user"

    ret = ds_util.invoke(function_url, req)

    if ret['http_status_code'] != 200:
        return error_html_f

    return login_html_f
Exemple #14
0
def login(req):
    values = parse_qs(req)
    if ("username" not in values or "password" not in values):
        return login_html_f

    req = {
        "username": values['username'][0],
        "password": values['password'][0]
    }

    function_url = "http://gateway.openfaas:8080/function/user-service-login"

    ret = ds_util.invoke(function_url, req)

    if ret['http_status_code'] != 200:
        return login_html_f

    return main(req)
Exemple #15
0
def upload_text(req_id, text):
    req = {"req_id": req_id, "text": text}
    function_url = "http://gateway.openfaas:8080/function/compose-post-service-upload-text"

    return ds_util.invoke(function_url, req)
Exemple #16
0
def compose_and_upload(req_id):
    req = {"req_id": req_id}
    function_url = "http://gateway.openfaas:8080/function/compose-post-service-compose-and-upload"

    return ds_util.invoke(function_url, req)
Exemple #17
0
def handle(req):
    """Given a user_id and a range, return all posts of that use within that
    range.

    Acquire the post_ids from Redis and call post-storage-service-read-posts to
    obtain posts based on the list of post_ids. A range is represented by the
    `start` and `stop` input parameters. They are the starting the ending
    indexes of post_ids in the sorted set in Redis.

    Args:
        req (str): request body
    """
    payload = json.loads(req)

    if ('user_id' not in payload or 'start' not in payload
            or 'stop' not in payload):
        msg = '''Make sure the input has `user_id`, `start`, `stop`'''
        ret = json.dumps({"status": "MissingFieldError", "message": msg})
        sys.exit(ret)

    start = int(payload['start'])
    stop = int(payload['stop'])
    user_id = str(payload['user_id'])

    # Read user_id's post_ids from Redis
    redis_server = os.getenv('REDIS_SERVER')
    redis_port = os.getenv('REDIS_PORT')
    try:
        r = redis.Redis(host=redis_server, port=redis_port)
    except:
        ret = {
            "status":
            "UserTimelineServiceReadUserTimelineError",
            "errors": [{
                "message": "Failed to connect to Redis",
                "exception": str(sys.exc_info()[1]),
                "traceback": traceback.format_exc()
            }]
        }
        sys.exit(dumps(ret))

    try:
        ret = r.zrevrange(user_id, start, stop - 1)
    except:
        ret = {
            "status":
            "UserTimelineServiceReadUserTimelineError",
            "errors": [{
                "message": "Failed to read post_ids from Redis",
                "exception": str(sys.exc_info()[1]),
                "traceback": traceback.format_exc()
            }]
        }
        sys.exit(dumps(ret))

    post_ids = [u.decode("utf-8") for u in ret]

    # call post-storage-service-read-posts to get post documents
    function_url = "http://gateway.openfaas:8080/function/post-storage-service-read-posts"
    ret = ds_util.invoke(function_url, {"post_ids": post_ids})

    if ret["http_status_code"] != 200:
        sys.exit(
            dumps({
                "status": "UserTimelineServiceReadUserTimelineError",
                "errors": [ret]
            }))

    return dumps({
        "status": "success",
        "post_ids": post_ids,
        "posts": ret["posts"]
    })
Exemple #18
0
def compose_post_service_upload_user_mentions(req_id, user_mentions):
    req = {"req_id": req_id, "user_mentions": user_mentions}
    function_url = "http://gateway.openfaas:8080/function/compose-post-service-upload-user-mentions"

    return ds_util.invoke(function_url, req)