Skip to content

Commit 10dc699

Browse files
authored
Merge pull request #57 from polywrap/nk/uri-resolver
feat: add uri resolvers
2 parents b785c3f + 99a0f02 commit 10dc699

File tree

255 files changed

+7568
-536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+7568
-536
lines changed

packages/polywrap-client/polywrap_client/client.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
from __future__ import annotations
22

3+
import json
34
from dataclasses import dataclass
45
from textwrap import dedent
56
from typing import Any, Dict, List, Optional, Union, cast
67

78
from polywrap_core import (
89
Client,
910
ClientConfig,
11+
Env,
1012
GetFileOptions,
1113
GetManifestOptions,
1214
InvokerOptions,
1315
IUriResolutionContext,
1416
IUriResolver,
17+
IWrapPackage,
1518
TryResolveUriOptions,
16-
Env,
1719
Uri,
18-
UriPackage,
1920
UriPackageOrWrapper,
2021
UriResolutionContext,
2122
Wrapper,
23+
build_clean_uri_history,
2224
)
2325
from polywrap_manifest import AnyWrapManifest
2426
from polywrap_msgpack import msgpack_decode, msgpack_encode
@@ -61,9 +63,7 @@ def get_implementations(self, uri: Uri) -> Result[Union[List[Uri], None]]:
6163
else:
6264
return Err.from_str(f"Unable to find implementations for uri: {uri}")
6365

64-
def get_env_by_uri(
65-
self, uri: Uri
66-
) -> Union[Env, None]:
66+
def get_env_by_uri(self, uri: Uri) -> Union[Env, None]:
6767
return self._config.envs.get(uri)
6868

