예제 #1
0
def create_user():
    table = resource_path_translator["Users"]
    context = log_and_extract_input(get_resource, table)
    param = context['body']
    # print(param)
    temp = {
        'id': str(uuid.uuid4()),
        'last_name': param['last_name'],
        'first_name': param['first_name'],
        'email': param['email'],
        'hashed_password': param['password'],
        'created_date': datetime.now()
    }
    # print(temp)
    r_table = dta.get_rdb_table(table, _db_name, connect_info=c_info)
    res = r_table.insert(temp)
    # print(res)
    if res == 1:
        location = {'Location': '/api/users/' + temp['id']}
        rsp = Response(json.dumps("user created success"),
                       status=201,
                       content_type="application/json",
                       headers=location)
    else:
        rsp = Response(json.dumps("user created fail"),
                       status=400,
                       content_type="application/json")
    return rsp
예제 #2
0
def user_login():
    inputs = log_and_extract_input(user_login)

    try:
        if inputs['method'] == 'POST':
            body = inputs['body']
            # Get the hashed password from database
            table = resource_path_translator["Users"]
            r_table = dta.get_rdb_table(table, _db_name, connect_info=c_info)
            template = {'email': body['email']}
            field_list = [
                'hashed_password', 'id', 'last_name', 'first_name', 'email'
            ]
            res = r_table.find_by_template(template=template,
                                           field_list=field_list)[0]
            pw_check = security.check_password(body['password'],
                                               res['hashed_password'])

            if pw_check:
                rsp_status = 201
                rsp_txt = 'CREATED'
                tok = security.generate_token(res)
            else:
                rsp_status = 401
                rsp_txt = 'NOT AUTHORIZED'
        elif inputs['method'] == 'GET':
            fields = inputs["query_params"]
            if fields['auth'] == 'google':
                return redirect(url_for('google_auth.login'))
            else:
                rsp_status = 401
                rsp_txt = 'NOT AUTHORIZED'
        else:
            rsp_txt = 'NOT IMPLEMENTED'
            rsp_status = 501

        if rsp_txt == 'CREATED':
            headers = {'Authorization': tok}
            full_rsp = Response(rsp_txt,
                                headers=headers,
                                status=rsp_status,
                                content_type='text/plain')
        else:
            full_rsp = Response(rsp_txt,
                                status=rsp_status,
                                content_type='text/plain')

    except Exception as e:
        logger.error('api/login: Exception=' + str(e))
        rsp_status = 500
        rsp_txt = 'INTERNAL SERVER ERROR'
        full_rsp = Response(rsp_txt,
                            status=rsp_status,
                            content_type='text/plain')

    return full_rsp
예제 #3
0
def index():
    try:
        if google_auth.is_logged_in():
            user_info = google_auth.get_user_info()
            r_table = dta.get_rdb_table(resource_path_translator["Users"],
                                        "userservice",
                                        connect_info=c_info)
            temp = {}
            temp['hashed_password'] = security.encode_pw(user_info['id'])
            field_list = ['hashed_password']
            res = r_table.find_by_template(template=temp,
                                           field_list=field_list)

            if len(res) != 0:
                rsp_status = 201
                rsp_txt = 'CREATED'
                tok = security.generate_token(res[0])
            else:
                self_user_info = {}
                self_user_info['password'] = user_info['id']
                self_user_info['last_name'] = user_info['family_name']
                self_user_info['first_name'] = user_info['given_name']
                self_user_info['email'] = user_info['email']
                return _register(self_user_info)

            if rsp_txt == 'CREATED':
                headers = {'Authorization': tok}
                rsp_txt += ". You're logged as " + user_info['given_name']
                full_rsp = Response(rsp_txt,
                                    headers=headers,
                                    status=rsp_status,
                                    content_type='text/plain')
            else:
                full_rsp = Response(rsp_txt,
                                    status=rsp_status,
                                    content_type='text/plain')
    except Exception as e:
        logger.error('api/login: Exception=' + str(e))
        rsp_status = 500
        rsp_txt = 'INTERNAL SERVER ERROR'
        full_rsp = Response(rsp_txt,
                            status=rsp_status,
                            content_type='text/plain')

    return full_rsp
