Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Fine-tuning your REST service
Lecture: A small refactoring

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, we are ready to go in production, but before we do that, we probably want to do some little refactoring.
0:07 What I want to do here is implement the separation of concerns design principle which I find to be super important
0:15 when we're building some larger website or web service. So what I did here is I created an authentication script
0:23 and I moved the authentication class there. Then I also created a callbacks script and I moved my callback functions right there
0:35 and again, I created the validation script where I moved my validator class. So my app script, which, remember, is our launch script
0:44 now is simply importing these features from their own modules, and then we are instanciating our Eve class passing the validator,
0:55 the redis instance, if we need to, we can pass our authentication class. And yes, we still have this custom route here
1:08 we could eventually move it elsewhere in its own module, if we had more than one custom endpoint, probably we might want to do that.
1:19 And then, we are simply attaching our callback function to our app. So the launch script only concentrates on creating the app
1:28 and preparing it for launch. And by the way, this separation of concerns principle can also be applied with success to the settings file
1:39 and specifically to the domain. Look at what I've done here. I created a folder, I called it domain and within the domain folder,
1:49 I have two modules, people which contains the endpoint definition for of course, the people endpoint,
1:56 with the schema and optional rules, like authentications or disabling hateoas, And then I have a works.py where I'm just defining the works endpoint.
2:09 Now, look at this file __init__.py, this is not a regular folder, this is a Python package, actually.
2:18 And here I'm importing the two definitions from people and works, and I am creating the domain dictionary.
2:27 So what I'm doing is allowing the people and works definition to live in their own modules
2:34 so they can grow over time without polluting the settings file. Since domain is a package in my settings file,
2:42 all I have to do is import the domain dictionary here and then my settings file is clean from endpoint definitions
2:50 and all it has to store are global settings for my API.


Talk Python's Mastodon Michael Kennedy's Mastodon