6969
async def get_file(
@@ -98,34 +98,32 @@ async def load_wrapper(
9898
if result.is_err():
9999
return cast(Err, result)
100100
if result.is_ok() and result.ok is None:
101-
# FIXME: add resolution stack
102101
return Err.from_str(
103102
dedent(
104103
f"""
105104
Error resolving URI "{uri.uri}"
106-
Resolution Stack: NotImplemented
105+
Resolution Stack: {json.dumps(build_clean_uri_history(resolution_context.get_history()), indent=2)}
107106
"""
108107
)
109108
)
110109

111110
uri_package_or_wrapper = result.unwrap()
112111

113112
if isinstance(uri_package_or_wrapper, Uri):
114-
# FIXME: add resolution stack
115113
return Err.from_str(
116114
dedent(
117115
f"""
118116
Error resolving URI "{uri.uri}"
119117
URI not found
120-
Resolution Stack: NotImplemented
118+
Resolution Stack: {json.dumps(build_clean_uri_history(resolution_context.get_history()), indent=2)}
121119
"""
122120
)
123121
)
124122

125-
if isinstance(uri_package_or_wrapper, UriPackage):
126-
return await uri_package_or_wrapper.package.create_wrapper()
123+
if isinstance(uri_package_or_wrapper, IWrapPackage):
124+
return await uri_package_or_wrapper.create_wrapper()
127125

128-
return Ok(uri_package_or_wrapper.wrapper)
126+
return Ok(uri_package_or_wrapper)
129127

130128
async def invoke(self, options: InvokerOptions) -> Result[Any]:
131129
resolution_context = options.resolution_context or UriResolutionContext()

packages/polywrap-client/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ exclude_dirs = ["tests"]
4242
target-version = ["py310"]
4343

4444
[tool.pyright]
45-
# default
45+
typeCheckingMode = "strict"
46+
reportShadowedImports = false
4647

4748
[tool.pytest.ini_options]
4849
asyncio_mode = "auto"

packages/polywrap-client/tests/test_bignumber.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,52 @@
55
from polywrap_client import PolywrapClient
66
from polywrap_core import Uri, InvokerOptions
77

8+
89
async def test_invoke_bignumber_1arg_and_1prop():
910
client = PolywrapClient()
1011
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
11-
args = { "arg1": "123", # The base number
12+
args = {
13+
"arg1": "123", # The base number
1214
"obj": {
13-
"prop1": "1000", # multiply the base number by this factor
14-
}
15+
"prop1": "1000", # multiply the base number by this factor
16+
},
1517
}
1618
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
1719
result = await client.invoke(options)
1820
assert result.unwrap() == "123000"
1921

22+
2023
async def test_invoke_bignumber_with_1arg_and_2props():
2124
client = PolywrapClient()
2225
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
23-
args = {
24-
"arg1": "123123",
25-
"obj": {
26-
"prop1": "1000",
27-
"prop2": "4"
28-
}
29-
}
26+
args = {"arg1": "123123", "obj": {"prop1": "1000", "prop2": "4"}}
3027
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
3128
result = await client.invoke(options)
32-
assert result.unwrap() == str(123123*1000*4)
29+
assert result.unwrap() == str(123123 * 1000 * 4)
30+
3331

3432
async def test_invoke_bignumber_with_2args_and_1prop():
3533
client = PolywrapClient()
3634
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
37-
args = {
38-
"arg1": "123123",
39-
"obj": {
40-
"prop1": "1000",
41-
"prop2": "444"
42-
}
43-
}
35+
args = {"arg1": "123123", "obj": {"prop1": "1000", "prop2": "444"}}
4436
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
4537
result = await client.invoke(options)
46-
assert result.unwrap() == str(123123*1000*444)
38+
assert result.unwrap() == str(123123 * 1000 * 444)
39+
4740

4841
async def test_invoke_bignumber_with_2args_and_2props():
4942
client = PolywrapClient()
5043
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
51-
args = {
52-
"arg1": "123123",
53-
"arg2": "555",
54-
"obj": {
55-
"prop1": "1000",
56-
"prop2": "4"
57-
}
58-
}
44+
args = {"arg1": "123123", "arg2": "555", "obj": {"prop1": "1000", "prop2": "4"}}
5945
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
6046
result = await client.invoke(options)
61-
assert result.unwrap() == str(123123*555*1000*4)
47+
assert result.unwrap() == str(123123 * 555 * 1000 * 4)
48+
6249

6350
async def test_invoke_bignumber_with_2args_and_2props_floats():
6451
client = PolywrapClient()
6552
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
66-
args = {
67-
"arg1": "123.123",
68-
"arg2": "55.5",
69-
"obj": {
70-
"prop1": "10.001",
71-
"prop2": "4"
72-
}
73-
}
53+
args = {"arg1": "123.123", "arg2": "55.5", "obj": {"prop1": "10.001", "prop2": "4"}}
7454
options = InvokerOptions(uri=uri, method="method", args=args, encode_result=False)
7555
result = await client.invoke(options)
76-
assert result.unwrap() == str(123.123*55.5*10.001*4)
56+
assert result.unwrap() == str(123.123 * 55.5 * 10.001 * 4)

packages/polywrap-client/tests/test_client.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import pytest
22
from pathlib import Path
3-
import pytest
43
from polywrap_client import PolywrapClient, PolywrapClientConfig
54
from polywrap_manifest import deserialize_wrap_manifest
6-
from polywrap_core import Uri, InvokerOptions, UriWrapper
5+
from polywrap_core import Uri, InvokerOptions, IFileReader
76
from polywrap_uri_resolvers import BaseUriResolver, SimpleFileReader, StaticResolver
87
from polywrap_result import Result, Ok, Err
9-
from polywrap_wasm import WRAP_MANIFEST_PATH, WRAP_MODULE_PATH, IFileReader, WasmWrapper
8+
from polywrap_wasm import WRAP_MANIFEST_PATH, WRAP_MODULE_PATH, WasmWrapper
109

1110
@pytest.fixture
1211
def simple_wrap_module():
@@ -58,8 +57,7 @@ async def test_invoke(
5857
wasm_module=simple_wrap_module,
5958
manifest=manifest
6059
)
61-
uri_wrapper = UriWrapper(uri=Uri("ens/wrapper.eth"), wrapper=wrapper)
62-
resolver = StaticResolver.from_list([uri_wrapper]).unwrap()
60+
resolver = StaticResolver({Uri("ens/wrapper.eth"): wrapper})
6361

6462
config = PolywrapClientConfig(resolver=resolver)
6563
client = PolywrapClient(config=config)

packages/polywrap-client/tests/test_sha3.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,78 @@ async def test_invoke_sha3_512():
1919
result = await client.invoke(options)
2020
s = hashlib.sha512()
2121
s.update(b"hello polywrap!")
22-
assert result.result == s.digest()
22+
assert result.unwrap() == s.digest()
2323

2424
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
2525
async def test_invoke_sha3_384():
2626
options = InvokerOptions(uri=uri, method="sha3_384", args=args, encode_result=False)
2727
result = await client.invoke(options)
2828
s = hashlib.sha384()
2929
s.update(b"hello polywrap!")
30-
assert result.result == s.digest()
30+
assert result.unwrap() == s.digest()
3131

3232
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
3333
async def test_invoke_sha3_256():
3434
options = InvokerOptions(uri=uri, method="sha3_256", args=args, encode_result=False)
3535
result = await client.invoke(options)
3636
s = hashlib.sha256()
3737
s.update(b"hello polywrap!")
38-
assert result.result == s.digest()
38+
assert result.unwrap() == s.digest()
3939

4040
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
4141
async def test_invoke_sha3_224():
4242
options = InvokerOptions(uri=uri, method="sha3_224", args=args, encode_result=False)
4343
result = await client.invoke(options)
4444
s = hashlib.sha224()
4545
s.update(b"hello polywrap!")
46-
assert result.result == s.digest()
46+
assert result.unwrap() == s.digest()
4747

4848
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
4949
async def test_invoke_keccak_512():
5050
options = InvokerOptions(uri=uri, method="keccak_512", args=args, encode_result=False)
5151
result = await client.invoke(options)
5252
k = keccak.new(digest_bits=512)
5353
k.update(b'hello polywrap!')
54-
assert result.result == k.digest()
54+
assert result.unwrap() == k.digest()
5555

5656
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
5757
async def test_invoke_keccak_384():
5858
options = InvokerOptions(uri=uri, method="keccak_384", args=args, encode_result=False)
5959
result = await client.invoke(options)
6060
k = keccak.new(digest_bits=384)
6161
k.update(b'hello polywrap!')
62-
assert result.result == k.digest()
62+
assert result.unwrap() == k.digest()
6363

6464
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
6565
async def test_invoke_keccak_256():
6666
options = InvokerOptions(uri=uri, method="keccak_256", args=args, encode_result=False)
6767
result = await client.invoke(options)
6868
k = keccak.new(digest_bits=256)
6969
k.update(b'hello polywrap!')
70-
assert result.result == k.digest()
70+
assert result.unwrap() == k.digest()
7171

7272
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
7373
async def test_invoke_keccak_224():
7474
options = InvokerOptions(uri=uri, method="keccak_224", args=args, encode_result=False)
7575
result = await client.invoke(options)
7676
k = keccak.new(digest_bits=224)
7777
k.update(b'hello polywrap!')
78-
assert result.result == k.digest()
78+
assert result.unwrap() == k.digest()
7979

8080
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
8181
async def test_invoke_hex_keccak_256():
8282
options = InvokerOptions(uri=uri, method="hex_keccak_256", args=args, encode_result=False)
8383
result = await client.invoke(options)
8484
k = keccak.new(digest_bits=256)
8585
k.update(b'hello polywrap!')
86-
assert result.result == k.hexdigest()
86+
assert result.unwrap() == k.hexdigest()
8787

8888
@pytest.mark.skip(reason="buffer keccak must be implemented in python in order to assert")
8989
async def test_invoke_buffer_keccak_256():
9090
options = InvokerOptions(uri=uri, method="buffer_keccak_256", args=args, encode_result=False)
9191
result = await client.invoke(options)
9292
# TODO: Not sure exactly what this function `buffer_keccak_256` is doing in order to assert it properly
93-
assert result.result == False
93+
assert result.unwrap() == False
9494

9595
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
9696
async def test_invoke_shake_256():
@@ -99,7 +99,7 @@ async def test_invoke_shake_256():
9999
result = await client.invoke(options)
100100
s = SHAKE256.new()
101101
s.update(b"hello polywrap!")
102-
assert result.result == s.read(8).hex()
102+
assert result.unwrap() == s.read(8).hex()
103103

104104
@pytest.mark.skip(reason="can't invoke sha3 wrapper due to an error related to wasmtime")
105105
async def test_invoke_shake_128():
@@ -108,4 +108,4 @@ async def test_invoke_shake_128():
108108
result = await client.invoke(options)
109109
s = SHAKE128.new()
110110
s.update(b"hello polywrap!")
111-
assert result.result == s.read(8).hex()
111+
assert result.unwrap() == s.read(8).hex()

0 commit comments

Comments
 (0)