Starlette Routing / adding additional REST API endpoints

354 views
Skip to first unread message

Christian König gen. Kersting

unread,
Aug 18, 2021, 9:22:30 AM8/18/21
to oTree help & discussion
Dear community,

I am trying to add additional endpoints to the oTree API and I am struggeling with the  routing in starlette. 

I tried adding a starlette Route to the list of routes imported from otree.urls which fails because of circular imports.

I also tried to import the starlett app from otree.asgi in order to call app.add_route(...) (see: https://github.com/encode/starlette/blob/master/starlette/applications.py), but that also leads to a circular import.

Has anyone figured out how to add additional Routes to oTree 5 and would be so kind to share the solution?


Best regards,
Christian

Chris @ oTree

unread,
Aug 18, 2021, 9:33:46 AM8/18/21
to "Christian König gen. Kersting", oTree help & discussion
I haven’t designed the URL routing system to be extensible. 

Which types of endpoints are you trying to add?

Sent from my phone

On Aug 18, 2021, at 7:22 AM, Christian König gen. Kersting <koenig....@gmail.com> wrote:

Dear community,
--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/a8a96b6c-af34-4dd5-88c5-e41db59fee73n%40googlegroups.com.

Christian König gen. Kersting

unread,
Aug 18, 2021, 10:00:44 AM8/18/21
to oTree help & discussion
Hi Chris,

Thank you for the quick reply. 
I was trying to add a data export endpoint but have since realized that simply requesting "/ExportSessionWide/<session_code>" does the trick. (By the way: This seems to work even when not logged-in and with OTREE_AUTH_LEVEL set to "STUDY").

In the past, I have used additional views to trigger payoff calculations that depend on all finished participants. For example, pay a bonus to participants who are better than the median participant in some game. In online experiments having a "wait for all" page before calculating the payoffs is hardly an option. Thus, I would implement a custom admin report where I could manually mark a session as "finished" and trigger the payoff (re-)calculation for all finished participants when a suitable number of participants had completed the experiments. As the number of participants who finish experiments is quite uncertain, I preferred this approach to hard coding a minimum number of participants to finish before triggering the payoff calculation for everyone.

I think it would be great if a future version of oTree would again allow us to somehow extend the routing system (also for Websockets). Maybe a single check for the presence of a routing.py or urls.py in an app folder would be enough to add to oTree-core.

Related to the API, it would also be nice to be able to specify webhooks for certain events. This would allow third-party applications to react to participants reaching the last page of the experiment for example. I imagine this could also be useful for applications like otree-hr.

Thank you,
Christian

Chris @ oTree

unread,
Aug 19, 2021, 5:12:44 AM8/19/21
to oTree help & discussion
On Wednesday, August 18, 2021 at 8:00:44 AM UTC-6 koenig....@gmail.com wrote:
Hi Chris,

Thank you for the quick reply. 
I was trying to add a data export endpoint but have since realized that simply requesting "/ExportSessionWide/<session_code>" does the trick. (By the way: This seems to work even when not logged-in and with OTREE_AUTH_LEVEL set to "STUDY").

Hi, yes this URL is available currently, although I would not recommend relying on it for a long-term project, because it's just part of the UI, not the API.
It's intentional that it's available without authentication because it requires knowing the session code, which is not exposed to participants.
But anyway, I just made a change locally to add an extra measure of security by requiring an auth token in the URL. (by the way this will change the URL, which is an example of how it does not have API-level stability).
 

In the past, I have used additional views to trigger payoff calculations that depend on all finished participants. For example, pay a bonus to participants who are better than the median participant in some game. In online experiments having a "wait for all" page before calculating the payoffs is hardly an option. Thus, I would implement a custom admin report where I could manually mark a session as "finished" and trigger the payoff (re-)calculation for all finished participants when a suitable number of participants had completed the experiments. As the number of participants who finish experiments is quite uncertain, I preferred this approach to hard coding a minimum number of participants to finish before triggering the payoff calculation for everyone.

Thanks for the suggestions! Noted.
  
I think it would be great if a future version of oTree would again allow us to somehow extend the routing system (also for Websockets). Maybe a single check for the presence of a routing.py or urls.py in an app folder would be enough to add to oTree-core. 

Related to the API, it would also be nice to be able to specify webhooks for certain events. This would allow third-party applications to react to participants reaching the last page of the experiment for example. I imagine this could also be useful for applications like otree-hr.

How about something like this:

def before_next_page(player, timeout_happened):
  requests.post('http://my-other-website.com', {'participant_finished': player.participant.code})

Björn Hamt

unread,
Aug 30, 2021, 4:26:32 AM8/30/21
to oTree help & discussion
In the past, I have used additional views to trigger payoff calculations that depend on all finished participants. (…). Thus, I would implement a custom admin report where I could manually mark a session as "finished" and trigger the payoff (re-)calculation for all finished participants when a suitable number of participants had completed the experiments.

Dear Christian,

I am facing a similar problem where I need to calcuate subject's payoff after a session is finished. Could you share some code example or snippes on how you implemented this with a custom admin report?

Best,
Björn

Reply all
Reply to author
Forward
0 new messages