How can I store request-local information in Yesod middleware and retrieve it from a Handler? - yesod

Before any handler code is executed, I need to load some information about the user, which is determined by the request cookies, and then access that in all of my handler code.
I've looked at the yesod-core codebase and found that I could override the yesodMiddleware function and provide a custom middleware which would load the data before the handler is executed.
The problem is that I don't know where to store this data, so that I can later retrieve it from the Handler.

You can use cached and cachedBy for per-request caching.

Related

How to log api calls into databse using django?

I don't know how to log all the API calls like API name, API call time, API call success/fail status into my sqlite3 database. Please help me I have no idea about this.
You can try with this Stack Overflow question. It creates a middleware which is a layer that interacts with every incoming request and every returning response. It can be used to perform certain action for incoming request - in this case save request details to the database.

In the backend, can you access data from previous requests?

This is more of a theory question, so I'm not going to post any code.
On the frontend, the user types in a search command. On the backend (Django in my case), it hits an API, the results of the search are saved into a Django View in views.py. On the frontend, the user interacts with this returned data and sends another request. On the backend, is the data from the first Django View still available for use? How do you access it?
(The data is also in the frontend and I can send it with the second request. But if it's still stored on the backend then I wouldn't need to.)
HTTP by it's own nature is a stateless protocol. It does mean that protocol doesn't know what or when should happen any request. Request comes and your API just reacts to this request by your implemented logic.
If you want to persist/save any state/data on your API side, you can do it by persisting them to database or saving to any local/global variable. Then you can access this saved state/data while recieving other requests to your back-end and implement the logic to use of previous state with the new incoming data.

In Django, what would be the proper way to persist session information?

I am designing a REST app in django, which I intend people to use without a browser (just direct API calls with curl or whatnot). I have several different views where I want to pull in information about the session based on values that may have been a acquired from previous calls to other views. It seems that every time a view is called the "request" object passed in is an entirely new session, so I'm wondering how I can persist values the "correct" way?
Example code:
def login(request):
...
##I want to assign a token value to this session that is persisted to the entity requesting it
request.session['token'] = response.json()['auth']
...
def grabSomeValues(request):
...
##I want to grab the session token value in here but of course the request object in the case is a completely new one that does not have that token value it seems
print(request.session['token']
....
I think Middleware would help you.
The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis.
https://docs.djangoproject.com/en/2.2/topics/http/sessions/

How to act upon user registration (onRegister)

I'd like to perform some actions when a new user registers to my Yesod app.
What is the way to hook into that event?
Assuming your user registration process is performed by a POST request to your application, you can hook into handler function, that handles this request.
Assuming you use the usual scaffold layout, the info which function that is can be found in the file config/routes. The name convention is to prepend "post" to the name of the route (i.e.: RegisterUserR -> postRegisterUserR).

How do I redirect and pass my Google API data after handling it in my Oauth2callback handler on Google App Engine

My Oauth2Callback handler is able to access the Google API data I want - I want to know the best way to get this data to my other handler so it can use the data I've acquired.
I figure I can add it to the datastore, or also perform redirect with the data. Is there a "best way" of doing this? For a redirect is there a better way than adding it to query string?
I think I found a better way of doing it, I just use the oauth callback to redirect only with no data, and then on the redirect handler I access the API data.