async def get_timezone(request: Request): area, city = request['validated_args']['area'], request['validated_args'][ 'city'] result = await TimezoneApiClient(timeout=30).get_time(area=area, city=city) if not result: return http_bad_request( f'timezone with area={area} and city={city} not found') return http_ok(GetTimezoneResultSchema().dump(result))
async def ping(request: Request): try: async with request.app.db_engine.acquire() as conn: await conn.execute('SELECT 1;') return http_ok() except Exception: logger.exception('ping db fail') return http_bad_request(msg='База недоступна')
async def update_student_by_id(request: Request, student_id): data = request['validated_json'] async with request.app.db_engine.acquire() as conn: result = await Student.update_by_id(conn=conn, student_id=student_id, values=data) if not result: return http_bad_request('student not found') return http_ok(StudentResultSchema().dump(result))
async def wrapper(*args, **kwargs): request = args[0] if isinstance(args[0], Request) else args[1] try: target_data = getattr(request, self._target) except InvalidUsage: return http_bad_request('data is not json') if not target_data and not self._empty: return _error_no_data(self._target) elif self._empty and target_data is None: target_data = {} schema = self._schema(partial=self._partial, many=self._many) try: validated_data = schema.load(target_data) except ValidationError as errors: return marshmallow_errors(errors.messages) request[f'validated_{self._target}'] = validated_data return await func(*args, **kwargs)
async def get_student_by_id(request: Request, student_id): async with request.app.db_engine.acquire() as conn: result = await Student.get_by_id(conn=conn, student_id=student_id) if not result: return http_bad_request('student not found') return http_ok(StudentResultSchema().dump(result))