async def get_handler(request: web.Request): try: if len(request.rel_url.query.keys()) == 0: services = await Service.get_all(DB.get(request, table)) return web.json_response({ 'data': DB.format_documents(Bson.to_json(services)), 'status_code': 200 }) else: services = [] if 'id' in request.rel_url.query: Validate.validate_object_id(request.rel_url.query.get('id')) service = await Service.get_by_id( request.rel_url.query.get('id'), DB.get(request, table)) if service is not None: services.append(service) elif 'state' in request.rel_url.query: services = await Service.get_by_state( request.rel_url.query.get('state'), DB.get(request, table)) elif 'secure' in request.rel_url.query: services = await Service.get_by_secure( bool(request.rel_url.query.get('secure')), DB.get(request, table)) return web.json_response({ 'data': DB.format_documents(Bson.to_json(services)), 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def retrieve_entry(request: web.Request): try: response = [] if 'rule_id' in request.rel_url.query: rule_id = request.rel_url.query.get('rule_id') Validate.validate_object_id(rule_id) response = await RateLimiter.get_entry_by_rule_id( rule_id, DB.get_redis(request)) elif 'host' in request.rel_url.query: host = request.rel_url.query.get('host') response = await RateLimiter.get_entry_by_host( host, DB.get_redis(request)) elif 'id' in request.rel_url.query: _id = request.rel_url.query.get('id') Validate.validate_object_id(_id) response = await RateLimiter.get_entry_by_id( _id, DB.get_redis(request)) else: response = await RateLimiter.get_all_entries(DB.get_redis(request)) return web.json_response({ 'data': response, 'status_code': 200 }, status=200) except Exception as err: return Error.handle(err)
async def get_handler(request: web.Request): try: admins = None if len(request.rel_url.query.keys()) == 0: admins = await Admin.get_all(DB.get(request, table)) else: admins = [] if 'id' in request.rel_url.query: admin_id = request.rel_url.query.get('id') Validate.validate_object_id(admin_id) admin = await Admin.get_by_id(admin_id, DB.get(request, table)) if admin is not None: admins.append(admin) elif 'email' in request.rel_url.query: admin_email = request.rel_url.query.get('email') admins = await Admin.get_by_email(admin_email, DB.get(request, table)) elif 'username' in request.rel_url.query: admin_username = request.rel_url.query.get('username') admins = await Admin.get_by_username(admin_username, DB.get(request, table)) return web.json_response({ 'data': DB.format_documents(Bson.to_json(admins)), 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def get_handler(request: web.Request): try: response = [] if 'service_id' in request.rel_url.query: Validate.validate_object_id(request.rel_url.query['service_id']) service_id = request.rel_url.query['service_id'] response = await RequestValidator.get_by_service_id( service_id, DB.get(request, table)) elif 'id' in request.rel_url.query: Validate.validate_object_id(request.rel_url.query.get('id')) req_validator = await RequestValidator.get_by_id( request.rel_url.query.get('id'), DB.get(request, table)) if req_validator is not None: response.append(req_validator) elif 'method' in request.rel_url.query: method = request.rel_url.query['method'] response = await RequestValidator.get_by_method( method, DB.get(request, table)) else: response = await RequestValidator.get_all(DB.get(request, table)) return web.json_response( { 'status_code': 200, 'data': Bson.to_json(response) }, status=200) except Exception as err: return Error.handle(err)
async def retrieve_rule(request: web.Request): try: # we want to identify the parameter which is used to identify the # records response = [] if 'status_code' in request.rel_url.query: status_code = request.rel_url.query.get('status_code') response = await RateLimiter.get_rule_by_status_code( status_code, DB.get_redis(request)) elif 'service_id' in request.rel_url.query: service_id = request.rel_url.query.get('service_id') response = await RateLimiter.get_rule_by_service_id( service_id, DB.get_redis(request)) elif 'id' in request.rel_url.query: _id = request.rel_url.query.get('id') Validate.validate_object_id(_id) rule = await RateLimiter.get_rule_by_id(_id, DB.get_redis(request)) if rule: response.append(rule) else: # fallback to get all if no param passed response = await RateLimiter.get_all_rules(DB.get_redis(request)) return web.json_response({ 'data': response, 'status_code': 200 }, status=200) except Exception as err: return Error.handle(err)
async def get_handler(request: web.Request): try: circuit_breakers = None if len(request.rel_url.query.keys()) == 0: circuit_breakers = await CircuitBreaker.get_all(DB.get(request, table)) else: circuit_breakers = [] if 'id' in request.rel_url.query: Validate.validate_object_id(request.rel_url.query.get('id')) circuit_breaker = await CircuitBreaker.get_by_id(request.rel_url.query.get('id'), DB.get(request, table)) if circuit_breaker is not None: circuit_breakers.append(circuit_breaker) elif 'service_id' in request.rel_url.query: Validate.validate_object_id( request.rel_url.query.get('service_id')) circuit_breakers = await CircuitBreaker.get_by_service_id(request.rel_url.query.get('service_id'), DB.get(request, table)) elif 'status_code' in request.rel_url.query: circuit_breakers = await CircuitBreaker.get_by_status_code(int(request.rel_url.query.get('status_code')), DB.get(request, table)) elif 'method' in request.rel_url.query: circuit_breakers = await CircuitBreaker.get_by_method(request.rel_url.query.get('method'), DB.get(request, table)) elif 'threshold' in request.rel_url.query: circuit_breakers = await CircuitBreaker.get_by_threshold(float(request.rel_url.query.get('threshold')), DB.get(request, table)) return web.json_response({ 'data': DB.format_documents(Bson.to_json(circuit_breakers)), 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def delete_handler(request: web.Request): try: Validate.validate_object_id(request.rel_url.query.get('id')) await Service.remove(request.rel_url.query.get('id'), DB.get(request, table)) return web.json_response({'message': 'service deleted'}) except Exception as err: return Error.handle(err)
def test_object_id(self, *args): try: with patch('bson.ObjectId.is_valid') as is_valid_mock: is_valid_mock.return_value = False mock_id = 'some-value' Validate.validate_object_id(mock_id) except Exception as err: expect(err.args[0]).to(be_a(object)) expect(err.args[0]).to(have_keys('message', 'status_code'))
async def delete_handler(request: web.Request): try: id = request.rel_url.query.get('id') Validate.validate_object_id(id) await RequestValidator.delete(id, DB.get(request, table)) return web.json_response({ 'message': 'request validator deleted', 'statusCode': 200 }) except Exception as err: return Error.handle(err)
async def delete_handler(request: web.Request): try: service_id = request.rel_url.query.get('id') Validate.validate_object_id(service_id) await Insights.remove(service_id, DB.get(request, table)) return web.json_response({ 'message': 'insight deleted', 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def delete_handler(request: web.Request): try: _id = request.rel_url.query.get('id') Validate.validate_object_id(_id) await EndpointCacher.delete(_id, DB.get_redis(request)) return web.json_response({ 'message': 'Endpoint cache deleted', 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def patch_handler(request: web.Request): try: ctx = json.loads(await request.text()) service_id = request.rel_url.query.get('id') Validate.validate_object_id(service_id) Validate.validate_schema(ctx, service_validator) await Service.update(service_id, ctx, DB.get(request, table)) return web.json_response({ 'message': 'service updated', }) except Exception as err: return Error.handle(err)
async def delete_rule(request: web.Request): try: # id to delete is from query params _id = request.rel_url.query.get('id') Validate.validate_object_id(_id) await RateLimiter.delete_rule(_id, DB.get_redis(request)) return web.json_response({ 'message': 'rate limiter rule deleted', 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def patch_handler(request: web.Request): try: ctx = json.loads(await request.text()) circuit_breaker_id = request.rel_url.query['id'] Validate.validate_schema(ctx, circuit_breaker_validator) Validate.validate_object_id(circuit_breaker_id) await CircuitBreaker.update(circuit_breaker_id, pydash.omit(ctx, 'id'), DB.get(request, table)) return web.json_response({ 'message': 'Circuit breaker updated', }) except Exception as err: return Error.handle(err)
async def patch_handler(request: web.Request): try: ctx = json.loads(await request.text()) admin_id = request.rel_url.query['id'] Validate.validate_schema(ctx, admin_validator) Validate.validate_object_id(admin_id) await Admin.update(admin_id, ctx, DB.get(request, table)) return web.json_response({ 'message': 'Admin updated', }) except Exception as err: return Error.handle(err)
async def update_handler(request: web.Request): try: id = request.rel_url.query['id'] body = json.loads(await request.text()) Validate.validate_object_id(id) Validate.validate_schema(body, request_validator) await RequestValidator.update(id, body, DB.get(request, table)) return web.json_response({ 'message': 'request validator updated', 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def update_rule(request: web.Request): try: ctx = json.loads(await request.text()) _id = request.rel_url.query.get('id') Validate.validate_schema(ctx, rate_limit_rule_validator) Validate.validate_object_id(_id) await RateLimiter.update_rule(_id, ctx, DB.get_redis(request)) return web.json_response({ 'message': 'rate limiter rule updated', 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def patch_handler(request: web.Request): try: ctx = json.loads(await request.text()) _id = request.rel_url.query.get('id') Validate.validate_schema(ctx, endpoint_cache_validator) Validate.validate_object_id(_id) await EndpointCacher.update(_id, pydash.omit(ctx, 'service_id', 'response_codes'), DB.get_redis(request)) return web.json_response({ 'message': 'Endpoint cache updated', 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def patch_handler(request: web.Request): try: ctx = json.loads(await request.text()) service_id = request.rel_url.query['id'] Validate.validate_object_id(service_id) Validate.validate_schema(ctx, insights_validator) await Insights.update(service_id, pydash.omit(ctx, 'id'), DB.get(request, table)) return web.json_response({ 'message': 'insight updated', }) except Exception as err: return Error.handle(err)
async def get_handler(request: web.Request): try: response = [] if 'id' in request.rel_url.query: _id = request.rel_url.query.get('id') Validate.validate_object_id(_id) cache = await EndpointCacher.get_by_id(_id, DB.get_redis(request)) if not pydash.is_empty(cache): response.append(cache) elif 'service_id' in request.rel_url.query: service_id = request.rel_url.query.get('service_id') Validate.validate_object_id(service_id) response = await EndpointCacher.get_by_service_id(service_id, DB.get_redis(request)) else: response = await EndpointCacher.get_all(DB.get_redis(request)) return web.json_response({ 'data': response, 'status_code': 200 }) except Exception as err: return Error.handle(err)
async def patch_handler_response_codes(request: web.Request): try: ctx = json.loads(await request.text()) _id = request.rel_url.query.get('id') action = request.rel_url.query.get('action') Validate.validate_object_id(_id) Validate.validate_schema(ctx, endpoint_cache_validator) if action == 'add': await EndpointCacher.add_status_codes(ctx['response_codes'], _id, DB.get_redis(request)) elif action == 'remove': await EndpointCacher.remove_status_codes(ctx['response_codes'], _id, DB.get_redis(request)) else: return web.json_response({ 'message': 'Invalid action provided', 'status_code': 400, }, status=400) return web.json_response({ 'message': 'Endpoint cache response codes updated', 'status_code': 200, }) except Exception as err: return Error.handle(err)