python类HTTPBadRequest()的实例源码

scheduler.py 文件源码 项目:FogLAMP 作者: foglamp 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _extract_args(data, curr_value):
    try:
        if 'type' in data and (not isinstance(data['type'], int) and not data['type'].isdigit()):
            raise ValueError('Error in type: {}'.format(data['type']))

        if 'day' in data and (not isinstance(data['day'], int) and not data['day'].isdigit()):
            raise ValueError('Error in day: {}'.format(data['day']))

        if 'time' in data and (not isinstance(data['time'], int) and not data['time'].isdigit()):
            raise ValueError('Error in time: {}'.format(data['time']))

        if 'repeat' in data and (not isinstance(data['repeat'], int) and not data['repeat'].isdigit()):
            raise ValueError('Error in repeat: {}'.format(data['repeat']))

        _schedule = dict()

        _schedule['schedule_id'] = curr_value['schedule_id'] if curr_value else None

        s_type = data.get('type') if 'type' in data else curr_value['schedule_type'] if curr_value else 0
        _schedule['schedule_type'] = int(s_type)

        s_day = data.get('day') if 'day' in data else curr_value['schedule_day'] if curr_value and curr_value['schedule_day'] else 0
        _schedule['schedule_day'] = int(s_day)

        s_time = data.get('time') if 'time' in data else curr_value['schedule_time'] if curr_value and curr_value['schedule_time'] else 0
        _schedule['schedule_time'] = int(s_time)

        s_repeat = data.get('repeat') if 'repeat' in data else curr_value['schedule_repeat'] if curr_value and curr_value['schedule_repeat']else 0
        _schedule['schedule_repeat'] = int(s_repeat)

        _schedule['schedule_name'] = data.get('name') if 'name' in data else curr_value['schedule_name'] if curr_value else None

        _schedule['schedule_process_name'] = data.get('process_name') if 'process_name' in data else curr_value['schedule_process_name'] if curr_value else None

        _schedule['schedule_exclusive'] = data.get('exclusive') if 'exclusive' in data else curr_value['schedule_exclusive'] if curr_value else 'True'
        _schedule['schedule_exclusive'] = 'True' if _schedule['schedule_exclusive'] else 'False'
    except ValueError as ex:
        raise web.HTTPBadRequest(reason=str(ex))

    return _schedule
scheduler.py 文件源码 项目:FogLAMP 作者: foglamp 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def get_schedule(request):
    """
    Return the information for the given schedule from schedules table

    :Example: curl -X GET  http://localhost:8082/foglamp/schedule/ac6dd55d-f55d-44f7-8741-984604bf2384
    """

    try:
        schedule_id = request.match_info.get('schedule_id', None)

        if not schedule_id:
            raise web.HTTPBadRequest(reason='Schedule ID is required.')

        try:
            assert uuid.UUID(schedule_id)
        except ValueError as ex:
            raise web.HTTPNotFound(reason="Invalid Schedule ID {}".format(schedule_id))

        sch = await server.Server.scheduler.get_schedule(uuid.UUID(schedule_id))

        schedule = {
            'id': str(sch.schedule_id),
            'name': sch.name,
            'process_name': sch.process_name,
            'type': Schedule.Type(int(sch.schedule_type)).name,
            'repeat': sch.repeat.total_seconds() if sch.repeat else 0,
            'time': (sch.time.hour * 60 * 60 + sch.time.minute * 60 + sch.time.second) if sch.time else 0,
            'day': sch.day,
            'exclusive': sch.exclusive
        }

        return web.json_response(schedule)
    except (ValueError, ScheduleNotFoundError) as ex:
        raise web.HTTPNotFound(reason=str(ex))
