SISA? Ambil JSON-RPC bodoh

Baru-baru ini di Habré, ada banyak kontroversi mengenai cara mempersiapkan REST API dengan benar.

Alih-alih mengamuk dalam komentar, pikirkan: apakah Anda benar-benar membutuhkan REST sama sekali?
Apakah ini pilihan atau kebiasaan yang disadari?

Mungkin proyek API seperti RPC Anda yang paling cocok?

Jadi apa itu JSON-RPC 2.0 ?
Ini adalah protokol stateless sederhana untuk membuat API dalam gaya RPC (Remote Procedure Call).
Biasanya terlihat sebagai berikut.

Anda memiliki satu titik akhir tunggal di server yang menerima permintaan dengan tubuh formulir:

{"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/id441854/


All Articles