diff --git a/docs/v4/index.html b/docs/v4/index.html index 14ef5dd..6c05f6d 100644 --- a/docs/v4/index.html +++ b/docs/v4/index.html @@ -75,6 +75,10 @@

Sub-modules

Module for interacting with an SQLite database

+
spin_sdk.util
+
+

Module for general-purpose utility functions

+
spin_sdk.variables

Module for interacting with Spin Variables

@@ -107,6 +111,7 @@

Sub-modules

  • spin_sdk.postgres
  • spin_sdk.redis
  • spin_sdk.sqlite
  • +
  • spin_sdk.util
  • spin_sdk.variables
  • spin_sdk.wit
  • diff --git a/docs/v4/util.html b/docs/v4/util.html new file mode 100644 index 0000000..3b2b8bc --- /dev/null +++ b/docs/v4/util.html @@ -0,0 +1,103 @@ + + + + + + +spin_sdk.util API documentation + + + + + + + + + + + +
    +
    +
    +

    Module spin_sdk.util

    +
    +
    +

    Module for general-purpose utility functions

    +
    +
    +
    +
    +
    +
    +

    Functions

    +
    +
    +async def collect(tuple: Tuple[componentize_py_async_support.streams.StreamReader[~T], componentize_py_async_support.futures.FutureReader[componentize_py_types.Ok[None] | componentize_py_types.Err[~E]]]) ‑> List[~T] +
    +
    +
    + +Expand source code + +
    async def collect(tuple: Tuple[StreamReader[T], FutureReader[Result[None, E]]]) -> List[T]:
    +    """
    +    Collect all items from the StreamReader portion of the provided Tuple and return them in a List,
    +    verifying the FutureReader result upon stream completion and, if it is error, raising it as an exception.
    +    """
    +    stream = tuple[0]
    +    future = tuple[1]
    +    collected = []
    +    with stream, future:
    +        while not stream.writer_dropped:
    +            collected += await stream.read(128)
    +        result = await future.read()
    +        if isinstance(result, Err):
    +            raise result
    +        else:
    +            return collected
    +
    +

    Collect all items from the StreamReader portion of the provided Tuple and return them in a List, +verifying the FutureReader result upon stream completion and, if it is error, raising it as an exception.

    +
    +
    +
    +
    +
    +
    + +
    + + + diff --git a/examples/spin-kv/app.py b/examples/spin-kv/app.py index ba0d974..71a93fc 100644 --- a/examples/spin-kv/app.py +++ b/examples/spin-kv/app.py @@ -1,8 +1,4 @@ -from typing import TypeVar, Tuple, List -from componentize_py_types import Result, Err -from componentize_py_async_support.streams import StreamReader -from componentize_py_async_support.futures import FutureReader -from spin_sdk import http, key_value +from spin_sdk import http, key_value, util from spin_sdk.http import Request, Response from spin_sdk.key_value import Store @@ -10,22 +6,14 @@ class WasiHttpHandler030Rc20260315(http.Handler): async def handle_request(self, request: Request) -> Response: with await key_value.open_default() as a: await a.set("test", bytes("hello world!", "utf-8")) - print(await get_keys(a)) + print(await util.collect(await a.get_keys())) print(await a.exists("test")) print(await a.get("test")) await a.delete("test") - print(await get_keys(a)) + print(await util.collect(await a.get_keys())) return Response( 200, {"content-type": "text/plain"}, bytes("Hello from Python!", "utf-8") ) - -async def get_keys(store: Store) -> list[str]: - stream, future = await store.get_keys() - with stream, future: - keys = [] - while not stream.writer_dropped: - keys += await stream.read(max_count=100) - return keys diff --git a/src/spin_sdk/util.py b/src/spin_sdk/util.py new file mode 100644 index 0000000..0a2c2a5 --- /dev/null +++ b/src/spin_sdk/util.py @@ -0,0 +1,26 @@ +"""Module for general-purpose utility functions""" + +from typing import TypeVar, Tuple, List +from componentize_py_types import Result, Err +from componentize_py_async_support.streams import StreamReader +from componentize_py_async_support.futures import FutureReader + +T = TypeVar('T') +E = TypeVar('E') + +async def collect(tuple: Tuple[StreamReader[T], FutureReader[Result[None, E]]]) -> List[T]: + """ + Collect all items from the StreamReader portion of the provided Tuple and return them in a List, + verifying the FutureReader result upon stream completion and, if it is error, raising it as an exception. + """ + stream = tuple[0] + future = tuple[1] + collected = [] + with stream, future: + while not stream.writer_dropped: + collected += await stream.read(128) + result = await future.read() + if isinstance(result, Err): + raise result + else: + return collected