API開發者永不“REST”

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"免責聲明:儘管標題有爭議,但本文並不是試圖證明RPC比REST更好,或者GraphQL比RPC更好。相反,本文的目的是向你介紹這些方法的大致情況以及它們的優缺點。最終的選擇將會是一個權衡。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"儘管HTTP是一個應用層(例如,L7)協議,但在API開發方面,HTTP實際上扮演着一個較低層次的傳輸機制的角色。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在HTTP上如何實現API有許多方法,它們在概念上有所不同:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"REST"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RPC"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GraphQL"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"...但是一個普通的開發人員需要知道的實際清單並不侷限於這三個傢伙。在這個領域中還有JSON、gRPC、protobuf等其它術語。讓我們試着一次性瞭解所有這些術語吧!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/6c\/be\/6cc50627f894c926c66fae8yybafb0be.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"代表性狀態轉移(REST)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Representational_state_transfer","title":"","type":null},"content":[{"type":"text","text":"REST"}]},{"type":"text","text":"只是一種軟件架構風格。它是一組設計約束,而不是具體的協議。REST依賴於資源的概念。例如,一個REST API是由一組資源(名詞)和與這些資源交互的有限數量的動作(動詞,查詢fetch、創建create、更新update、刪除delete等)組成。它在思想上非常接近最初的HTTP設計,主要基於資源(URLs)和方法(GET、POST、PUT、DELETE)。因此,從實現的角度來看,REST模型到HTTP協議的映射相對簡單:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"# Create new book\nPOST http:\/\/myapi.com\/books\/ (author=\"R. Feynman\", year=1975) -> book_id\n\n\n# Get book with ID = 1\nGET http:\/\/myapi.com\/books\/1 () -> (id=1, author=\"R. Feynman\", year=1975)\n\n\n# Update book with ID = 1\nPUT http:\/\/myapi.com\/books\/1 (id=1, author=\"Richard Feynman\", year=1975) -> (...)\n\n\n# Delete book with ID = 1\nDELETE http:\/\/myapi.com\/books\/1 () -> nu"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可能每一個現代web框架都提供了構建一個REST風格的Web服務所需的所有現成工具。從客戶端的角度來看,調用一個REST API非常簡單——它只需要將指定的HTTP方法發送到一組預定義的URLs。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章