def search():
    # only used for req/response logging in UI
    req_id = None
    response = Response()
    try:
        request_args = request.args

        if 'requestId' in request_args:
            req_id = int(request_args.get('requestId'))

        if 'product' not in request_args or request_args.get('product') == '':
            response.message = 'No search term provided.'
            return jsonify(attr.asdict(response)), 200

        fuzziness = None
        if 'fuzziness' in request_args:
            fuzziness = int(request_args.get('fuzziness'))

        product = request_args.get('product')
        response = service.product_service.search_products(product, fuzziness)
        response.request_id = req_id

        if response.error:
            return jsonify(attr.asdict(response)), 500

        return jsonify(attr.asdict(response)), 200
    except Exception as ex:
        response.message = 'Error attempting to search for products.'
        response.error = {
            'message': repr(ex),
            'stackTrace': traceback.format_exc()
        }
        response.requestId = req_id
        return jsonify(attr.asdict(response)), 500
Ejemplo n.º 2
0
def verify_user_session():
    # only used for req/response logging in UI
    req_id = None
    response = Response()
    try:
        request_content = request.get_json()

        if request_content and 'requestId' in request_content:
            req_id = int(request_content['requestId'])

        jwt = g.get('jwt', None)
        if not jwt['token']:
            if jwt['result'].authorized is not None and not jwt[
                    'result'].authorized:
                return jsonify(attr.asdict(jwt['result'])), 401

            return jsonify(attr.asdict(jwt['result'])), 400

        username = jwt['result'].data['username']
        response = service.user_service.get_user_from_session(
            username, jwt['token'])
        response.request_id = req_id

        if response.error:
            return jsonify(attr.asdict(response)), 500

        return jsonify(attr.asdict(response)), 200
    except Exception as ex:
        response.message = 'Error trying to verify user session.'
        response.error = {
            'message': repr(ex),
            'stackTrace': traceback.format_exc()
        }
        response.requestId = req_id
        return jsonify(attr.asdict(response)), 500
Ejemplo n.º 3
0
def register():
    # only used for req/response logging in UI
    req_id = None
    response = Response()
    try:
        request_content = request.get_json()

        if 'requestId' in request_content:
            req_id = int(request_content['requestId'])

        response = service.user_service.register(request_content)
        response.request_id = req_id

        if response.error:
            return jsonify(attr.asdict(response)), 500

        return jsonify(attr.asdict(response)), 200
    except Exception as ex:
        response.message = 'Error attempting to register user.'
        response.error = {
            'message': repr(ex),
            'stackTrace': traceback.format_exc()
        }
        response.requestId = req_id
        return jsonify(attr.asdict(response)), 500
Ejemplo n.º 4
0
def login():
    # only used for req/response logging in UI
    req_id = None
    response = Response()
    try:
        request_content = request.get_json()

        if 'requestId' in request_content:
            req_id = int(request_content['requestId'])

        if not ('username' in request_content
                and 'password' in request_content):
            response.message = 'No username and/or password provided.'
            response.requestId = req_id
            return jsonify(attr.asdict(response)), 400

        req = {
            'username': request_content['username'],
            'pw': request_content['password']
        }

        response = service.user_service.login(req)
        response.request_id = req_id

        if response.error:
            return jsonify(attr.asdict(response)), 500

        if response.data and response.authorized:
            return jsonify(attr.asdict(response)), 200

        return jsonify(attr.asdict(response)), 401

    except Exception as ex:
        response.message = 'Error attempting to login user.'
        response.error = {
            'message': repr(ex),
            'stackTrace': traceback.format_exc()
        }
        response.requestId = req_id
        return jsonify(attr.asdict(response)), 500