Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Your first Eve service
Lecture: Full range of CRUD operations

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So far, we've been working at the people endpoint which means that all the commands that we issue against this endpoint
0:09 are relative to the word collection, so when we send a get request we get back an array of documents,
0:16 if we do a delete operation and pay attention to this one, we are going to delete all the documents in the people collection.
0:25 When we do a post, we are adding a new document to the collection, but what if we want to edit a single document?
0:33 Well, then we have to eat the document endpoint and the document endpoint by default is simply the general endpoint / the document id
0:42 as we can see here in the ahref for the single document, so if I do a get at this endpoint which is people and document with the id, and it is a get,
0:59 what I get back is the full document as you can see, we have the unique id, of course, the data, metafileds, etags, and if you look at the links,
1:10 we now get the parent, self, and also the collection relative to the document. Now that we know how to access a single document
1:21 we can talk about edit operations on the document. Now, typically you have three kinds of operations that you want to do besides reading the document
1:31 one is editing single fields maybe or multiple fields. The second one is replacing the document completely with a new copy of it.
1:40 And the third, of course, is deletion of the document. So, in Rest world, when you want to do an edit operation, what you issue is a patch command.
1:51 When you want to replace a document you use the put command, and when you want to delete, off course, you use the delete command.
2:00 Now, all of these three commands are supported by Eve. We can try, for example, an edit operation, so let's try a patch,
2:10 we go to the body of the request and we say that we want to add the last name to our document, So the document is existing already,
2:21 we simply want to edit it by adding a new field, we already added the last name to the schema for this endpoint, so we should be good to go.
2:33 But, if we try this patch operation on the document we get an error, and we already saw this error back when we were working on the people endpoint
2:45 and the collection endpoint, as you might recall, and the reason why it doesn't work here at the document endpoint,
2:52 is the same reason it didn't work at the collection endpoint, which is— Eve by default is a read-only,
2:59 so yes, we did enable write operations on the collection endpoint, but if you remember, we only enabled the get, post and delete commands,
3:09 patch is a document command and we still need to enable it, and the same holds true for, off course, put and delete, so let's see how we can fix this.
3:22 Back to our Code editor, and as you see, here in the comment of our resource method is the methods allowed at the resource or collection endpoint.
3:33 What we need now is a new keyword and it is item methods, this is again, an array of methods allowed at the document or item endpoint.
3:46 By default, this is the value of this setting, we can of course add edit commands like patch, put and delete.
4:04 Again, get is read, patch is edit, put is replace and delete is, well, delete. Let's save them, and relaunch our server.
4:17 Now if we go back to our client and try our patch command, we get a different error message, but this is expected.
4:32 If we look at the error message, at the human readable message we have a clear explanation of what's going on,
4:39 to edit a document, its etag must be provided using the if match header. So what this is telling us is that this request is not complete.
4:50 Something is missing, and what is missing is an if match header, and the value of this if match header is supposed to be the etag for the document.
5:02 Now we don't have it right now, because we attempted a patch but we need to go back, get our document,
5:11 copy the etag, and now that we have it, we can try again our patch, and provide a valid value for the if match header. Okay, let's try again.
5:24 This time we got our patch in and the document was updated. If we go and do a get of the document,
5:31 we now see that it has both, the first name and the last name. So our patch went in. So what is this if match check all about?
5:41 Well, this is concurrency control work. We want to make sure that only the client with the exact same etag
5:50 as the one stored on the server can perform an edit operation. So the if match header check is there to prevent old versions of the document
6:01 to overwrite the newest one stored on the server. This is a powerful feature, you probably want to keep it enabled,
6:09 but if you don't like it, you want to adopt let's, say, an optimistic approach over concurrency.
6:16 You can simply disable your concurrency control feature, you do that by going back to your editor and simply setting if match to false,
6:28 . If you do this, you need to add an if match header every single time you perform an edit operation on a document.


Talk Python's Mastodon Michael Kennedy's Mastodon