import urllib.request
import json
import ssl

def fetch_ls(port, csrf, method, args):
    url = f"http://127.0.0.1:{port}/exa.language_server_pb.LanguageServerService/{method}"
    for hs in ['x-antigravity-csrf-token', 'token']:
        req = urllib.request.Request(url, data=json.dumps(args).encode(), headers={
            'Content-Type': 'application/json',
            hs: csrf
        })
        try:
            with urllib.request.urlopen(req) as response:
                return json.loads(response.read().decode('utf-8'))
        except Exception as e:
            continue
    return "Failed"

# Process 1
p1 = 60517
c1 = "7c0c7815-ec11-48d6-9866-daab2690448f"
ec1 = "f348d963-9a36-43ea-a708-603e668b0063"

# Process 2
p2 = 54285
c2 = "5e529def-51fe-4bde-9955-5eca7299bd89"
ec2 = "b9bc824e-5543-4e26-99b3-2387fe4d2942"

target = "370d1a09-1fa8-4aed-90d7-4024e36b3a2d"
args = {"cascadeId": target, "verbosity": 1}
args_alt = {"googleAgentId": target}
args_all = {"limit": 10}

for port, csrf in [(p1, c1), (p1, ec1), (p2, c2), (p2, ec2)]:
    res = fetch_ls(port, csrf, "GetCascadeTrajectorySteps", args)
    print(f"Port {port} with csrf {csrf[:8]}: GetCascadeTrajectorySteps = {str(res)[:100]}")
    res_alt = fetch_ls(port, csrf, "GetCascadeTrajectory", args_alt)
    print(f"Port {port} with csrf {csrf[:8]}: GetCascadeTrajectory = {str(res_alt)[:100]}")
