
2023年12個必備的書籍API
好吧,等一下,內容相當多。讓我描述一下重要的幾行。
1.我們將POST
在端點執行請求/tracking/{parcelId}/subscribe
,以便訂閱有關 所標識的包裹的通知parcelId
。
注意: $ref: '#/components/schemas/parcelId'
這是一個技巧,使我們能夠重用parcelId
模式定義(在文檔末尾的組件部分中定義)
2.請求主體部分定義預期的輸入負載:
url
使您可以告訴物流公司您希望在哪個 URL 上接收通知token
是我們安全的基礎。每次訂閱都會生成令牌并存儲在我們這邊。在收到通知時,我們將驗證令牌是否由我們生成。注意: token 可能是隨機生成的數據(我們需要將其存儲在我們這邊)。它也可能是公鑰(那么我們只需要驗證它是否與我們的秘密私鑰匹配)。
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
url:
type: string
format: uri
token:
type: string
required:
- url
- token
3.接下來,responses
部分定義單個202
響應。202
表示“接受”以供進一步處理。
最重要的部分: 。在我們的例子中,keywordcallbacks
下的鍵是它們的名稱。然后是標題 – 。此路徑是運行時表達式。路徑表示將從請求正文中提取并構建通知接收 URL。callbacksnotification'{$request.body#/url}?token={$request.body#/token}'
urltoken
client.oas3.yaml
openapi: 3.0.0
paths:
/notify:
post:
summary: 'Receive notification about a parcel'
security:
- TokenSecurity: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/notification'
responses:
'200':
description: 'Notification successfully processed'
content:
text/plain:
examples:
ok:
value: 'ok'
components:
securitySchemes:
TokenSecurty:
type: apiKey
name: token
in: query
schemas:
parcelId:
type: string
pattern: '^[0-9]{16}$'
notification:
type: object
properties:
parcelId:
$ref: "#/components/schemas/parcelId"
status:
type: string
enum:
- pick-up
- in-transit
- delivered
required:
- parcelId
- status
我們的 API 規范定義了一個處理通知接收的端點。就這么簡單!
我們已經定義了物流服務 API 和我們的 API 的規范。現在是時候啟動prism
實例并查看一些操作了。
下面的命令將為港口prism
的物流公司運行實例4010
。
prism mock -p 4010 logistics.oas3.yaml
現在請打開新的控制臺并運行以下命令:
prism mock -p 4011 client.oas3.yaml
準備好?
curl -v -H'Content-type: application/json' -d'{ "url": "http://localhost:4011/notify", "token": "dontpeek" }' http://127.0.0.1:4010/tracking/7352110771638879/subscribe
以下是物流服務的輸出:
[CLI] … awaiting Starting Prism…
[HTTP SERVER] info Server listening at http://127.0.0.1:4010
[CLI] info POST http://127.0.0.1:4010/tracking/7352110771638879/subscribe
[HTTP SERVER] post /tracking/7352110771638879/subscribe info Request received
[NEGOTIATOR] info Request contains an accept header: */*
[VALIDATOR] success The request passed the validation rules. Looking for the best response
[NEGOTIATOR] success Found a compatible content for */*
[NEGOTIATOR] success Responding with the requested status code 202
[CALLBACK] info notification: Making request to http://localhost:4011/notify?token=dontpeek...
[CALLBACK] info notification: Request finished
以及客戶端服務控制臺相應的輸出:
[CLI] … awaiting Starting Prism…
[HTTP SERVER] info Server listening at http://127.0.0.1:4011
[CLI] info POST http://127.0.0.1:4011/notify
[HTTP SERVER] post /notify info Request received
[NEGOTIATOR] info Request contains an accept header: */*
[VALIDATOR] success The request passed the validation rules. Looking for the best response
[NEGOTIATOR] success Found a compatible content for */*
[NEGOTIATOR] success Responding with the requested status code 200
哇哦!發生什么事了?
POST
已提出http://127.0.0.1:4010/tracking/7352110771638879/subscribe
202
表示訂閱已被接受。http://localhost:4011/notify?token=dontpeek
/notify
Prism使用模擬有效負載向我們的端點執行請求對于那些喜歡圖表的人來說,這里有一個序列圖。它展示了訂閱回調和接收三個通知的過程。
恭喜,您剛剛模擬了兩個服務并強制它們相互通信。本教程到此結束!
測試回調可能會變得很復雜。你可能需要編寫一些小工具來模擬調用者服務。或者用curl “手動”調用它。或者強制服務調用你的 API 并相信它確實會這樣做。
聽起來還挺簡單的?現在想象一下你的 API 會隨著時間而改變。你需要維護該工具,但你丟失了curl命令,而且沒有關于如何快速測試 API 的文檔。哦,多么熟悉啊!
但是,等一下,您有一個始終保持最新的 OpenAPI 規范(因為您的老板告訴您這樣做),并且 Prism 可以將其變成 API 任何部分的功能齊全的模仿?,F在您既不需要秘密命令,也不需要非文檔化的工具。
原文鏈接:Mocking?Callbacks?with?OpenAPI?and?Prism