Skip to content

Commit d1c0f29

Browse files
committed
Update v6->v7 migration details for middleware builder classes.
1 parent 9194222 commit d1c0f29

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

docs/migration.rst

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,53 @@ instantiated with custom middleware.
8282
Class-Based Middleware Model
8383
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8484

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.
88100

89101
Now, middleware logic can be separated into ``request_processor`` and ``response_processor``
90102
functions that enable pre-request and post-response logic, respectively. This change offers
91103
a simpler, clearer interface for defining middleware, gives more flexibility for
92104
asynchronous operations and also paves the way for supporting batch requests - included in
93105
the roadmap for web3.py.
94106

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))
96132
97133
98134
Middleware Renaming and Removals

newsfragments/3462.docs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update ``v6`` -> ``v7`` migration guide with examples for importing and adding middleware, as well as examples on how to use the ``MiddlewareBuilder`` classes.

0 commit comments

Comments
 (0)