From c03c21e17e55cb9ad0958627771463fe112a7cd6 Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Tue, 22 Feb 2022 16:07:41 -0500
Subject: [PATCH] convert tahoe_lafs_downloader to the Tahoe interface

---
 src/_zkapauthorizer/recover.py            | 20 ++++++--------------
 src/_zkapauthorizer/tahoe.py              |  1 +
 src/_zkapauthorizer/tests/test_recover.py |  5 +++--
 3 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/src/_zkapauthorizer/recover.py b/src/_zkapauthorizer/recover.py
index 082077f..d5bfd04 100644
--- a/src/_zkapauthorizer/recover.py
+++ b/src/_zkapauthorizer/recover.py
@@ -19,13 +19,9 @@ from io import BytesIO
 from sqlite3 import Cursor
 from typing import BinaryIO, Callable, Dict, Iterator, Optional
 
-from allmydata.node import _Config
 from attrs import define
-from treq.client import HTTPClient
-from twisted.python.filepath import FilePath
 
-from .config import read_node_url
-from .tahoe import download
+from .tahoe import Tahoe
 
 
 class SnapshotMissing(Exception):
@@ -204,8 +200,7 @@ def recover(statements: Iterator[str], cursor) -> None:
 
 
 async def tahoe_lafs_downloader(
-    treq: HTTPClient,
-    node_config: _Config,
+    client: Tahoe,
     recovery_cap: str,
     set_state: SetState,
 ) -> Awaitable:  # Awaitable[FilePath]
@@ -213,17 +208,14 @@ async def tahoe_lafs_downloader(
     Download replica data from the given replica directory capability into the
     node's private directory.
     """
-    api_root = read_node_url(node_config)
-    snapshot_path = FilePath(node_config.get_private_path("snapshot.sql"))
+    snapshot_path = client.get_private_path("snapshot.sql")
 
     set_state(RecoveryState(stage=RecoveryStages.downloading))
-    await download(treq, snapshot_path, api_root, recovery_cap, ["snapshot.sql"])
+    await client.download(snapshot_path, recovery_cap, ["snapshot.sql"])
     return snapshot_path
 
 
-def get_tahoe_lafs_downloader(
-    httpclient: HTTPClient, node_config: _Config
-) -> Callable[[str], Downloader]:
+def get_tahoe_lafs_downloader(client: Tahoe) -> Callable[[str], Downloader]:
     """
     Bind some parameters to ``tahoe_lafs_downloader`` in a convenient way.
 
@@ -233,7 +225,7 @@ def get_tahoe_lafs_downloader(
 
     def get_downloader(cap_str):
         def downloader(set_state):
-            return tahoe_lafs_downloader(httpclient, node_config, cap_str, set_state)
+            return tahoe_lafs_downloader(client, cap_str, set_state)
 
         return downloader
 
diff --git a/src/_zkapauthorizer/tahoe.py b/src/_zkapauthorizer/tahoe.py
index 9738995..1b185ac 100644
--- a/src/_zkapauthorizer/tahoe.py
+++ b/src/_zkapauthorizer/tahoe.py
@@ -10,6 +10,7 @@ from typing import Callable, Dict, Iterable, List, Optional
 
 import treq
 from allmydata.node import _Config
+from allmydata.uri import from_string as capability_from_string
 from allmydata.util.base32 import b2a as b32encode
 from attrs import Factory, define, field
 from hyperlink import DecodedURL
diff --git a/src/_zkapauthorizer/tests/test_recover.py b/src/_zkapauthorizer/tests/test_recover.py
index e71d693..799551f 100644
--- a/src/_zkapauthorizer/tests/test_recover.py
+++ b/src/_zkapauthorizer/tests/test_recover.py
@@ -39,7 +39,7 @@ from ..recover import (
     noop_downloader,
     recover,
 )
-from ..tahoe import link, make_directory, upload
+from ..tahoe import Tahoe, link, make_directory, upload
 from .fixtures import Treq
 from .resources import client_manager
 from .sql import Table, create_table
@@ -316,7 +316,8 @@ class TahoeLAFSDownloaderTests(TestCase):
             )
         )
 
-        get_downloader = get_tahoe_lafs_downloader(httpclient, config)
+        tahoeclient = Tahoe(httpclient, config)
+        get_downloader = get_tahoe_lafs_downloader(tahoeclient)
         download = get_downloader(replica_dir_cap_str)
 
         downloaded_snapshot_path = yield Deferred.fromCoroutine(
-- 
GitLab