예제 #4
0
def _register(user_info):
    hashed_pw = security.encode_pw(user_info['password'])
    user_info['hashed_password'] = hashed_pw
    del user_info['password']

    table = resource_path_translator["Users"]
    new_uuid = str(uuid.uuid4())
    print(new_uuid)
    temp = {
        'id': new_uuid,
        'last_name': user_info['last_name'],
        'first_name': user_info['first_name'],
        'email': user_info['email'],
        'hashed_password': user_info['hashed_password'],
        'created_date': datetime.now(),
        'status': 'PENDING'
    }
    # print(temp)
    r_table = dta.get_rdb_table(table, _db_name, connect_info=c_info)
    res = r_table.insert(temp)
    tok = security.generate_token(user_info)

    return new_uuid, tok
예제 #5
0
def create_address():

    user_info = security.check_authentication(request)

    table = resource_path_translator["Address"]
    context = log_and_extract_input(create_address, table)
    param = context['body']

    keys = {
        'auth_id': SMARTY_AUTH_ID,
        'auth_token': SMARTY_AUTH_TOKEN,
        'address_url': 'https://us-street.api.smartystreets.com/street-address'
    }
    # print(param)
    check_rsp = address.validate_address(param, keys)
    if check_rsp is None or check_rsp == 422:
        rsp = Response(json.dumps("invalid input"),
                       status=422,
                       content_type="application/json")
        return rsp
    if check_rsp == 500:
        rsp = Response(json.dumps("Internal server error"),
                       status=500,
                       content_type="application/json")
        return rsp
    try:
        temp = {
            'id':
            check_rsp['delivery_point_barcode'],
            "primary_number":
            check_rsp.get('primary_number', None),
            "street_predirection":
            check_rsp.get('street_predirection', None),
            "street_postdirection":
            check_rsp.get('street_postdirection', None),
            "street_name":
            check_rsp.get('street_name', None),
            "street_suffix":
            check_rsp.get('street_suffix', None),
            "secondary_number":
            check_rsp.get('secondary_number', None),
            "secondary_designator":
            check_rsp.get('primary_number', None),
            "extra_secondary_number":
            check_rsp.get('extra_secondary_number', None),
            "extra_secondary_designator":
            check_rsp.get('extra_secondary_designator', None),
            "city_name":
            check_rsp.get('city_name', None),
            "state_abbreviation":
            check_rsp.get('state_abbreviation', None),
            "zipcode":
            check_rsp.get('zipcode', None),
            "userid":
            user_info[2]['id']
        }
    except KeyError as ke:
        rsp = Response(json.dumps("Internal server error"),
                       status=500,
                       content_type="application/json")
        return rsp
        raise ke

    print(temp)
    r_table = dta.get_rdb_table(table, _db_name, connect_info=c_info)
    res = r_table.insert(temp)
    if res == 1:
        location = {'Location': '/api/address/' + temp['id']}
        rsp = Response(json.dumps("Created"),
                       status=201,
                       content_type="application/json",
                       headers=location)
    else:
        rsp = Response(json.dumps("Bad Request"),
                       status=400,
                       content_type="application/json")
    return rsp
예제 #6
0
def resource_by_id(resource, primary_key, dbname=_db_name):
    # print('resourcebyid')
    """

    :param dbname: Schema/database name.
    :param resource: Table name.
    :param primary_key: Primary key in the form "col1_col2_..._coln" with the values of key columns.
    :return: Result of operations.
    """

    result = None
    resource = resource_path_translator[resource]
    try:
        # Parse the incoming request into an application specific format.
        context = log_and_extract_input(resource_by_id,
                                        (dbname, resource, primary_key))
        # print(context)

        #
        # SOME CODE GOES HERE
        #
        # -- TO IMPLEMENT --

        if request.method == 'GET':

            #
            # SOME CODE GOES HERE
            #
            # -- TO IMPLEMENT --f
            fields = context.get("fields", None)
            r_table = dta.get_rdb_table(resource, dbname, connect_info=c_info)
            key = primary_key.split(_key_delimiter)
            res = r_table.find_by_primary_key(key, field_list=fields)

            rsp = Response(json.dumps(res, default=str),
                           status=200,
                           content_type="application/json")
            return rsp

        elif request.method == 'DELETE':
            #
            # SOME CODE GOES HERE
            #
            # -- TO IMPLEMENT --
            r_table = dta.get_rdb_table(resource, dbname, connect_info=c_info)
            key = primary_key.split(_key_delimiter)
            res = r_table.delete_by_key(key)

            rsp = Response(json.dumps(res, default=str),
                           status=200,
                           content_type="application/json")
            return rsp

        elif request.method == 'PUT':
            #
            # SOME CODE GOES HERE
            #
            # -- TO IMPLEMENT --
            temp = context['body']
            # print(temp,"This is temp")

            r_table = dta.get_rdb_table(resource, dbname, connect_info=c_info)
            key = primary_key.split(_key_delimiter)
            res = r_table.update_by_key(key, new_values=temp)

            rsp = Response(json.dumps(res, default=str),
                           status=200,
                           content_type="application/json")
            return rsp

    except Exception as e:
        print(e)
        return handle_error(e, result)