scheduler.py 文件源码 项目:FogLAMP 作者: foglamp 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def start_schedule(request):
    """
    Starts a given schedule

    :Example: curl -X POST  http://localhost:8082/foglamp/schedule/start/fd439e5b-86ba-499a-86d3-34a6e5754b5a
    """

    try:
        schedule_id = request.match_info.get('schedule_id', None)

        if not schedule_id:
            raise web.HTTPBadRequest(reason='Schedule ID is required.')

        try:
            assert uuid.UUID(schedule_id)
        except ValueError as ex:
            raise web.HTTPNotFound(reason="Invalid Schedule ID {}".format(schedule_id))

        sch = await server.Server.scheduler.get_schedule(uuid.UUID(schedule_id))

        # Start schedule
        await server.Server.scheduler.queue_task(uuid.UUID(schedule_id))

        return web.json_response({'id': schedule_id, 'message': 'Schedule started successfully'})
    except (ValueError, ScheduleNotFoundError) as ex:
        raise web.HTTPNotFound(reason=str(ex))
scheduler.py 文件源码 项目:FogLAMP 作者: foglamp 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def get_task(request):
    """
    Returns a task

    :Example: curl -X GET  http://localhost:8082/foglamp/task/{task_id}?name=xxx&state=xxx
    """

    try:
        task_id = request.match_info.get('task_id', None)

        if not task_id:
            raise web.HTTPBadRequest(reason='Task ID is required.')

        try:
            assert uuid.UUID(task_id)
        except ValueError as ex:
            raise web.HTTPNotFound(reason="Invalid Task ID {}".format(task_id))

        tsk = await server.Server.scheduler.get_task(task_id)

        task = {
            'id': str(tsk.task_id),
            'process_name': tsk.process_name,
            'state': Task.State(int(tsk.state)).name,
            'start_time': str(tsk.start_time),
            'end_time': str(tsk.end_time),
            'exit_code': tsk.exit_code,
            'reason': tsk.reason
        }

        return web.json_response(task)
    except (ValueError, TaskNotFoundError) as ex:
        raise web.HTTPNotFound(reason=str(ex))
service_registry.py 文件源码 项目:FogLAMP 作者: foglamp 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def get_service(request):
    """ Returns a list of all services or of the selected service

    :Example: curl -X GET  http://localhost:8082/foglamp/service
    :Example: curl -X GET  http://localhost:8082/foglamp/service?name=X&type=Storage
    """
    service_name = request.query['name'] if 'name' in request.query else None
    service_type = request.query['type'] if 'type' in request.query else None

    try:
        if not service_name and not service_type:
            services_list = Service.Instances.all()
        elif service_name and not service_type:
            services_list = Service.Instances.get(name=service_name)
        elif not service_name and service_type:
            services_list = Service.Instances.get(s_type=service_type)
        else:
            services_list = Service.Instances.filter_by_name_and_type(
                    name=service_name, s_type=service_type
                )
    except Service.DoesNotExist as ex:
        raise web.HTTPBadRequest(reason="Invalid service name and/or type provided" + str(ex))

    services = []
    for service in services_list:
        svc = dict()
        svc["id"] = service._id
        svc["name"] = service._name
        svc["type"] = service._type
        svc["address"] = service._address
        svc["management_port"] = service._management_port
        svc["protocol"] = service._protocol
        svc["status"] =  service._status
        if service._port:
            svc["service_port"] = service._port
        services.append(svc)

    return web.json_response({"services": services})
methods.py 文件源码 项目:djaio 作者: Sberned 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def from_http(self, request):
        self.total = None
        self.success = None
        self.errors = []
        self.params = None
        self.output = None
        self.pagination = None
        self.limit = None
        self.offset = None

        if not isinstance(request, web.Request):
            raise web.HTTPBadRequest()

        self.meta = {
            'cookies': getattr(request, 'cookies', {}),
            'headers': getattr(request, 'headers', {})
        }
        req_params = {}
        # if GET or DELETE we read a query params
        if request.method in (METH_GET, METH_DELETE):
            req_params = self.process_request(request.GET)
        # else we read a POST-data
        elif request.method in (METH_PUT, METH_POST):
            try:
                req_params = self.process_request(await request.json())
            except (ValueError, TypeError):
                req_params = self.process_request(await request.post())

        # Here we add or override params by PATH-params.
        # If it exist
        if request.match_info:
            req_params.update(request.match_info.copy())

        self.limit = get_int_or_none(request.headers.get('X-Limit')) or \
                     get_int_or_none(req_params.pop('limit', None)) or \
                     get_int_or_none(request.app.settings.LIMIT)
        self.offset = get_int_or_none(request.headers.get('X-Offset')) or \
                      get_int_or_none(req_params.pop('offset', None)) or \
                      get_int_or_none(request.app.settings.OFFSET)

        self.params = self.validate_params(req_params)
        self.result = []
        self.app = request.app
        self.settings = request.app.settings
