Skip to content

Commit

Permalink
Update v6->v7 migration details for middleware builder classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Aug 27, 2024
1 parent 9194222 commit d1c0f29
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
44 changes: 40 additions & 4 deletions docs/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,53 @@ instantiated with custom middleware.
Class-Based Middleware Model
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The middleware model has been changed to a class-based model. Previously, middleware
were defined as functions that tightly wrapped the provider's ``make_request`` function,
where transformations could be conditionally applied before and after the request was made.
The middleware model has been changed to a class-based model.

.. code-block:: python
# v6 (no longer supported)
from web3.middleware import pythonic_middleware
w3.middleware_onion.add(pythonic_middleware)
# v7
from web3.middleware import PythonicMiddleware
w3.middleware_onion.add(PythonicMiddleware)
Previously, middleware were defined as functions that tightly wrapped the provider's
``make_request`` function, where transformations could be conditionally applied before
and after the request was made.

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

The new middleware model is documented in the :ref:`middleware_internals` section.
Major changes for migration are highlighted in this section. Consult the
:ref:`middleware_internals` section of the documentation for specifics and examples on
the new class-based design.


Middleware Builder Classes
~~~~~~~~~~~~~~~~~~~~~~~~~~

In ``v6``, certain middleware needed to be constructed with parameters. This was done
by passing the parameters to a constructor method.

.. code-block:: python
# v6 (no longer supported)
from web3.middleware import construct_sign_and_send_raw_middleware
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(private_key))
In the class-based ``v7`` middleware model, a middleware builder class is instantiated
with the necessary parameters via the ``build()`` method.

.. code-block:: python
# v7
from web3.middleware import SignAndSendRawMiddlewareBuilder
w3.middleware_onion.add(SignAndSendRawMiddlewareBuilder.build(private_key))
Middleware Renaming and Removals
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3462.docs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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 comments on commit d1c0f29

Please sign in to comment.