#!/usr/bin/env python3
"""
Connects two running Cloudless servers A and B, 
 in the sense that B acts as a proxy for A,
 and A is proxied under the name $NAME
i.e. Cloudless B will redirect: 
http://cloudless-B-url/proxy/$NAME/* to A/*
"""

import requests
import argparse
import json

parser = argparse.ArgumentParser()
parser.add_argument(
    "proxied",
    help="Cloudless URL for the proxied server"
)
parser.add_argument(
    "proxy",
    help="Cloudless URL for the proxying server"
)
parser.add_argument(
    "name",
    help="Name for the connections, unique within the proxying server"
)

args = parser.parse_args()

assert args.proxied.startswith("http://")  or args.proxied.startswith("https://")
assert args.proxy.startswith("ws://")  or args.proxy.startswith("wss://")
params = {
    "proxy": args.proxy,
    "name": args.name,
}
proxied_instance = args.proxied + "/connect_to_cloudless"
response = requests.put(proxied_instance,params=params)
print("Status:", response.status_code, response.text)