coroweb.py 文件源码 项目:awesome-python3-webapp 作者: syusonn 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __call__(self,request):
        kw = None
        if self._has_var_kw_arg or self._has_named_kw_args or self._required_kw_args:
            if request.method == 'POST':
                if not request.content_type:
                    return web.HTTPBadRequest(text='Missing content_type')
                ct = request.content_type.lower()
                if ct.startswith('application/json'):
                    params = await request.json()
                    if not isinstance(params,dict):
                        return web.HTTPBadRequest(text='Json body mmust be object')
                    kw = params
                elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
                    params = await request.post()
                    kw = dict(**params)
                else:
                    return web.HTTPBadRequest(text = 'Unsupported content_type:%s' % request.content_type)
            if request.method == 'GET':
                qs = request.query_string
                if qs:
                    kw = dict()
                    for k,v in parse.parse_qs(qs,True).items():
                        kw[k] = v[0]
        if kw is None:
            kw = dict(**request.match_info)
        else:
            # ????????????????request????????????????
            if not self._has_var_kw_arg and  self._named_kw_args:
                #remove all unamed kw
                copy = dict()
                for name in self._named_kw_args:
                    if name in kw:
                        copy[name] = kw[name]
                kw = copy
            #check named arg
            for k,v in request.match_info.items():
                if k in kw:
                    logging.warning('Duplicate arg name in named arg and kw args:%s' % k)
                kw[k] = v
        if self._has_request_arg:
            kw['request'] = request
        #check required kw
        if self._required_kw_args:
            for name in self._required_kw_args:
                if not name in kw:
                    return web.HTTPBadRequest(text='Missing argument : %s' % name)
        logging.info('call with args:%s' % str(kw))
        try:
            r = await self._func(**kw)
            return r
        except APIError as e:
            raise dict(error=e.error,data=e.data,message=e.message)

#??????????
coreweb.py 文件源码 项目:python-awesome-web 作者: tianzhenyun 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __call__(self, request):
        kw = None
        if self._has_var_kw_arg or self._has_named_kw_args or self._requested_kw_args:
            if request.method == 'POST':
                if not request.content_type:
                    return web.HTTPBadRequest('Missing Content-Type')
                ct = request.content_type.lower()
                if ct.startswith('application/json'):
                    params = yield from request.json()
                    if not isinstance(params, dict):
                        return web.HTTPBadRequest('JSON body must be object.')
                    kw = params
                elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
                    params = yield from request.post()
                    kw = dict(**params)
                else:
                    return web.HTTPBadRequest('Unsupported Content-Type: {}'.format(request.content_type))
            if request.method == 'GET':
                qs = request.query_string
                if qs:
                    kw = dict()
                    for k, v in parse.parse_qs(qs, True).items():
                        kw[k] = v[0]

        if kw is None:
            kw = dict(**request.match_info)
        else:
            if not self._has_var_kw_arg and self._named_kw_args:
                #remove all unamed kw:
                copy = dict()
                for name in self._named_kw_args:
                    if name in kw:
                        copy[name] = kw[name]
                kw = copy
            #check named arg:
            for k, v in request.match_info.items():
                if k in kw:
                    logging.warning('Duplicate arg name in named arg and args:{}'.format(k))
                kw[k] = v

        if self._has_request_arg:
            kw['request'] = request

        #check required kw:
        if self._requested_kw_args:
            for name in self._requested_kw_args:
                if not name in kw:
                    return web.HTTPBadRequest('Missing argument:{}'.format(name))
        logging.info('call with args: {}'.format(str(kw)))
        try:
            r = yield from self._func(**kw)
            return r
        except APIError as e:
            return dict(error = e.error, data = e.data, message = e.message)
