Effective customer interaction RESTful

I have, apparently, a fairly simple question about implementing a data access client that strictly adheres to the architectural principles of REST. First, suppose I have a well-managed REST API that I want to use with a Django application. I will start by opening the available services (edited for later observation) :

GET example.com/services/ HTTP/1.1

HTTP/1.1 200 OK
<?xml version="1.0" encoding="UTF-8"?>
<services>
  <service>
    <name>Widgets</name>
    <link>http://example.com/services/widgets/</link>
    <item_link>http://example.com/services/widgets/{widget_id}/</item_link>
  </service>
  <service>
    <name>Factories</name>
    <link>http://example.com/services/factories/</link>
    <item_link>http://example.com/services/factories/{factory_id}/</item_link>
  </service>
  ...
</services>

Now, since I am building a Django application based on the consumption of this API, how will I continue to learn these services RESTfully? To adhere to the principles of REST, my application must be managed by the resulting hypermedia. I believe that the first step is quite simple - interaction with the service by name. I created a Django view as follows:

def get_service(request, service_name):
    doc = etree.parse(urllib.urlopen('http://example.com/services/'))
    uri = doc.xpath("service/name[.='%s']/following-sibling::*" % service_name)[0].text
    ...

( ):

GET example.com/services/widgets/ HTTP/1.1

HTTP/1.1 200 OK
<?xml version="1.0" encoding="UTF-8"?>
<widgets>
  <item_link>http://example.com/services/widgets/{widget_id}/</item_link>
  <widget>
    <id>1</id>
    <name>Whizbang Foobar</name>
    <link>http://example.com/services/widgets/1</link>
  </widget>
  ...
</widgets>

Django. , , RESTfully? , , , , - Django URI .

, , REST, , , . , API REST "", , .

.

:

( URI) ? ( ) Django :

def get_item(request, service_name, item_id):
    doc = etree.parse(urllib.urlopen('http://example.com/services/'))
    uri = doc.xpath("service/name[.='%s']/following-sibling::item_link" % service_name)[0].text
    ...

:

GET example.com/services/widgets/1 HTTP/1.1

HTTP/1.1 200 OK
<?xml version="1.0" encoding="UTF-8"?>
<widget>
  <id>1</id>
  <name>Whizbang Foobar</name>
  <tags>foo bar baz ham eggs</tags>
  <index_link>http://example.com/services/widgets/</index_link>
</widget>
+3
4

, , REST, , , . , API REST "", , .

, , Sun Cloud API. , , , -, .

, API. , , , API .

. HATEOAS , , , . , , , .

, "", . , , ..

, "Item". , / .. Widget : .

, Index. , Media. , Widget, , , Item. ( ) , .

mime , .

, , .

+3

, REST , . REST.

, REST HTTP. , . , RESTful.

RESTful , . , REST -, , , . - , . RESTful , .

, REST , .

REST , -, , RESTful URL- !

+3

, , ?

, , , OPTIONS "widget", , HTTP- ( Allow ). < > URI, <link rel="whatever">. , , , GET, ...
, .

, , , ( ), , <link rel="whatever">, , , , . "" ", , , .

+1

Sun Cloud API - API RESTful, .

+1

Source: https://habr.com/ru/post/1714279/


All Articles