summaryrefslogtreecommitdiffstats
path: root/examples/list_groups.py
blob: 61773e2f2154af01d774a3fef5b4d6d516463a0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
"""List a user's groups, and whether a node serving each one is up."""

import sys

import httpx
from meshbay_session import auth_key

HUB = "https://meshbay.org"
USERNAME = "alice"
PASSPHRASE = "your passphrase"


def main() -> None:
    with httpx.Client(base_url=HUB, timeout=30) as hub:
        r = hub.post("/v1/users/login",
                     json={"username": USERNAME, "auth_key": auth_key(USERNAME, PASSPHRASE)})
        if r.status_code != 200:
            sys.exit(f"Sign-in refused: {r.text}")
        auth = {"Authorization": f"Bearer {r.json()['access_token']}"}
        groups = hub.get("/v1/groups/mine", headers=auth).json()["groups"]

    # "up" means a node serving the group is connected to the hub right now.
    # It does not prove that this machine can reach that node.
    for g in groups:
        state = "up" if g["node_online"] else "down"
        print(f"{state:<6}{g['name']}  (@{g['owner_username']})")


if __name__ == "__main__":
    main()