__init__.py 文件源码 项目:aiohttp_validate 作者: dchaplinsky 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _validate_data(data, schema, validator_cls):
    """
    Validate the dict against given schema (using given validator class).
    """
    validator = validator_cls(schema)
    _errors = defaultdict(list)
    for err in validator.iter_errors(data):
        path = err.schema_path

        # Code courtesy: Ruslan Karalkin
        # Looking in error schema path for
        # property that failed validation
        # Schema example:
        # {
        #    "type": "object",
        #    "properties": {
        #        "foo": {"type": "number"},
        #        "bar": {"type": "string"}
        #     }
        #    "required": ["foo", "bar"]
        # }
        #
        # Related err.schema_path examples:
        # ['required'],
        # ['properties', 'foo', 'type']

        if "properties" in path:
            path.remove("properties")
        key = path.popleft()

        # If validation failed by missing property,
        # then parse err.message to find property name
        # as it always first word enclosed in quotes
        if key == "required":
            key = err.message.split("'")[1]

        _errors[key].append(str(err))

    if _errors:
        _raise_exception(
            web.HTTPBadRequest,
            "Request is invalid; There are validation errors.",
            _errors)
handlers.py 文件源码 项目:aiohttp_cas 作者: bard-it 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def login_handler(request):
    """Handles login requests.

    We get the ticket ID from the user, the rest comes from info stored on
    the session.
    """
    ticket = request.GET.get('ticket')

    session = await get_session(request)
    redir = session[SESSION_KEY].get('redir')
    login_route = request.app[APP_KEY]['LOGIN_ROUTE']
    root_url = request.app[APP_KEY]['ROOT_URL']
    on_success = request.app[APP_KEY]['ON_SUCCESS']
    version = request.app[APP_KEY]['VERSION']

    # If we're missing neccessary data, return 400 Bad Request
    if not (request.scheme and request.host):
        log.warn("Invalid scheme ({}) or host ({})"
                 .format(request.scheme, request.host))
        return web.HTTPBadRequest()

    # Build the service URL.
    service = parse.urlunsplit(
            (request.scheme, request.host,
             login_route, None, None)
            )

    if ticket:
        # Validate the ticket.
        attrs = await validate(ticket, service, root_url, version)
        # If it succeeds, add the returned attributes to the session.
        if attrs:
            log.info("Authentication suceeded for ticket ID {}".format(ticket))
            session[SESSION_KEY] = attrs
            # Go to the requested redirect or, failing that,
            # the default "on_success" url
            return web.HTTPFound(redir or on_success)
        else:
            # THEY SHALL NOT PASS
            log.info("Authentication fail for ticket ID {}".format(ticket))
            return web.HTTPUnauthorized()
    # If we don't get a ticket (or if something else happens), redirect
    # to the CAS service login.
    return web.HTTPFound(cas_url('login', root_url, service=service))
handlers.py 文件源码 项目:cockatiel 作者: raphaelm 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def put_file(request: web.Request):
    checksum = hashlib.sha1()

    with tempfile.SpooledTemporaryFile(max_size=1024 * 1024) as tmpfile:
        try:
            while True:
                chunk = yield from request._payload.read(1024)
                if chunk is streams.EOF_MARKER:
                    break
                if isinstance(chunk, asyncio.Future):
                    chunk = yield from asyncio.wait_for(chunk, timeout=60)
                if chunk:
                    checksum.update(chunk)
                    tmpfile.write(chunk)
        except asyncio.TimeoutError:
            raise web.HTTPRequestTimeout()

        calculated_hash = checksum.hexdigest()
        if 'X-Content-SHA1' in request.headers:
            client_hash = request.headers['X-Content-SHA1'].lower()
            if calculated_hash != client_hash:
                logger.warn('SHA1 hash mismatch: %s != %s' % (calculated_hash, client_hash))
                raise web.HTTPBadRequest(text='SHA1 hash does not match')

        name = request.match_info.get('name').strip()

        if name in replication.dellog:
            # We know this is already deleted
            raise web.HTTPConflict(text='This file has already been deleted in the cluster.')

        is_replication = request.headers['User-Agent'].startswith('cockatiel/')
        filename = generate_filename(name, calculated_hash,
                                     get_timestamp_from_name(name) if is_replication else str(int(time.time())))
        filepath = os.path.join(config.args.storage, filename)

        if not os.path.exists(filepath):
            directory, _ = os.path.split(filepath)
            os.makedirs(directory, exist_ok=True)

            tmpfile.seek(0)
            with open(filepath, 'wb') as f:
                for chunk in chunks(tmpfile):
                    f.write(chunk)

            logger.debug('Created file {}, scheduling replication.'.format(filename))
            replication.queue_operation('PUT', filename)
            return web.Response(status=201, headers={
                'Location': '/' + filename
            })
        else:
            logger.debug('File {} already existed.'.format(filename))
            return web.Response(status=302, headers={
                'Location': '/' + filename
            })
