33from abc import ABC , abstractmethod
44from datetime import timedelta
55from pathlib import Path
6+ from typing import List , Optional
67
8+ import momento .config .middleware .aio
79from momento .retry import RetryStrategy
810
11+ from .middleware import Middleware
912from .transport .transport_strategy import TransportStrategy
1013
1114
1215class ConfigurationBase (ABC ):
13- # TODO: Middlewares
1416 @abstractmethod
1517 def get_retry_strategy (self ) -> RetryStrategy :
1618 pass
@@ -35,20 +37,39 @@ def with_client_timeout(self, client_timeout: timedelta) -> Configuration:
3537 def with_root_certificates_pem (self , root_certificate_path : Path ) -> Configuration :
3638 pass
3739
40+ @abstractmethod
41+ def with_middlewares (self , middlewares : List [Middleware ]) -> Configuration :
42+ pass
43+
44+ @abstractmethod
45+ def add_middleware (self , middleware : Middleware ) -> Configuration :
46+ pass
47+
48+ @abstractmethod
49+ def get_middlewares (self ) -> List [Middleware ]:
50+ pass
51+
3852
3953class Configuration (ConfigurationBase ):
4054 """Configuration options for Momento Simple Cache Client."""
4155
42- def __init__ (self , transport_strategy : TransportStrategy , retry_strategy : RetryStrategy ):
56+ def __init__ (
57+ self ,
58+ transport_strategy : TransportStrategy ,
59+ retry_strategy : RetryStrategy ,
60+ middlewares : Optional [List [Middleware ]] = None ,
61+ ):
4362 """Instantiate a Configuration.
4463
4564 Args:
4665 transport_strategy (TransportStrategy): Configuration options for networking with
4766 the Momento service.
4867 retry_strategy (RetryStrategy): the strategy to use when determining whether to retry a grpc call.
68+ middlewares: Middleware that can intercept Momento calls. May be aio or synchronous.
4969 """
5070 self ._transport_strategy = transport_strategy
5171 self ._retry_strategy = retry_strategy
72+ self ._middlewares : List [Middleware ] = list (middlewares or [])
5273
5374 def get_retry_strategy (self ) -> RetryStrategy :
5475 """Access the retry strategy.
@@ -67,7 +88,7 @@ def with_retry_strategy(self, retry_strategy: RetryStrategy) -> Configuration:
6788 Returns:
6889 Configuration: the new Configuration with the specified RetryStrategy.
6990 """
70- return Configuration (self ._transport_strategy , retry_strategy )
91+ return Configuration (self ._transport_strategy , retry_strategy , self . _middlewares )
7192
7293 def get_transport_strategy (self ) -> TransportStrategy :
7394 """Access the transport strategy.
@@ -86,7 +107,7 @@ def with_transport_strategy(self, transport_strategy: TransportStrategy) -> Conf
86107 Returns:
87108 Configuration: the new Configuration with the specified TransportStrategy.
88109 """
89- return Configuration (transport_strategy , self ._retry_strategy )
110+ return Configuration (transport_strategy , self ._retry_strategy , self . _middlewares )
90111
91112 def with_client_timeout (self , client_timeout : timedelta ) -> Configuration :
92113 """Copies the Configuration and sets the new client-side timeout in the copy's TransportStrategy.
@@ -97,7 +118,11 @@ def with_client_timeout(self, client_timeout: timedelta) -> Configuration:
97118 Return:
98119 Configuration: the new Configuration.
99120 """
100- return Configuration (self ._transport_strategy .with_client_timeout (client_timeout ), self ._retry_strategy )
121+ return Configuration (
122+ self ._transport_strategy .with_client_timeout (client_timeout ),
123+ self ._retry_strategy ,
124+ self ._middlewares ,
125+ )
101126
102127 def with_root_certificates_pem (self , root_certificates_pem_path : Path ) -> Configuration :
103128 """Copies the Configuration and sets the new root certificates in the copy's TransportStrategy.
@@ -106,10 +131,60 @@ def with_root_certificates_pem(self, root_certificates_pem_path: Path) -> Config
106131 root_certificates_pem_path (Path): the new root certificates.
107132
108133 Returns:
109- ConfigurationBase : the new Configuration.
134+ Configuration : the new Configuration.
110135 """
111136 grpc_configuration = self ._transport_strategy .get_grpc_configuration ().with_root_certificates_pem (
112137 root_certificates_pem_path
113138 )
114139 transport_strategy = self ._transport_strategy .with_grpc_configuration (grpc_configuration )
115140 return self .with_transport_strategy (transport_strategy )
141+
142+ def with_middlewares (self , middlewares : List [Middleware ]) -> Configuration :
143+ """Copies the Configuration and adds the new middlewares to the end of the list.
144+
145+ Args:
146+ middlewares: the middleware list to be appended to the Configuration's existing middleware. These can be
147+ aio or synchronous middleware.
148+
149+ Returns:
150+ Configuration: the new Configuration.
151+ """
152+ new_middlewares = self ._middlewares .copy () + middlewares
153+ return Configuration (self ._transport_strategy , self ._retry_strategy , new_middlewares )
154+
155+ def add_middleware (self , middleware : Middleware ) -> Configuration :
156+ """Copies the Configuration and adds the new middleware to the end of the list.
157+
158+ Args:
159+ middleware: the middleware to be appended to the Configuration's existing middleware. This can be aio or
160+ synchronous middleware.
161+
162+ Returns:
163+ Configuration: the new Configuration.
164+ """
165+ new_middlewares = self ._middlewares .copy () + [middleware ]
166+ return Configuration (self ._transport_strategy , self ._retry_strategy , new_middlewares )
167+
168+ def get_middlewares (self ) -> List [Middleware ]:
169+ """Access the middleware list.
170+
171+ Returns:
172+ the configuration's list of middleware.
173+ """
174+ return self ._middlewares .copy ()
175+
176+ def get_aio_middlewares (self ) -> List [momento .config .middleware .aio .Middleware ]:
177+ """Access the aio middleware from the middleware list.
178+
179+ Returns:
180+ the configuration's list of aio middleware.
181+ """
182+ return [m for m in self ._middlewares if isinstance (m , momento .config .middleware .aio .Middleware )]
183+
184+ def get_sync_middlewares (self ) -> List [momento .config .middleware .synchronous .Middleware ]:
185+ """Access the synchronous middleware from the middleware list.
186+
187+ Returns:
188+ the configuration's list of synchronous middleware.
189+ """
190+ return [m for m in self ._middlewares if isinstance (m , momento .config .middleware .synchronous .Middleware )]
0 commit comments