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