Skip to content
Snippets Groups Projects
get-passes.py 3.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • #
    # Get a paid voucher and tell the Tahoe-LAFS client node to redeem it for some
    # ZKAPs from an issuer.  Exit with success when the Tahoe-LAFS client node
    # reports that the voucher has been redeemed.
    #
    
    
    from sys import argv
    
    from requests import post, get, put
    
    from json import dumps
    
    from time import sleep
    
        if len(argv) == 4:
            # If no issuer is given then we just won't make the charge request.
            # This is useful for following the webhook-based workflow.
            clientAPIRoot, clientAPITokenPath, voucher = argv[1:]
            issuerAPIRoot = None
        elif len(argv) == 5:
            clientAPIRoot, clientAPITokenPath, issuerAPIRoot, voucher = argv[1:]
        else:
    
                "usage: %s <client api root> <client api token path> [<issuer api root>] <voucher>",
    
        if not clientAPIRoot.endswith("/"):
            clientAPIRoot += "/"
    
        if issuerAPIRoot is not None and not issuerAPIRoot.endswith("/"):
    
            issuerAPIRoot += "/"
    
        zkapauthz = clientAPIRoot + "storage-plugins/privatestorageio-zkapauthz-v2"
    
        with open(clientAPITokenPath) as p:
            clientAPIToken = p.read().strip()
    
    
        if issuerAPIRoot is not None:
            # Submit a charge to the issuer (which is also the PaymentServer).
            charge_response = post(
                issuerAPIRoot + "v1/stripe/charge",
                dumps(charge_json(voucher)),
                headers={
                    "content-type": "application/json",
                },
            )
            charge_response.raise_for_status()
    
    
        # Tell the client to redeem the voucher.
    
        response = put(
    
            zkapauthz + "/voucher",
            dumps({"voucher": voucher}),
    
            headers={
                "content-type": "application/json",
                "authorization": "tahoe-lafs " + clientAPIToken,
            }
    
    
        if response.status_code // 100 != 2:
            print("Unexpected response: {}".format(response.content))
            response.raise_for_status()
    
    
        # Poll the vouchers list for a while to see it get redeemed.
    
        def find_redeemed_voucher():
    
            response = get(
                zkapauthz + "/voucher/" + voucher,
                headers={
                    "authorization": "tahoe-lafs " + clientAPIToken,
                },
            )
    
            response.raise_for_status()
            actual = response.json()
            print("Actual response: {}".format(actual))
    
            try:
                check = (
                    actual["version"],
                    actual["number"],
                    actual["state"]["name"],
                )
            except Exception as e:
                print("Check failed: {}".format(e))
                return False
            else:
                print("Checking {}".format(check))
                return check == (1, voucher, "redeemed")
    
        retry(
            "find redeemed voucher",
    
            find_redeemed_voucher,
    
        )
    
    
    def retry(description, f):
        for i in range(60):
            print("trying to {}...".format(description))
            if f():
                print("{} succeeded".format(description))
    
            sleep(1.0)
        raise ValueError("failed to {} after many tries".format(description))
    
    
    
    def charge_json(voucher):
        return {
            "token": "tok_abcdef",
            "voucher": voucher,
    
        }
    
    if __name__ == '__main__':
        main()