1313 GetManifestOptions ,
1414 InvokerOptions ,
1515 IUriResolutionContext ,
16- IUriResolver ,
17- IWrapPackage ,
16+ UriPackage ,
17+ UriResolver ,
18+ UriWrapper ,
1819 TryResolveUriOptions ,
1920 Uri ,
2021 UriPackageOrWrapper ,
21- UriResolutionContext ,
2222 Wrapper ,
23- build_clean_uri_history ,
2423)
2524from polywrap_manifest import AnyWrapManifest
2625from polywrap_msgpack import msgpack_decode , msgpack_encode
27- from polywrap_result import Err , Ok , Result
28- from polywrap_uri_resolvers import FsUriResolver , SimpleFileReader
26+ from polywrap_uri_resolvers import UriResolutionContext , build_clean_uri_history
2927
3028
3129@dataclass (slots = True , kw_only = True )
@@ -36,16 +34,13 @@ class PolywrapClientConfig(ClientConfig):
3634class PolywrapClient (Client ):
3735 _config : PolywrapClientConfig
3836
39- def __init__ (self , config : Optional [PolywrapClientConfig ] = None ):
40- # TODO: this is naive solution need to use polywrap-client-config-builder once we have it
41- self ._config = config or PolywrapClientConfig (
42- resolver = FsUriResolver (file_reader = SimpleFileReader ())
43- )
37+ def __init__ (self , config : PolywrapClientConfig ):
38+ self ._config = config
4439
4540 def get_config (self ):
4641 return self ._config
4742
48- def get_uri_resolver (self ) -> IUriResolver :
43+ def get_uri_resolver (self ) -> UriResolver :
4944 return self ._config .resolver
5045
5146 def get_envs (self ) -> Dict [Uri , Env ]:
@@ -56,100 +51,81 @@ def get_interfaces(self) -> Dict[Uri, List[Uri]]:
5651 interfaces : Dict [Uri , List [Uri ]] = self ._config .interfaces
5752 return interfaces
5853
59- def get_implementations (self , uri : Uri ) -> Result [ Union [List [Uri ], None ] ]:
54+ def get_implementations (self , uri : Uri ) -> Union [List [Uri ], None ]:
6055 interfaces : Dict [Uri , List [Uri ]] = self .get_interfaces ()
61- if interfaces .get (uri ):
62- return Ok (interfaces .get (uri ))
63- else :
64- return Err .from_str (f"Unable to find implementations for uri: { uri } " )
56+ return interfaces .get (uri )
6557
6658 def get_env_by_uri (self , uri : Uri ) -> Union [Env , None ]:
6759 return self ._config .envs .get (uri )
6860
69- async def get_file (
70- self , uri : Uri , options : GetFileOptions
71- ) -> Result [Union [bytes , str ]]:
72- loaded_wrapper = (await self .load_wrapper (uri )).unwrap ()
61+ async def get_file (self , uri : Uri , options : GetFileOptions ) -> Union [bytes , str ]:
62+ loaded_wrapper = await self .load_wrapper (uri )
7363 return await loaded_wrapper .get_file (options )
7464
7565 async def get_manifest (
7666 self , uri : Uri , options : Optional [GetManifestOptions ] = None
77- ) -> Result [ AnyWrapManifest ] :
78- loaded_wrapper = ( await self .load_wrapper (uri )). unwrap ( )
67+ ) -> AnyWrapManifest :
68+ loaded_wrapper = await self .load_wrapper (uri )
7969 return loaded_wrapper .get_manifest ()
8070
8171 async def try_resolve_uri (
82- self , options : TryResolveUriOptions
83- ) -> Result [ UriPackageOrWrapper ] :
72+ self , options : TryResolveUriOptions [ UriPackageOrWrapper ]
73+ ) -> UriPackageOrWrapper :
8474 uri = options .uri
8575 uri_resolver = self ._config .resolver
8676 resolution_context = options .resolution_context or UriResolutionContext ()
8777
8878 return await uri_resolver .try_resolve_uri (uri , self , resolution_context )
8979
9080 async def load_wrapper (
91- self , uri : Uri , resolution_context : Optional [IUriResolutionContext ] = None
92- ) -> Result [Wrapper ]:
81+ self ,
82+ uri : Uri ,
83+ resolution_context : Optional [IUriResolutionContext [UriPackageOrWrapper ]] = None ,
84+ ) -> Wrapper [UriPackageOrWrapper ]:
9385 resolution_context = resolution_context or UriResolutionContext ()
9486
95- result = await self .try_resolve_uri (
87+ uri_package_or_wrapper = await self .try_resolve_uri (
9688 TryResolveUriOptions (uri = uri , resolution_context = resolution_context )
9789 )
98- if result .is_err ():
99- return cast (Err , result )
100- if result .is_ok () and result .ok () is None :
101- return Err .from_str (
102- dedent (
103- f"""
104- Error resolving URI "{ uri .uri } "
105- Resolution Stack: { json .dumps (build_clean_uri_history (resolution_context .get_history ()), indent = 2 )}
106- """
107- )
108- )
10990
110- uri_package_or_wrapper = result .unwrap ()
111-
112- if isinstance (uri_package_or_wrapper , Uri ):
113- return Err .from_str (
114- dedent (
115- f"""
116- Error resolving URI "{ uri .uri } "
117- URI not found
118- Resolution Stack: { json .dumps (build_clean_uri_history (resolution_context .get_history ()), indent = 2 )}
119- """
120- )
91+ if isinstance (uri_package_or_wrapper , UriPackage ):
92+ return await cast (
93+ UriPackage [UriPackageOrWrapper ], uri_package_or_wrapper
94+ ).package .create_wrapper ()
95+
96+ if isinstance (uri_package_or_wrapper , UriWrapper ):
97+ return cast (UriWrapper [UriPackageOrWrapper ], uri_package_or_wrapper ).wrapper
98+
99+ raise RuntimeError (
100+ dedent (
101+ f"""
102+ Error resolving URI "{ uri .uri } "
103+ URI not found
104+ Resolution Stack: { json .dumps (build_clean_uri_history (resolution_context .get_history ()), indent = 2 )}
105+ """
121106 )
107+ )
122108
123- if isinstance (uri_package_or_wrapper , IWrapPackage ):
124- return await uri_package_or_wrapper .create_wrapper ()
125-
126- return Ok (uri_package_or_wrapper )
127109
128- async def invoke (self , options : InvokerOptions ) -> Result [ Any ] :
110+ async def invoke (self , options : InvokerOptions [ UriPackageOrWrapper ] ) -> Any :
129111 resolution_context = options .resolution_context or UriResolutionContext ()
130- wrapper_result = await self .load_wrapper (
112+ wrapper = await self .load_wrapper (
131113 options .uri , resolution_context = resolution_context
132114 )
133- if wrapper_result .is_err ():
134- return cast (Err , wrapper_result )
135- wrapper = wrapper_result .unwrap ()
136115 options .env = options .env or self .get_env_by_uri (options .uri )
137116
138- result = await wrapper .invoke (options , invoker = self )
139- if result .is_err ():
140- return cast (Err , result )
141- invocable_result = result .unwrap ()
117+ invocable_result = await wrapper .invoke (options , invoker = self )
142118
143119 if options .encode_result and not invocable_result .encoded :
144120 encoded = msgpack_encode (invocable_result .result )
145- return Ok ( encoded )
121+ return encoded
146122
147123 if (
148124 not options .encode_result
149125 and invocable_result .encoded
150126 and isinstance (invocable_result .result , (bytes , bytearray ))
151127 ):
152128 decoded : Any = msgpack_decode (invocable_result .result )
153- return Ok ( decoded )
129+ return decoded
154130
155- return Ok ( invocable_result .result )
131+ return invocable_result .result
0 commit comments