views.py 文件源码 项目:chromewhip 作者: chuckus 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _go(request: web.Request):

    js_profiles = request.app['js-profiles']
    c = request.app['chrome-driver']

    url = request.query.get('url')
    if not url:
        return web.HTTPBadRequest(reason='no url query param provided')  # TODO: match splash reply

    wait_s = float(request.query.get('wait', 0))

    raw_viewport = request.query.get('viewport', '1024x768')
    parts = raw_viewport.split('x')
    width = int(parts[0])
    height = int(parts[1])

    js_profile_name = request.query.get('js', None)
    if js_profile_name:
        profile = js_profiles.get(js_profile_name)
        if not profile:
            return web.HTTPBadRequest(reason='profile name is incorrect')  # TODO: match splash

    # TODO: potentially validate and verify js source for errors and security concerrns
    js_source = request.query.get('js_source', None)

    await c.connect()
    tab = c.tabs[0]
    cmd = page.Page.setDeviceMetricsOverride(width=width,
                                             height=height,
                                             deviceScaleFactor=0.0,
                                             mobile=False)
    await tab.send_command(cmd)
    await tab.enable_page_events()
    await tab.go(url)
    await asyncio.sleep(wait_s)
    if js_profile_name:
        await tab.evaluate(js_profiles[js_profile_name])

    if js_source:
        await tab.evaluate(js_source)

    return tab
web.py 文件源码 项目:fireq 作者: superdesk 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def auth_middleware(app, handler):
    """ Login via Github """
    def gh_client(**kw):
        return GithubClient(conf['github_id'], conf['github_secret'], **kw)

    async def callback(request):
        session = await get_session(request)
        log.debug('callback: session=%s GET=%s', session, request.GET)
        if session.get('github_state') != request.GET.get('state'):
            return web.HTTPBadRequest()
        code = request.GET.get('code')
        if not code:
            return web.HTTPBadRequest()

        gh = gh_client()
        token, _ = await gh.get_access_token(code)
        gh = gh_client(access_token=token)
        req = await gh.request('GET', 'user')
        user = await req.json()
        req.close()
        users = []
        for org in conf['github_orgs']:
            _, resp = await gh_api('orgs/%s/members?per_page=100' % org)
            users.extend(u['login'] for u in resp)
        log.debug('members %s: %s', len(users), users)
        if user.get('login') in users:
            session['login'] = user.get('login')
            session.pop('github_state', None)
            session.pop('github_url', None)
            location = session.pop('location')
            return web.HTTPFound(location)
        return web.HTTPForbidden()

    async def check_auth(request):
        session = await get_session(request)
        login = session.get('login')
        if login:
            request['login'] = login
            return await handler(request)
        elif 'github_state' not in session:
            gh = gh_client()
            state = str(uuid.uuid4())
            url = gh.get_authorize_url(scope='', state=state)
            session['github_state'] = state
            session['github_url'] = url
            session['location'] = request.path
            log.debug('check_auth: %s', session)
        return web.HTTPFound(conf['url_prefix'] + '/login')

    async def inner(request):
        if request.path == (conf['url_prefix'] + conf['github_callback']):
            return await callback(request)
        elif request.path == (conf['url_prefix'] + '/hook'):
            return await handler(request)
        elif request.path == (conf['url_prefix'] + '/login'):
            return await handler(request)
        else:
            return await check_auth(request)

    return inner
