RUHE? Nimm einen dummen JSON-RPC

In letzter Zeit gab es auf HabrĂ© viele Kontroversen darĂŒber, wie die REST-API richtig vorbereitet werden kann.

Anstatt in den Kommentaren zu toben, denken Sie: Brauchen Sie wirklich ĂŒberhaupt REST?
Ist das eine bewusste Entscheidung oder Gewohnheit?

Vielleicht ist Ihr RPC-Ă€hnliches API-Projekt am besten geeignet?

Was ist JSON-RPC 2.0 ?
Dies ist ein einfaches zustandsloses Protokoll zum Erstellen einer API im Stil von RPC (Remote Procedure Call).
Es sieht normalerweise wie folgt aus.

Sie haben einen einzelnen Endpunkt auf dem Server, der Anforderungen mit einem Textkörper des Formulars akzeptiert:

{"jsonrpc": "2.0", "method": "post.like", "params": {"post": "12345"}, "id": 1}

:

{"jsonrpc": "2.0", "result": {"likes": 123}, "id": 1}

— :

{"jsonrpc": "2.0", "error": {"code": 666, "message": "Post not found"}, "id": "1"}

!

batch-:

Request:

[
  {"jsonrpc":"2.0","method":"server.shutdown","params":{"server":"42"},"id":1},
  {"jsonrpc":"2.0","method":"server.remove","params":{"server":"24"},"id":2}
]

Response:

[
  {"jsonrpc":"2.0","result":{"status":"down"},"id":1}
  {"jsonrpc":"2.0","error":{"code":1234,"message":"Server not found"},"id": 2}
]

id API , .

«» — «id», :

{"jsonrpc":"2.0","method":"analytics:trackView","params":{"type": "post", "id":"123"}},

, , .
— . , .

RPC-, npmjs.com, :

client.request('add', [1, 1], function(err, response) {
  if (err) throw err;
  console.log(response.result); // 2
});


-

-, HTTP- URI.

, API .
— -, gamedev, realtime-.




"↑" — , , .

post.like(id) PUT /posts/{id}/likes?

CQRS, RPC- API .

-, HTTP , -, .

- 200-, - , HTTP-.

JSON-RPC integer — .

JSON-RPC — ,

.
:
RESTRPC
URI---
GET----
HTTP----

:
RESTRPC
HTTP----
HTTP----
( )( )


POST /server/{id}/status PATCH /server/{id}?
. POST /api.

best practices , .
, .

, REST API . 


, Github API, reactions issue?
Accept: application/vnd.github.squirrel-girl-preview
? , . .

HTTP

, REST API HTTP.
-.

JSON-RPC over HTTP JSON-RPC over Websocket. TCP.
JSON-RPC , .

- (HTTP).

HTTP 404
RESTRPC
---
APIAPI




JSON-RPC , :
— Batch-
— ,
—

, JSON-RPC. — .


HTTP-

API HTTP — RPC .
, , read-only API.
- .

API «» «» — .

access.log

JSON-RPC API - .
.



JSON-RPC swagger.io.
apidocjs.com, .
, API markdown-.

Stateless

«REST»  — ,   HTTP — . .

, , HTTP .

, RPC API. «Stateless».
, , - . .
RPC API -, , .

stateless API, ? - statefull   —  FTP.

: [ TCP-]
: 220 ProFTPD 1.3.1 Server (ProFTPD)
: USER anonymous
: 331 Anonymous login ok, send complete email address as your password
: PASS user@example.com
: 230 Anonymous access granted, restrictions apply
: CWD posts/latest
: 250 CWD command successful
: RETR rest_api.txt
: 150 Opening ASCII mode data connection for rest_api.txt (4321 bytes)
: 226 Transfer complete
: QUIT
: 221 Goodbye.


. FTP- , , , «» .

API , .  .


JSON-RPC 2.0, RPC API HTTP -.
, , , ?

GraphQL, .

gRPC - (), .

REST, . , , .

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


All Articles