Skip to content
This repository was archived by the owner on Sep 26, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
contrib - Adds ping command to client
  • Loading branch information
sr-gi committed May 19, 2021
commit ada366d1c9084994b9adf98a80fd08d4309421f0
8 changes: 8 additions & 0 deletions contrib/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ Refer to [INSTALL.md](INSTALL.md)

The client has four commands:

- `ping`: pings the tower to check if it's online.
- `register`: registers your user with the tower.
- `add_appointment`: sends a json formatted appointment to the tower.
- `get_appointment`: gets json formatted data about an appointment from the tower.
- `help`: shows a list of commands or help for a specific command.

### ping
This command is used to check the status of the tower.

#### Usage

teos-client ping

### register
This commands serves as registration. It sends your public key to the tower to create a subscription (free at the moment) and returns a number of available appointment slots in the tower. Topping up the subscription can be done by simply sending a register message again.

Expand Down
12 changes: 12 additions & 0 deletions contrib/client/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def show_usage():
"USAGE: "
"\n\tteos-client [global options] command [command options] [arguments]"
"\n\nCOMMANDS:"
"\n\tping \t\t\tPings the tower to check if it's online."
"\n\tregister \t\tRegisters your user public key with the tower."
"\n\tadd_appointment \tRegisters a json formatted appointment with the tower."
"\n\tget_appointment \tGets json formatted data about an appointment from the tower."
Expand All @@ -16,6 +17,17 @@ def show_usage():
)


def help_ping():
return (
"NAME:"
"\n\n\tping"
"\n\nUSAGE:"
"\n\n\tteos-client ping"
"\n\nDESCRIPTION:"
"\n\n\tPings the tower to check if it is online."
)


def help_register():
return (
"NAME:"
Expand Down
41 changes: 38 additions & 3 deletions contrib/client/teos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,43 @@
help_get_appointment,
help_get_subscription_info,
help_register,
help_ping,
)

logging.basicConfig(level=logging.INFO, format="%(message)s")

logger = logging.getLogger()


def ping(teos_url):
"""
Pings the tower to check if it is online.

Args:
teos_url (:obj:`str`): the teos base url.

Raises:
:obj:`TowerResponseError`: if the tower responded with an error, or the response was invalid.
:obj:`ConnectionError`: if the client cannot connect to the tower.
"""

ping_endpoint = "{}/ping".format(teos_url)

logger.info(f"Pinging the Eye of Satoshi at {teos_url}")

try:
response = requests.get(ping_endpoint)

if response.status_code == constants.HTTP_EMPTY:
logger.info(f"The Eye of Satoshi is alive")
else:
raise TowerResponseError(
"The server returned an error", status_code=response.status_code, reason=response.reason, data=response,
)
except ConnectionError:
raise ConnectionError(f"The Eye of Satoshi is down")


def register(user_id, teos_id, teos_url):
"""
Registers the user to the tower.
Expand All @@ -51,8 +81,7 @@ def register(user_id, teos_id, teos_url):
Raises:
:obj:`InvalidParameter`: if `user_id` is invalid.
:obj:`ConnectionError`: if the client cannot connect to the tower.
:obj:`TowerResponseError`: if the tower responded with an error, or the
response was invalid.
:obj:`TowerResponseError`: if the tower responded with an error, or the response was invalid.
"""

if not is_compressed_pk(user_id):
Expand Down Expand Up @@ -460,6 +489,9 @@ def main(command, args, command_line_conf):
Cryptographer.save_key_file(user_sk.to_der(), "user_sk", config.get("DATA_DIR"))
user_id = Cryptographer.get_compressed_pk(user_sk.public_key)

if command == "ping":
ping(teos_url)

if command == "register":
if not args:
raise InvalidParameter("Cannot register. No tower id was given")
Expand Down Expand Up @@ -515,6 +547,9 @@ def main(command, args, command_line_conf):
if args:
command = args.pop(0)

if command == "ping":
sys.exit(help_ping())

if command == "register":
sys.exit(help_register())

Expand All @@ -541,7 +576,7 @@ def main(command, args, command_line_conf):

def run():
command_line_conf = {}
commands = ["register", "add_appointment", "get_appointment", "get_subscription_info", "help"]
commands = ["ping", "register", "add_appointment", "get_appointment", "get_subscription_info", "help"]

try:
opts, args = getopt(argv[1:], "h", ["apiconnect=", "apiport=", "help"])
Expand Down
17 changes: 17 additions & 0 deletions contrib/client/test/test_teos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

teos_url = "http://{}:{}".format(config.get("API_CONNECT"), config.get("API_PORT"))
add_appointment_endpoint = "{}/add_appointment".format(teos_url)
ping_endpoint = "{}/ping".format(teos_url)
register_endpoint = "{}/register".format(teos_url)
get_appointment_endpoint = "{}/get_appointment".format(teos_url)
get_all_appointments_endpoint = "{}/get_all_appointments".format(teos_url)
Expand Down Expand Up @@ -83,6 +84,22 @@ def post_response():
}


@responses.activate
def test_ping():
# Simulate a ping response with the tower offline
with pytest.raises(ConnectionError):
teos_client.ping(teos_url)

# Simulate a ping response with the tower online
responses.add(responses.GET, ping_endpoint, status=204)
teos_client.ping(teos_url)

# Simulate a ping response with the tower erroring
with pytest.raises(TowerResponseError):
responses.replace(responses.GET, ping_endpoint, status=404)
teos_client.ping(teos_url)


@responses.activate
def test_register():
# Simulate a register response
Expand Down