webframe.py 文件源码 项目:Python_Blog 作者: qqj1228 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def __call__(self, request):
        kw = None
        if self._has_var_kw_arg or self._has_named_kw_arg or self._required_kw_args:
            if request.method == 'POST':
                if not request.content_type:
                    return web.HTTPBadRequest('Missing Content-Type.')
                ct = request.content_type.lower()
                if ct.startswith('application/json'):
                    params = await request.json()
                    if not isinstance(params, dict):
                        return web.HTTPBadRequest('JSON body must be object.')
                    kw = params
                elif ct.startswith('application/x-www-form-urlencoded') or \
                        ct.startswith('multipart/form-data'):
                    params = await request.post()
                    kw = dict(**params)
                else:
                    return web.HTTPBadRequest('Unsupported Content-Type: %s' % request.content_type)
            if request.method == 'GET':
                qs = request.query_string
                if qs:
                    kw = dict()
                    for k, v in parse.parse_qs(qs, True).items():
                        kw[k] = v[0]
        if kw is None:
            kw = dict(**request.match_info)
        else:
            if not self._has_var_kw_arg and self._named_kw_args:
                # remove all unnamed kw
                copy = dict()
                for name in self._named_kw_args:
                    if name in kw:
                        copy[name] = kw[name]
                kw = copy
            # check named arg
            for k, v in request.match_info.items():
                if k in kw:
                    logging.warning('Duplicate arg name in named arg and kw args: %s' % k)
                kw[k] = v
        if self._has_request_arg:
            kw['request'] = request
        # check required kw
        if self._required_kw_args:
            for name in self._required_kw_args:
                if name not in kw:
                    return web.HTTPBadRequest('Missing argument: %s' % name)
        logging.info('call with args: %s' % str(kw))

        try:
            r = await self._func(**kw)
            return r
        except APIError as e:
            return dict(error=e.error, data=e.data, message=e.message)
coroweb.py 文件源码 项目:xs-python-architecture 作者: xsingHu 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __call__(self, request):
        kw = None
        if self._has_var_kw_arg or self._has_named_kw_args or self._required_kw_args:
            if request.method == 'POST':
                if not request.content_type:
                    return web.HTTPBadRequest('Missing Content-Type.')
                ct = request.content_type.lower()
                if ct.startswith('application/json'):
                    params = yield from request.json()
                    if not isinstance(params, dict):
                        return web.HTTPBadRequest('JSON body must be object.')
                    kw = params
                elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
                    params = yield from request.post()
                    kw = dict(**params)
                else:
                    return web.HTTPBadRequest('Unsupported Content-Type: %s' % request.content_type)
            if request.method == 'GET':
                qs = request.query_string
                if qs:
                    kw = dict()
                    for k, v in parse.parse_qs(qs, True).items():
                        kw[k] = v[0]
        if kw is None:
            kw = dict(**request.match_info)
        else:
            if not self._has_var_kw_arg and self._named_kw_args:
                # remove all unamed kw:
                copy = dict()
                for name in self._named_kw_args:
                    if name in kw:
                        copy[name] = kw[name]
                kw = copy
            # check named arg:
            for k, v in request.match_info.items():
                if k in kw:
                    logging.warning('Duplicate arg name in named arg and kw args: %s' % k)
                kw[k] = v
        if self._has_request_arg:
            kw['request'] = request
        # check required kw:
        if self._required_kw_args:
            for name in self._required_kw_args:
                if not name in kw:
                    return web.HTTPBadRequest('Missing argument: %s' % name)
        logging.info('call with args: %s' % str(kw))
        try:
            r = yield from self._func(**kw)
            return r
        except APIError as e:
            return dict(error=e.error, data=e.data, message=e.message)
