Services REST

En règle générale, la seule méthode qu'un service doit implémenter est self.handle. Cependant, en utilisant des méthodes dédiées, il est possible de faire en sorte qu'un service réagisse uniquement à des verbes HTTP spécifiques - ce qui facilite le développement de services basés sur REST.

La convention à utiliser est handle_VERB où VERB est un verbe HTTP qu'une méthode donnée doit traiter, par exemple http_GET ou http_POST, comme ci-dessous:

# stdlib
from httplib import NO_CONTENT

# Zato
from zato.server.service import Service

class MyResource(Service):

    def handle_GET(self):
        self.response.headers['Content-Type'] = 'application/vnd.example.customer'
        self.response.payload.data = '{"cust_name":"Mike Drums"}'

    def handle_DELETE(self):
        # Delete the resource here..
        pass

        # And produce the correct status
        self.response.status_code = NO_CONTENT

Maintenant, après avoir monté le service sur un canal REST, il est possible de l'invoquer en utilisant soit GET soit DELETE, mais pas une autre méthode - si une méthode non supportée est utilisée par les clients, HTTP 405 Method Not Allowed est retourné automatiquement.

$ curl -v localhost:11223/my/resource/123 ; echo
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 11223 (#0)
> GET /my/resource HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:11223
> Accept: */*
>
< HTTP/1.1 200 OK
* Server Zato is not blacklisted
< Server: Zato
< Date: Tue, 11 Nov 2014 14:50:56 GMT
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/vnd.example.customer
< X-Zato-CID: 8c3840e8dda4ffa8858a01e6
<
* Closing connection 0
{"cust_name":"Mike Drums"}
$
$ curl -v -XDELETE 127.0.0.1:11223/my/resource/123
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 11223 (#0)
> DELETE /my/resource/123 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:11223
> Accept: */*
>
< HTTP/1.1 204 No Content
* Server Zato is not blacklisted
< Server: Zato
< Date: Tue, 11 Nov 2014 15:13:27 GMT
< Connection: close
< Content-Type: application/json
< X-Zato-CID: 4885abb5c6b854f0502c7b71
<
* Closing connection 0
$
$ curl -v -XPOST 127.0.0.1:11223/my/resource/123
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 11223 (#0)
> POST /my/resource/123 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:11223
> Accept: */*
>
< HTTP/1.1 405 Method Not Allowed
* Server Zato is not blacklisted
< Server: Zato
< Date: Tue, 11 Nov 2014 15:14:45 GMT
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json
< X-Zato-CID: 63551ccc564a17dfba1a5362
<
* Closing connection 0
$

Notes:

  • Vous pouvez simplement implémenter handle ou un ensemble d'une ou plusieurs méthodes handle_* pour chacun des verbes nécessaires, c'est-à-dire que si vous implémentez l'une des méthodes handle_*, la méthode handle elle-même sera ignorée.
  • Les méthodes handle_* sont entièrement compatibles avec toutes les autres fonctionnalités de Zato, telles que les canaux REST et les hooks.