| 1 | # ************************************************************************* | ||
| 2 | # Copyright (c) 2014-2022, SUSE LLC | ||
| 3 | # | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # | ||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||
| 14 | # documentation and/or other materials provided with the distribution. | ||
| 15 | # | ||
| 16 | # 3. Neither the name of SUSE LLC nor the names of its contributors may be | ||
| 17 | # used to endorse or promote products derived from this software without | ||
| 18 | # specific prior written permission. | ||
| 19 | # | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | # ************************************************************************* | ||
| 32 | |||
| 33 | package Web::MREST::WebServicesIntro; | ||
| 34 | |||
| 35 | use 5.012; | ||
| 36 | use strict; | ||
| 37 | use warnings; | ||
| 38 | |||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | Web::MREST::WebServicesIntro - General discussion of REST and Web Services | ||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | =head1 GENERAL DISCUSSION OF REST AND WEB SERVICES | ||
| 48 | |||
| 49 | Before you try to implement a REST server using L<Web::MREST>, you might want | ||
| 50 | to take a look at our "prerequisites". The heading of each subsection below | ||
| 51 | describes the prerequisite. However, the text under each subsection heading | ||
| 52 | should B<not> be taken as an authoritative discourse on the subject. | ||
| 53 | |||
| 54 | |||
| 55 | =head2 Know what Web Services are | ||
| 56 | |||
| 57 | A "Web Service" is a client-server application that uses the HTTP protocol for | ||
| 58 | communications between client and server. More specifically, the client | ||
| 59 | attempts to open a TCP connection to a pre-defined host and port where the | ||
| 60 | server is listening. Once a connection is open, the client and server | ||
| 61 | communicate in HTTP. | ||
| 62 | |||
| 63 | Web Services can run on any TCP/IP network - the public Internet is one | ||
| 64 | example, but many Web Services run on corporate intranets, for example. A | ||
| 65 | developer will typically have an isolated testing network on his own machine, | ||
| 66 | etc. | ||
| 67 | |||
| 68 | |||
| 69 | =head2 Know what a RESTful Web Service is | ||
| 70 | |||
| 71 | Before you write a REST server, you should probably learn what a REST server | ||
| 72 | is. Here is a crash course. | ||
| 73 | |||
| 74 | Even if you _think_ you know what a REST server is, it might be useful to | ||
| 75 | either skim this crash course or, even better, just read L<Leonard Richardson's | ||
| 76 | paper|http://www.crummy.com/writing/speaking/2008-QCon/act3.html> which this | ||
| 77 | "crash course" attempts to paraphrase. | ||
| 78 | |||
| 79 | |||
| 80 | =head3 Introduction | ||
| 81 | |||
| 82 | REST is an approach to implementing client-server software architecture, in | ||
| 83 | which communications between client and server use the HTTP protocol. It turns | ||
| 84 | out that HTTP is "good enough" for many applications, and using it can save a | ||
| 85 | lot of work. | ||
| 86 | |||
| 87 | I urge all prospective REST server developers to study and "grok" the | ||
| 88 | L<Richardson REST Maturity Model|http://www.crummy.com/writing/speaking/2008-QCon/act3.html>, | ||
| 89 | since it is the conceptual basis for this discourse. | ||
| 90 | |||
| 91 | |||
| 92 | =head3 More than a web server | ||
| 93 | |||
| 94 | Providing a Web Service implies having a web server. L<Web::MREST> does this | ||
| 95 | for you, with help from L<Web::Machine> and L<Plack>. | ||
| 96 | |||
| 97 | But the mere presence of a web server does not make a Web Service "RESTful". | ||
| 98 | |||
| 99 | |||
| 100 | =head3 Level 0: tunnelling mechanism | ||
| 101 | |||
| 102 | Some notorious Web Services - such as those based on the XML-RPC and SOAP | ||
| 103 | technologies - use HTTP as a tunnelling mechanism. In this paradigm, each | ||
| 104 | client message is serialized and sent to the server in the body of a C<POST> | ||
| 105 | request. The server always responds with a 200 status code, which in this case | ||
| 106 | signifies no more than that the message was received and processed, and the | ||
| 107 | server's serialized response is placed in the response body. | ||
| 108 | |||
| 109 | Richardson calls this "One URI, one HTTP method". | ||
| 110 | |||
| 111 | Example HTTP request: | ||
| 112 | |||
| 113 | Method: POST | ||
| 114 | URI: http://myapp.example.com/ | ||
| 115 | Header: Accept: application/json | ||
| 116 | Body: { | ||
| 117 | "command" : "employee.insert", | ||
| 118 | "arguments" : { ... } | ||
| 119 | } | ||
| 120 | |||
| 121 | Example HTTP response: | ||
| 122 | |||
| 123 | Status code: 200 OK | ||
| 124 | Content-Type: application/json | ||
| 125 | Body: { | ||
| 126 | "status" : { | ||
| 127 | "level" : "ERROR", | ||
| 128 | "code" : "MYAPP_INSUFFICIENT_PRIVS", | ||
| 129 | "text" : "Insufficient privileges" | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | To quote Richardson: | ||
| 134 | |||
| 135 | If you look at an XML-RPC service, or a typical SOAP service . . ., you'll | ||
| 136 | see something that looks a lot like a C library. There are a bunch of functions, | ||
| 137 | sometimes namespaced with periods. All of these functions are accessed by | ||
| 138 | sending a POST request to one single URI. | ||
| 139 | |||
| 140 | |||
| 141 | =head3 Level 1: resources | ||
| 142 | |||
| 143 | The next step, which Richardson calls "Many URIs, one HTTP method", involves | ||
| 144 | moving some part of the XML/JSON body into the URI. Though this step might seem | ||
| 145 | insignificant, calling it "revolutionary" would be closer to the truth. | ||
| 146 | |||
| 147 | Let's apply this to our example. If employees can be uniquely identified by | ||
| 148 | their nick, a request for employee "simona" might look like this: | ||
| 149 | |||
| 150 | Method: POST | ||
| 151 | URI: http://myapp.example.com/employee/nick/simona | ||
| 152 | Header: Accept: application/json | ||
| 153 | Body: { | ||
| 154 | "command" : "GET" | ||
| 155 | } | ||
| 156 | |||
| 157 | By moving the object specification to the URI, I<the object becomes a web | ||
| 158 | resource>, and this is what makes it "revolutionary". | ||
| 159 | |||
| 160 | The very purpose of the HTTP standard is to facilitate the publishing and | ||
| 161 | manipulation of web resources, and the URI is the "Uniform Resource | ||
| 162 | Identifier". Moving from level 0 to level 1 involves the same paradigm shift as | ||
| 163 | embracing OO principles in your code. | ||
| 164 | |||
| 165 | But even if you already were using OO principles in the underlying code, what | ||
| 166 | benefit is there in bundling the object identifier in the HTTP request body? | ||
| 167 | The Uniform Resource Identifier (URI) is the right tool for that. | ||
| 168 | |||
| 169 | |||
| 170 | =head3 Level 2: HTTP verbs | ||
| 171 | |||
| 172 | If you know about HTTP methods, the previous example should cry out to you (or, | ||
| 173 | rather, you might cry out to it): "why are they using C<POST> for a GET | ||
| 174 | request?!" And, while it may seem astonishing, that is exactly what many Web | ||
| 175 | Services do (or used to do before Richardson published his influential paper). | ||
| 176 | |||
| 177 | The next "level" in Richardson's structure involves leveraging HTTP methods to | ||
| 178 | distinguish read requests, which should be idempotent, from write requests, | ||
| 179 | which modify the underlying data. When this distinction is hidden in the API, | ||
| 180 | there is no way for client code to optimize read-only requests. | ||
| 181 | |||
| 182 | Illustrating with our example: | ||
| 183 | |||
| 184 | Method: GET | ||
| 185 | URI: http://myapp.example.com/employee/nick/simona | ||
| 186 | |||
| 187 | The barest glace is enough to make it obvious that this request is far simpler | ||
| 188 | than its level 1 equivalent. At level 2, the server guarantees that GET | ||
| 189 | requests will never change the data, and that means your client code can | ||
| 190 | dispense with whatever special precautions it needs to take to prevent unwanted | ||
| 191 | modifications. | ||
| 192 | |||
| 193 | Richardson's designation for this level is: "Many URIs, each supporting | ||
| 194 | multiple HTTP methods". Quoting Richardson again to drive the point home: | ||
| 195 | |||
| 196 | The web is powerful because it gives you tools for splitting the inherent | ||
| 197 | complexity of a task into small chunks. The URI lets you give a name to | ||
| 198 | every object in the system. With URIs, every object can be a little bit | ||
| 199 | complex. That's the URI level. On the HTTP level, the major advance of the | ||
| 200 | web is that although it can handle any kind of operation, it splits out | ||
| 201 | read operations, operations that want to fetch data, and treats them specially. | ||
| 202 | |||
| 203 | Taking our example a little bit further, let's say we want to create a new | ||
| 204 | employee at this level. Here's what the request might look like: | ||
| 205 | |||
| 206 | Method: PUT | ||
| 207 | URI: http://myapp.example.com/employee/nick/george | ||
| 208 | Header: Accept: application/json | ||
| 209 | Body: { | ||
| 210 | "name" : "George III", | ||
| 211 | "occupation" : "King of England" | ||
| 212 | ... | ||
| 213 | } | ||
| 214 | |||
| 215 | The important point here is that the request body now contains content only - | ||
| 216 | no command or function name. The role of the function name is taken over by the | ||
| 217 | combination of HTTP method and URI. | ||
| 218 | |||
| 219 | Now we are really using HTTP to its fullest potential. Or are we? | ||
| 220 | |||
| 221 | |||
| 222 | =head3 Level 3: hypermedia controls | ||
| 223 | |||
| 224 | Until this point, the discourse has been easy to follow. Yet, Richardson | ||
| 225 | describes a third level, "hypermedia", which he defines as: | ||
| 226 | |||
| 227 | Resources describe their own capabilities and interconnections | ||
| 228 | |||
| 229 | This is also sometimes referred to as "Hypermedia As The Engine Of Application | ||
| 230 | State", or HATEOAS. As Richardson himself acknowledges, this is where the | ||
| 231 | enthusiasm starts to fade. | ||
| 232 | |||
| 233 | According to Richardson, whereas level 1 is "the lesson of URIs" and level 2 is | ||
| 234 | "the lesson of HTTP", the lesson we learn at this level is "the lesson of | ||
| 235 | HTML". That is because HTML is an example of hypermedia controls that we are | ||
| 236 | all familiar with. Generalizing this, we can say that a HATEOAS client | ||
| 237 | "navigates" its server very much like a human surfs the web, that is: by | ||
| 238 | parsing and following links. Just like on the WWW, in a HATEOAS application, | ||
| 239 | resources link to other resources and, crucially, I<those links are expressed | ||
| 240 | as URIs>. | ||
| 241 | |||
| 242 | Returning to our example, let us say that our employee objects link to | ||
| 243 | occupation objects. Inside the database, each occupation is identified by its | ||
| 244 | "occupation_id", an integer value, and linked tables use this as a foreign key. | ||
| 245 | Without hypermedia controls, our request for employee "george" and the server's | ||
| 246 | response (the part following the '*') might look like this: | ||
| 247 | |||
| 248 | Method: GET | ||
| 249 | URI: http://myapp.example.com/employee/nick/george | ||
| 250 | * | ||
| 251 | Status code: 200 OK | ||
| 252 | Content-Type: application/json | ||
| 253 | Body: { | ||
| 254 | "name" : "George III", | ||
| 255 | "occupation_id" : 553, | ||
| 256 | ... | ||
| 257 | } | ||
| 258 | |||
| 259 | In HATEOAS, the same request/response might look like this: | ||
| 260 | |||
| 261 | Method: GET | ||
| 262 | URI: http://myapp.example.com/employee/nick/george | ||
| 263 | * | ||
| 264 | Status code: 200 OK | ||
| 265 | Content-Type: application/json | ||
| 266 | Body: { | ||
| 267 | "name" : "George III", | ||
| 268 | "occupation" : { | ||
| 269 | "link" : { | ||
| 270 | "href" : "http://myapp.example.com/occupation/catalog/553, | ||
| 271 | "rel" : "http://myapp.example.com/occupation", | ||
| 272 | "name" : "King of England" | ||
| 273 | }, | ||
| 274 | ... | ||
| 275 | } | ||
| 276 | |||
| 277 | While at first glance it seems more complicated, this approach (which we will | ||
| 278 | call the HATEOAS approach) is superior to the non-HATEOAS approach illustrated | ||
| 279 | by the first example. | ||
| 280 | |||
| 281 | In the non-HATEOAS version, the client code needs to know that occupation objects | ||
| 282 | are identified by their 'occupation_id' property. Further, to gain access to | ||
| 283 | the object it needs to know how to transform the occupation ID into the | ||
| 284 | appropriate resource so it can issue a GET request for it. | ||
| 285 | |||
| 286 | By putting the full URI of the occupation resource into the response, the client | ||
| 287 | no longer needs to know any of that. To get the resource, it directly issues a | ||
| 288 | GET request to the URI provided in 'href'. | ||
| 289 | |||
| 290 | But the "link" property gives us more than this. From the additional properties | ||
| 291 | the client can, for example, derive that the resource can be modified by | ||
| 292 | issuing a C<POST> request to C<http://myapp.example.com/occupation> and | ||
| 293 | including the "name" property (with the value "King of England") in the request | ||
| 294 | body. | ||
| 295 | |||
| 296 | The non-HATEOAS variant, by contrast, provides nothing more than a | ||
| 297 | number. The "knowledge" of what can be done with it must be embedded in the | ||
| 298 | client code. As Richardson notes, this makes client code more brittle. He cites | ||
| 299 | examples of RESTful Web Service projects where clients were abandoned | ||
| 300 | after being broken repeatedly by server-side changes to the REST API. | ||
| 301 | |||
| 302 | |||
| 303 | =head3 Conclusion | ||
| 304 | |||
| 305 | There is more that can be done with the HATEOAS approach, of course, than | ||
| 306 | provide URI links in the HTTP response. The idea is for clients to get | ||
| 307 | information on their state from the server via HTTP. This should make the | ||
| 308 | clients less prone to breakage when changes are made on the server side. | ||
| 309 | |||
| 310 | =cut | ||
| 311 | |||
| 312 | 1; | ||
| 313 |