openapi: 3.0.0
paths:
/tracking/{parcelId}/subscribe:
parameters:
- name: parcelId
in: path
required: true
description: The parcel identifier
schema:
$ref: '#/components/schemas/parcelId'
post:
summary: Subscribe to notifications about given parcel
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
url:
type: string
format: uri
token:
type: string
required:
- url
- token
responses:
202:
description: 'Successfully subscribed to notifications about given parcel'
content:
text/plain:
examples:
ok:
value: 'ok'
callbacks:
notification:
'{$request.body#/url}?token={$request.body#/token}':
post:
summary: 'Receive single notification about a parcel'
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:
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

好吧,等一下,內容相當多。讓我描述一下重要的幾行。

1.我們將POST在端點執行請求/tracking/{parcelId}/subscribe,以便訂閱有關 所標識的包裹的通知parcelId。

注意: $ref: '#/components/schemas/parcelId'這是一個技巧,使我們能夠重用parcelId模式定義(在文檔末尾的組件部分中定義)

2.請求主體部分定義預期的輸入負載:

注意: 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

客戶端服務的 OpenAPI 規范

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

哇哦!發生什么事了?

  1. 請求POST已提出http://127.0.0.1:4010/tracking/7352110771638879/subscribe
  2. Prism 回復202表示訂閱已被接受。
  3. 請求體包含回調URL和token
  4. 回調定義包含有關如何構造 WebHook URL 的配方,這將導致http://localhost:4011/notify?token=dontpeek
  5. 發生 Parcel 事件,觸發通知請求
  6. /notifyPrism使用模擬有效負載向我們的端點執行請求
  7. 客戶端控制臺顯示已收到通知

對于那些喜歡圖表的人來說,這里有一個序列圖。它展示了訂閱回調和接收三個通知的過程。

序列圖

恭喜,您剛剛模擬了兩個服務并強制它們相互通信。本教程到此結束!

那很好,但是我為什么需要 Prism?

測試回調可能會變得很復雜。你可能需要編寫一些小工具來模擬調用者服務。或者用curl “手動”調用它。或者強制服務調用你的 API 并相信它確實會這樣做。

聽起來還挺簡單的?現在想象一下你的 API 會隨著時間而改變。你需要維護該工具,但你丟失了curl命令,而且沒有關于如何快速測試 API 的文檔。哦,多么熟悉啊!

但是,等一下,您有一個始終保持最新的 OpenAPI 規范(因為您的老板告訴您這樣做),并且 Prism 可以將其變成 API 任何部分的功能齊全的模仿?,F在您既不需要秘密命令,也不需要非文檔化的工具。

原文鏈接:Mocking?Callbacks?with?OpenAPI?and?Prism

上一篇:

Java API 最佳實踐

下一篇:

API版本控制與微服務:最佳實踐的重要性
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費