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,
0:02 but before we do that, we probably want to do some little refactoring.
0:06 What I want to do here is implement the separation of concerns design principle
0:12 which I find to be super important
0:14 when we're building some larger website or web service.
0:18 So what I did here is I created an authentication script
0:22 and I moved the authentication class there.
0:25 Then I also created a callbacks script
0:30 and I moved my callback functions right there
0:34 and again, I created the validation script where I moved my validator class.
0:39 So my app script, which, remember, is our launch script
0:43 now is simply importing these features from their own modules,
0:49 and then we are instanciating our Eve class passing the validator,
0:54 the redis instance, if we need to, we can pass our authentication class.
1:04 And yes, we still have this custom route here
1:07 we could eventually move it elsewhere in its own module,
1:12 if we had more than one custom endpoint, probably we might want to do that.
1:18 And then, we are simply attaching our callback function to our app.
1:22 So the launch script only concentrates on creating the app
1:27 and preparing it for launch.
1:29 And by the way, this separation of concerns principle
1:35 can also be applied with success to the settings file
1:38 and specifically to the domain.
1:41 Look at what I've done here.
1:43 I created a folder, I called it domain and within the domain folder,
1:48 I have two modules, people which contains
1:52 the endpoint definition for of course, the people endpoint,
1:55 with the schema and optional rules, like authentications or disabling hateoas,
2:02 And then I have a works.py where I'm just defining the works endpoint.
2:08 Now, look at this file __init__.py,
2:12 this is not a regular folder, this is a Python package, actually.
2:17 And here I'm importing the two definitions from people and works,
2:22 and I am creating the domain dictionary.
2:26 So what I'm doing is allowing the people and works definition
2:31 to live in their own modules
2:33 so they can grow over time without polluting the settings file.
2:37 Since domain is a package in my settings file,
2:41 all I have to do is import the domain dictionary here
2:45 and then my settings file is clean from endpoint definitions
2:49 and all it has to store are global settings for my API.