@@ -82,17 +82,53 @@ instantiated with custom middleware.
82
82
Class-Based Middleware Model
83
83
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84
84
85
- The middleware model has been changed to a class-based model. Previously, middleware
86
- were defined as functions that tightly wrapped the provider's ``make_request `` function,
87
- where transformations could be conditionally applied before and after the request was made.
85
+ The middleware model has been changed to a class-based model.
86
+
87
+ .. code-block :: python
88
+
89
+ # v6 (no longer supported)
90
+ from web3.middleware import pythonic_middleware
91
+ w3.middleware_onion.add(pythonic_middleware)
92
+
93
+ # v7
94
+ from web3.middleware import PythonicMiddleware
95
+ w3.middleware_onion.add(PythonicMiddleware)
96
+
97
+ Previously, middleware were defined as functions that tightly wrapped the provider's
98
+ ``make_request `` function, where transformations could be conditionally applied before
99
+ and after the request was made.
88
100
89
101
Now, middleware logic can be separated into ``request_processor `` and ``response_processor ``
90
102
functions that enable pre-request and post-response logic, respectively. This change offers
91
103
a simpler, clearer interface for defining middleware, gives more flexibility for
92
104
asynchronous operations and also paves the way for supporting batch requests - included in
93
105
the roadmap for web3.py.
94
106
95
- The new middleware model is documented in the :ref: `middleware_internals ` section.
107
+ Major changes for migration are highlighted in this section. Consult the
108
+ :ref: `middleware_internals ` section of the documentation for specifics and examples on
109
+ the new class-based design.
110
+
111
+
112
+ Middleware Builder Classes
113
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
114
+
115
+ In ``v6 ``, certain middleware needed to be constructed with parameters. This was done
116
+ by passing the parameters to a constructor method.
117
+
118
+ .. code-block :: python
119
+
120
+ # v6 (no longer supported)
121
+ from web3.middleware import construct_sign_and_send_raw_middleware
122
+ w3.middleware_onion.add(construct_sign_and_send_raw_middleware(private_key))
123
+
124
+ In the class-based ``v7 `` middleware model, a middleware builder class is instantiated
125
+ with the necessary parameters via the ``build() `` method.
126
+
127
+ .. code-block :: python
128
+
129
+ # v7
130
+ from web3.middleware import SignAndSendRawMiddlewareBuilder
131
+ w3.middleware_onion.add(SignAndSendRawMiddlewareBuilder.build(private_key))
96
132
97
133
98
134
Middleware Renaming and Removals
0 commit comments