DrupalCon Prague 2013

REST y serialización en Drupal 8

Lin Clark, Klaus Purer  · 

Presentación

HTML (pincha para descargar)

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

okay I think we're going to get started because we have a lot to talk about but there will still be some people trickling in from lunch I'm sure so so we're here to talk about reston serialization in Drupal 8 i'm lynn clark i'm a core module

maintainer for the serialization module also the hal module which i'll be talking about in this presentation and another module or two and I recently joined the tag one consulting team of top-notch performance engineers and this is Klaus Klaus puter he

is a developer Epico and has been working on rest since Drupal 7 he did the rest WS module and has brought a lot of those principles to Drupal 8 with the rest module ok also just want to give a heads up we are getting into some details of how things are implemented

in Drupal 8 which means that there's actually a lot of code in these slides if you're interested in those details you might want to move forward although this is a pretty large screen so maybe that's not a problem but there's also these slides

are available on the web they're updated each time we give these this presentation with any API changes or anything that's happened since the last time we gave it so you can go there to see the slides at any time so first I want to talk a little bit

about what rest is you know people are super excited about rest and Drupal 8 it has been talked about a lot but i'm not sure that everyone's on the same page as to what rest actually is so I want to just give a basic intro to just make sure that we

all are on the same page or using the same definitions so I'm first going to start with some really boring dry Wikipedia definitions wikipedia says web services a web service as a method of communication between two electronic devices over the world wide

web so that's a really boring way of saying it's what you do every single day when you're using the web you know say that you're using your phone to get maps because you're at Drupal pond and you have no idea where you're going so your

phone in this case is a client to a server like the open street maps server and you're sending requests you're communicating with that server you're saying you know get Portland map data or get Prague map data or you're saying add a new road

to this data because open street maps is actually both read and write capable so that's what a web service is you're actually pretty familiar with it I'm sure and wikipedia says rest is a style of software architecture for distributed systems such

as the World Wide Web so that's basically saying it's one way of doing web services there are actually other ways of doing web services you may have heard of soap xmlrpc and so it's important to know that restful web services it's just one

style and to know the differences between there are some key differences that it's important to note between these different styles of web services and they mentioned the world wide web here that's because the guy who came up with rest as a way of

describing software architecture he was actually one of the people who worked on HTTP so he was working on HTTP took a step back to write his thesis and kind of generalize the ideas from that early web development and generalize them into a general architecture

so I'm just going to show what we have in Drupal 8 right now you could test this on your own this is the most basic rest request you could use to interact with drupal's web services so first we go to entity node 1 and we run a get request I'm using

a browser extension called dev HTTP client you can get this for crow or other browsers and there are other ones that you can use a wheel so you enter the URI hear you say that you want to get that resource and then you add an accept header so you're going

to accept application JSON and then you get back the node in JSON so let's go over the four basic components of this really simple request there's the base form at the HTTP verbs the URI and the media type also sometimes known as the mime type so the

base for matt i'm sure you're all familiar with base formats for example json is an example of a base format xml is another example it's basically just the way that you take information and put it into a string that can be sent along the network

your eyes are used in two ways in rest services so you use your eyes to indicate resources that you're interacting with these are the things that you want to you know modify or create so in Drupal 8 right now if you were interacting with node one as your

resource you would have entity / no / one as your resource URI we hope to change that Klaus can give an update on where that is but we want it to actually be the same URI that you interact with when you're interacting with the HTML page for that resource

and then another way that you use your eyes is linked relations and that's basically the way that these resources are related to each other so for example you might have a link relation between anode and the taxonomy terms that are it is tagged with so

that would be one example relation I'll get into link relations more towards the end of this talk but I want to give an example of the restful use of your eyes and contrast that with what is not restful so what's not restful is if you post to an end

point and you have your identifiers actually in your body your mum in the content body so that you say you want to interact with node three you would have your identifier in the actual body instead what is restful is to actually post directly to the resource

URI for node 3 or thing 3 so media types people often get media types confused with base formats one of the reasons for this you know people think that JSON is a media type which it actually is it is a media type but in addition you can have more specific

media types that build on base formats so how is one that we're using in Drupal 8 and all that Hal does see now saying that you're using application helpless JSON all that does is indicate that there are a couple of extra bits of semantics you know

the consumer knows that there might be this links keyword or this there might be this embedded keyword that they can use to get some extra information and so it's really just a contract between the client and the server as to what the extra information

contained inside might be so you can have these media types that are specified on top of base formats these will often go through standardization bodies such as the IETF w3c or you can even have vendor specific media types so github has their own media type

and and there are special rules they say all resources may have one or more wild card score URL properties linking to other resources now I think it's actually a lot nicer just to have the links and embedded keywords rather than having to you know do a

regex on keys but any consumer that actually understands the github media type will know that they have to regex the keys and will know how to do that and so some of the people who are into pragmatic rest will say that to indicate your media type you should

just append the file extension to the end but that gets a little bit messy when you start using these more specific media types when you're not just using base formats and so instead a restful way to do it is to use the HTTP headers that are actually meant

to communicate this information so you have the accept header which you use when you're doing a get request basically you can specify what things you accept what formats you accept and understand and then if you're actually sending some data up to

a server use the content type because you know what the media type that you wrote that in is HTTP methods these are how you indicate how you want to interact with your resource what you want to do so for example we have implemented for of the standardized

while one is not quite standardized but we've been to implemented for HTTP methods in Drupal so the first one is yet and what get basically means is go fetch this page and bring it back to me don't change it it's important to implement get in a

way where if somebody sends a get request your site you aren't actually changing things you aren't actually deleting nodes or doing anything like that post it doesn't have quite as strict semantics is get but most people use it in the same way

here's some data go make a thing for me you know basically you're creating something new patch is what we're using in Drupal to say here are some change fields go update them on the existing thing another HTTP verb you can use to indicate that

is put but we for very specific reasons have chosen to use patch instead we may remember to talk about that later if we don't remember to talk about it feel free to ask about that at the end if you're interested and then delete you know get rid of

that thing pretty simple and so a non restful way to indicate what you want to do is to actually have your verb in the URL to say you know you want to get node 1 or you want to add a node instead you should use the HTTP methods to indicate what interaction

you want to have with this resource so let's look at how these four basic components came together for that really basic request that we did earlier so we had the base format which is JSON we had the URI which is entity node 1 we have the HTTP method which

is get and the media type which is application JSON so let's just change a couple of those things we're going to change the URI that we're interacting with the HTTP method to post and the media type to application how plus JSON and so if we go

through this change our your I our HTTP verb we're using the content-type header here instead of the accept because we're posting and then we run that request you'll see that there's a tool one created that shows up here that's a status

code it's a way of communicating from the server back to the client you know what happened when you sent this request this data up there what happened on the other end so 201 create is a good thing if you're posting that means it was successful and

[ ... ]

Nota: se han omitido las otras 5.027 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.