coroweb.py 文件源码 项目:awesome-webapp 作者: TsangTen 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __call__(self, request):
        kw = None
        if self._has_var_kw_arg or self._has_named_kw_args or self._required_kw_args:
            if request.method == 'POST':
                if not request.content_type:
                    return web.HTTPBadRequest('Missing Content Type.')
                ct = request.content_type.lower()
                if ct.startswith('application/json'):
                    params = await request.json()
                    if not isinstance(params, dict):
                        return web.HTTPBadRequest('JSON body must be object.')
                    kw = params
                elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
                    params = await request.post()
                    kw = dict(**params)
                else:
                    return web.HTTPBadRequest('Unsupported Content-Type: %s' % request.content_type)
            if request.method == 'GET':
                qs = request.query_string
                if qs:
                    kw = dict()
                    for k, v in parse.parse_qs(qs, True).items():
                        kw[k] = v[0]
        if kw is None:
            kw = dict(**request.match_info)
        else:
            if not self._has_var_kw_arg and self._named_kw_args:
                # remove all unamed kw:
                copy = dict()
                for name in self._named_kw_args:
                    if name in kw:
                        copy[name] = kw[name]
                kw = copy
            # check named arg:
            for k, v in request.match_info.items():
                if k in kw:
                    logging.warning('Duplicate arg name in named arg and kw args: %s' % k)
                kw[k] = v
        if self._has_request_arg:
            kw['request'] = request
        # check required kw:
        if self._required_kw_args:
            for name in self._required_kw_args:
                if not name in kw:
                    return web.HTTPBadRequest('Missing argument: %s' % name)
        logging.info('call with args: %s' % str(kw))
        try:
            r = await self._func(**kw)
            return r
        except APIError as e:
            return dict(error=e.error, data=e.data, message=e.message)
RequestHandler.py 文件源码 项目:jupiter 作者: dianbaer 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __call__(self, request):
        kw = None
        if self._has_var_kw_arg or self._has_named_kw_args:
            if request.method == 'POST':
                if not request.content_type:
                    return web.HTTPBadRequest('Missing Content-Type.')
                ct = request.content_type.lower()
                if ct.startswith('application/json'):
                    params = await request.json()
                    if not isinstance(params, dict):
                        return web.HTTPBadRequest('JSON body must be object.')
                    kw = params
                elif ct.startswith('multipart/form-data'):
                    params = await request.post()
                    kwAndFile = dict(**params)
                    if kwAndFile.get('packet') is None:
                        return web.HTTPBadRequest('packet is None')
                    packet = unquote(kwAndFile.get('packet'))
                    kw = json.loads(packet)
                    kwAndFile.pop('packet')
                    kw['file'] = kwAndFile
                else:
                    return web.HTTPBadRequest('Unsupported Content-Type: %s' % request.content_type)
            if request.method == 'GET':
                qs = request.query_string
                if qs:
                    kw = dict()
                    for k, v in parse.parse_qs(qs, True).items():
                        kw[k] = v[0]
        if kw is None:
            kw = dict(**request.match_info)
        else:
            if not self._has_var_kw_arg and self._has_named_kw_args:
                copy = dict()
                for name in self._named_kw_args:
                    if name in kw:
                        copy[name] = kw[name]
                kw = copy
        if self._has_request_arg:
            kw['request'] = request
        if self._required_kw_args:
            for name in self._required_kw_args:
                if not name in kw:
                    return web.HTTPBadRequest('Missing argument: %s' % name)
        logging.info('call with args: %s' % str(kw))
        logging.info('%s RequestHandlerC call start next handler %s ' % (request.__uuid__, self._fn))
        r = await self._fn(**kw)
        logging.info('%s RequestHandlerC call end ' % (request.__uuid__))
        return r
