def create_operation(name, data, credits_per_probe, result_format): try: user = request.user probes = current_app.db.probes.find_selected_probes(data["probes"]) available_probes = get_available_probes(probes, credits_per_probe) if len(available_probes) == 0: return error_response(HTTP_NOT_FOUND, "No available probes") total_credits = credits_per_probe * len(available_probes) operation = current_app.db.operations.create_operation( name, data["params"], available_probes, user, total_credits, result_format) operation_data = operation.public_data() if user.credits - total_credits < 0: return error_response(HTTP_BAD_REQUEST, "Not enough credits") current_app.db.users.change_credits(user, user.credits - total_credits) do_operation(name, available_probes, operation_data, credits_per_probe) return make_response(operation_data, HTTP_CREATED) except: return error_response(HTTP_NOT_FOUND, "Invalid data provided")
def give_credits(): data = request.get_json(force=True) if not validate_credits(data): return error_response(HTTP_BAD_REQUEST, "Invalid data provided") try: user = current_app.db.users.find_user_by_id(data["id"]) current_app.db.users.change_credits(user, user.credits + data["credits"]) return make_response(user.public_data(), HTTP_OK) except: return error_response(HTTP_NOT_FOUND, "User doesn't exist")
def create_user(): data = request.get_json(force=True) validation_error = validate_user_data(data) if validation_error != "": return error_response(HTTP_BAD_REQUEST, validation_error) try: user = current_app.db.users.create_user(email=data["email"], password=data["password"], is_superuser=False, credits_=0) user_data = user.public_data() return make_response(jsonify(user_data), HTTP_CREATED) except: return error_response(HTTP_BAD_REQUEST, "That email is already registered")
def dns(): data = request.get_json(force=True) if not validate_dns(data): return error_response(HTTP_BAD_REQUEST, "Invalid data provided") total_destinations = count_destinations(data["params"]) credits_per_probe = calculate_credits_dns(total_destinations) return create_operation("dns", data, credits_per_probe, "text")
def ping(): data = request.get_json(force=True) if not validate_ping(data): return error_response(HTTP_BAD_REQUEST, "Invalid data provided") total_destinations = count_destinations(data["params"]) credits_ = calculate_credits_ping(total_destinations) return create_operation("ping", data, credits_, "json")
def operation(): op_id = request.args.get('id') try: operation = current_app.db.operations.find_operation(op_id) operation_data = operation.public_data() return make_response(operation_data, HTTP_OK) except: return error_response(HTTP_NOT_FOUND, "Operation doesn't exist")
def login_user(): data = request.get_json(force=True) validation_error = validate_user_data_present(data) if validation_error != "": return error_response(HTTP_BAD_REQUEST, validation_error) try: user = current_app.db.users.find_user(data["email"]) if not user.verify_password(data["password"]): return error_response(HTTP_NOT_FOUND, "Invalid email or password") user_data = user.public_data() access_token = current_app.token.create_token(user_data) return make_response({"access_token": access_token}, HTTP_OK) except: return error_response(HTTP_NOT_FOUND, "Invalid email or password")
def traceroute(): data = request.get_json(force=True) if not validate_traceroute(data): return error_response(HTTP_BAD_REQUEST, "Invalid data provided") total_destinations = count_destinations(data["params"]) credits_ = calculate_credits_traceroute(total_destinations) return create_operation("traceroute", data, credits_, data["result_format"])
def register_probe(): user = request.user location_json = request.get_json(force=True) validation_error = validate_location(location_json) if validation_error != "": return error_response(HTTP_BAD_REQUEST, validation_error) location = location_json["location"] probe = current_app.db.probes.create_probe(user, location) token = current_app.token.create_token(probe.public_data()) return make_response({"token": token}, HTTP_CREATED)
def traceroute(): data = request.get_json(force=True) if not validate_traceroute(data): return error_response(HTTP_BAD_REQUEST, "Invalid data provided") total_destinations = count_destinations(data["params"]) [credits_for_probe, credits_per_probe] = calculate_credits_traceroute( data["params"]["cron"], data["params"]["stop_time"], int(data["params"]["times_per_minute"]), total_destinations ) return create_operation( "traceroute", data, credits_for_probe, credits_per_probe, data["result_format"] )