I am a very beginner writing one of my first webapps. I'm using FastAPI and I'm stuck on the logic of creating an endpoint that has to do a lot of things before it returns something back to the user. Since I'm new I also lack a lot of the vocabulary that I think I'd have if I were more experienced so bear with me:
To start -- What is my web app doing?
Well, I am trying to create something that will pull a schedule from a 3rd party API and then show it to me, allowing me to book things from my webapp rather than having to navigate to the 3rd party APIs. Some of these events are recurring (or are assumed to be) and get recorded as a "preference"
My approach so far, using 'MVVM'
User navigates to
www.myfakeurl.com/schedule
(either directly or from the home page)This sends a request to the
/schedule
router
(A) view model task 1: call 3rd party API and wait for a response
- In the ScheduleViewModel, there's no payload to check, so I send a request using the current user's email + token to access the 3rd party API's own schedule page (this is done by
get_current_schedule()
inservices/3p_schedule.py
)
(B) view model task 2: validate / parse the 3rd party API response
- Back in ScheduleViewModel, I validate the response. The 3rd party API sends over a lot of stuff I don't need, so I just keep a few relevant pieces of info (like booking_id, if I already am signed up for a class, etc)
(C) view model task 3: compare schedule against user preferences database table and note any overlap
- Still in ScheduleViewModel, I quickly check against the UserPreferences table (in the SQLAlchemy DB) if any classes in the schedule match my preferences and create a
is_a_preferred_time
attribute for each class for when I add the schedule info to my database
(D) view model task 4: construct a dict from steps 4 and 5 and return it
Still in the ScheduleViewModel, I finally combine the dict of info from step
4
with my info about user preferences from step5
. The schedule is aList[Dict[str, dict]]
where the outer dict has thedate
and the inner dict keys areuser_id
(from step3
),time
(from 3rd party API),is_preferred
(from UserPreferences table),booking_id
(from 3rd party API),currently_enrolled
(from 3rd party API)I use the dict to populate a template.
My concerns and confusions
TLDR -- Basically, the goal of hitting the /schedule
endpoint is to return an exercise class schedule from the 3rd party API + some info regarding user preferences (eg "Wednesdays at 7:30 am") if it's in the schedule (eg, maybe a different font color if there's a class time that matches your preferences)... Given that this is the end goal, it takes a few steps to get there and I am wondering if (A)
, (B)
, (C)
, and (D)
noted in bold font results in the view model violating the single responsibility principle, and, if yes, if that could be solved by simply abstracting away much of the logic of A-D into a separate function outside the view model
I started off using pydantic for a lot of this, and then I learned about MVVM and got confused and just went with MVVM since I don't know what I am doing and it seemed more clear.
But, this feels like a lot for one endpoint to handle ? Or maybe the examples in all my tutorials are just very basic and real-life is closer to what I have going on?
I apologize in advance for just how clueless I am, but I don't have anyone to ask about this and am looking for any and all guidance / resources. I think I am in a very steep section of the learning curve atm :)