chain of responsibility pattern most web frameworks create request object that is used throughout the system simple request object might look like thisclass request(object)def __init__(selfheadersurlbodygetpost)self headers self url self body self get self post at the momentyou have no extra functionality associated with the requestbut this limited object should be good start for you to build up some intuition about the way middleware is used in handling client requests to web app web frameworks need to return some sort of response to the client let' define basic response objectclass response(object)def __init__(selfheadersstatus_codebody)self headers self status_code self body if you look at the documentation of one of the more popular frameworkslike django or flaskyou will see that the objects are way more involved that the simple ones just definedbut these will serve our purpose one of the things we would like our middleware to do is check for token in the request object' headers and then add the user object to the object this will allow the main app to access the currently logged-in user to alter the interface and information displayed as needed let' write basic function that will grab the user token and load user object from database the function will then return user object that can be attached to the request object import user def get_user_object(usernamepassword)return user find_by_token(usernamepassword