예제 #7
0
def get_resource(resource_name, dbname=_db_name):
    result = None
    resource_name = resource_path_translator[resource_name]
    try:
        context = log_and_extract_input(get_resource, resource_name)

        #
        # SOME CODE GOES HERE
        #
        # -- TO IMPLEMENT --
        if request.method == 'GET':
            #
            # SOME CODE GOES HERE
            #
            # -- TO IMPLEMENT --
            args = request.args
            # print(args)

            offset = args.get('offset') if 'offset' in args else 0
            limit = args.get('limit') if 'limit' in args else None

            fields = context.get("fields", None)

            # temp = context.get("body", None) if context.get("body", None) is not None else {}
            temp = {}
            for i in context['query_params']:
                if i != 'offset' and i != 'limit':
                    temp.update({i: context['query_params'][i]})

            r_table = dta.get_rdb_table(resource_name,
                                        dbname,
                                        connect_info=c_info)
            res = r_table.find_by_template(template=temp,
                                           field_list=fields,
                                           offset=offset,
                                           limit=limit)
            data = []

            for result in res:
                if resource_name == "user_table":
                    link = [{
                        "rel": "email",
                        "href": "/api/user?emails=" + result["email"],
                        "method": "GET"
                    }]
                    data.append({"data": result, "links": link})
                elif resource_name == "address_table":
                    link = [{
                        "rel": "user for this address",
                        "href": "/api/user?id=" + str(result["userid"]),
                        "method": "GET"
                    }]
                    data.append({"data": result, "links": link})
            if limit is None and offset == 0:
                rsp = Response(json.dumps({'data': data}, default=str),
                               status=200,
                               content_type="application/json")
            else:
                total = r_table.get_row_count()
                next_link = request.base_url + '?limit=' + str(limit) + (
                    '&offset=' + str(int(offset) + int(limit)) if int(offset) +
                    int(limit) < int(total) else '&offset=' + str(total))
                prev_link = request.base_url + '?limit=' + str(limit) + (
                    '&offset=' + str(int(offset) - int(limit))
                    if int(offset) - int(limit) >= 0 else '&offset=' + '0')
                rsp = Response(json.dumps(
                    {
                        'pagination': {
                            'offset': int(offset),
                            'limit': int(limit),
                            'total': total
                        },
                        'data': data,
                        'links': {
                            'next': next_link,
                            'prev': prev_link
                        }
                    },
                    default=str),
                               status=200,
                               content_type="application/json")

            return rsp

        elif request.method == 'POST':
            #
            # SOME CODE GOES HERE
            #
            # -- TO IMPLEMENT --
            # print((context['query_params']))
            temp = context['body']

            for i in context['query_params']:
                if i != 'field':
                    temp[i] = context['query_params'][i]
            # print(fields)
            r_table = dta.get_rdb_table(resource_name,
                                        dbname,
                                        connect_info=c_info)
            res = r_table.insert(new_record=temp)

            rsp = Response(json.dumps(res, default=str),
                           status=200,
                           content_type="application/json")
            return rsp
        else:
            result = "Invalid request."
            return result, 400, {'Content-Type': 'text/plain; charset=utf-8'}
    except Exception as e:
        print("Exception e = ", e)