scheduler.py 文件源码 项目:FogLAMP 作者: foglamp 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def update_schedule(request):
    """
    Update a schedule in schedules table

    :Example: curl -d '{"type": 4, "name": "sleep30 updated", "process_name": "sleep30", "repeat": "15"}'  -X PUT  http://localhost:8082/foglamp/schedule/84fe4ea1-df9c-4c87-bb78-cab2e7d5d2cc
    """

    try:
        data = await request.json()
        schedule_id = request.match_info.get('schedule_id', None)

        if not schedule_id:
            raise web.HTTPBadRequest(reason='Schedule ID is required.')

        try:
            assert uuid.UUID(schedule_id)
        except ValueError as ex:
            raise web.HTTPNotFound(reason="Invalid Schedule ID {}".format(schedule_id))

        sch = await server.Server.scheduler.get_schedule(uuid.UUID(schedule_id))
        if not sch:
            raise ValueError('No such Schedule: {}.'.format(schedule_id))

        curr_value = dict()
        curr_value['schedule_id'] = sch.schedule_id
        curr_value['schedule_process_name'] = sch.process_name
        curr_value['schedule_name'] = sch.name
        curr_value['schedule_type'] = sch.schedule_type
        curr_value['schedule_repeat'] = sch.repeat.total_seconds() if sch.repeat else 0
        curr_value['schedule_time'] = (sch.time.hour * 60 * 60 + sch.time.minute * 60 + sch.time.second) if sch.time else 0
        curr_value['schedule_day'] = sch.day
        curr_value['schedule_exclusive'] = sch.exclusive

        go_no_go = await _check_schedule_post_parameters(data, curr_value)
        if len(go_no_go) != 0:
            raise ValueError("Errors in request: {}".format(','.join(go_no_go)))

        updated_schedule_id = await _execute_add_update_schedule(data, curr_value)

        sch = await server.Server.scheduler.get_schedule(updated_schedule_id)

        schedule = {
            'id': str(sch.schedule_id),
            'name': sch.name,
            'process_name': sch.process_name,
            'type': Schedule.Type(int(sch.schedule_type)).name,
            'repeat': sch.repeat.total_seconds() if sch.repeat else 0,
            'time': (sch.time.hour * 60 * 60 + sch.time.minute * 60 + sch.time.second) if sch.time else 0,
            'day': sch.day,
            'exclusive': sch.exclusive
        }

        return web.json_response({'schedule': schedule})
    except (ValueError, ScheduleNotFoundError) as ex:
        raise web.HTTPNotFound(reason=str(ex))
scheduler.py 文件源码 项目:FogLAMP 作者: foglamp 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def get_tasks(request):
    """
    Returns the list of tasks

    :Example: curl -X GET  http://localhost:8082/foglamp/task
    :Example: curl -X GET  http://localhost:8082/foglamp/task?name=xxx
    :Example: curl -X GET  http://localhost:8082/foglamp/task?state=xxx
    :Example: curl -X GET  http://localhost:8082/foglamp/task?name=xxx&state=xxx
    """

    try:
        limit = request.query.get('limit') if 'limit' in request.query else '100'
        if limit:
            if not re.match("(^[0-9]*$)", limit):
                raise web.HTTPBadRequest(reason='This limit {} not permitted.'.format(limit))
            elif int(limit) > 100:
                limit = 100
            else:
                limit = int(limit)

        state = request.query.get('state') if 'state' in request.query else None
        if state:
            if state.upper() not in [t.name for t in list(Task.State)]:
                raise web.HTTPBadRequest(reason='This state value {} not permitted.'.format(state))
            else:
                z = dict()
                for i in list(Task.State):
                    z.update({i.name: i.value})
                state = z[state.upper()]

        name = request.query.get('name') if 'name' in request.query else None

        where_clause = None
        if name and state:
            where_clause = (["process_name", "=", name], ["state", "=", state])
        elif name:
            where_clause = ["process_name", "=", name]
        elif state:
            where_clause = ["state", "=", state]

        tasks = await server.Server.scheduler.get_tasks(where=where_clause, limit=limit)

        if len(tasks) == 0:
            raise TaskNotFoundError("No Tasks")

        new_tasks = []
        for task in tasks:
            new_tasks.append(
                {'id': str(task.task_id),
                     'process_name': task.process_name,
                     'state': Task.State(int(task.state)).name,
                     'start_time': str(task.start_time),
                     'end_time': str(task.end_time),
                     'exit_code': task.exit_code,
                     'reason': task.reason
                 }
            )

        return web.json_response({'tasks': new_tasks})
    except (ValueError, TaskNotFoundError) as ex:
        raise web.HTTPNotFound(reason=str(ex))


问题


面经


文章

微信
公众号

扫码关注公众号