[WordPress] 外掛分享: WP REST API Idempotent Requests

首頁外掛目錄 › WP REST API Idempotent Requests
全新外掛
安裝啟用
尚無評分
3292 天前
最後更新
問題解決
WordPress 4.7+ v1.0 上架:2017-04-08

內容簡介

p>當遇到網路錯誤時,API 客戶端應該能夠重試請求而不會出現請求被處理了兩次的風險。
WP API Idempotence 提供對客戶端包含唯一識別該請求的穩定性關鍵的支援。如果服務器檢測到已經處理了同一個鍵的請求,或者當前正在處理,將返回初始請求的回應。

例如:

{
"title": "My Important Post",
"content": "This will only go out once!",
"status": "draft",
"idempotency_key": "1ced64e9-9537-4b7b-9919-444d9e15e201"
}

配置

唯一識別性關鍵可以在請求標頭或請求正文中傳遞。
唯一識別性關鍵的名稱可以自定義。
更改唯一識別性關鍵支援的 HTTP 方法。預設為POST、PUT、PATCH。

包括一個示範所選擇的配置的請求界面。

開發人員

該外掛包括兩個操作可修改依賴注入容器 (DIC) 並插入自定義服務。

wp_api_idempotence_initialize_container_builder 操作允許您修改依賴性注入生成器本身,
wp_api_idempotence_initialize_container 操作允許您覆蓋依賴關係。例如:

add_action( 'wp_api_idempotence_initialize_container', function( $container ) {
$container->set( '_dataStore', DI\object( 'YourName\CustomDataStore' ) );
} );

在插件底層中,該插件由DataStore、RequestHasher、ResponseSerializer和RequestPoller組成。

DataStore 主要責任是檢索或儲存穩定請求。預設情況下,請求
儲存在自定義數據庫表中。但是,可以通過實現 DataStore
介面將其替換為自定義驅動程序,例如 Redis 服務器。

RequestHasher 產生唯一的散列值,用於 WP_REST_Request 對象。此散列值是基於請求的內容,而不是對象上的 spl_object_hash 或類似物。通過實現
RequestHasher介面,可以對其進行替換。

ResponseSerializer 將 WP_REST_Response 或 WP_Error 對象轉換成字串表示形式。
預設的 JSON 序列化支援使用wp_api_idempotence_serialized_response_data和wp_api_idempotence_attach_serialized_response_data過濾器過濾序列化過程。參考src/ResponseSerializer/Filtered.php。
如果實現ResponseSerializer界面,則會整個更換序列化器。

最後,RequestPoller 類在確定有一個穩定請求正在被處理,當另一個具有相同鍵的請求到達時,會為其輪詢數據存儲點的回應。預設情況下,數據儲存庫會輪詢每秒最多 15 次以嘗試取回回應對象。如果找不到回應,則會返回代碼為 rest_duplicate_idempotency_key 的錯誤。可以通過在 DIC 中覆蓋poll.sleepSeconds 和
poll.maxQueries 來調整這個問題。如果實現RequestPoller
介面,則可完全替換RequestPoller。

貢獻

該外掛托管在

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.0) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「WP REST API Idempotent Requests」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

When a network error is encountered, API clients should be able to retry a request without a risk of their request being processed twice.
WP API Idempotence adds support for clients to include an idempotency key that uniquely identifies that request. If the server detects
that a request with the same key has already been processed or is currently being processed, the response for the initial request will
be returned.
For Example:
{
"title": "My Important Post",
"content": "This will only go out once!",
"status": "draft",
"idempotency_key": "1ced64e9-9537-4b7b-9919-444d9e15e201"
}

Configuration

Idempotency key can either be passed in the request header or the request body.
The idempotency key name can be customized.
Change the HTTP methods the idempotency key is supported for. Defaults to POST, PUT, PATCH.

A sample request interface is included to demonstrate the selected configuration.
Developers
The plugin includes two actions to modify the dependency injection container (DIC) and insert custom services.
The wp_api_idempotence_initialize_container_builder action allows you to modify the Dependency Injection builder itself and
the wp_api_idempotence_initialize_container action allows you to override dependencies. For example:
add_action( 'wp_api_idempotence_initialize_container', function( $container ) {
$container->set( '_dataStore', DI\object( 'YourName\CustomDataStore' ) );
} );

Under the hood, the plugin is made up of a DataStore, RequestHasher, ResponseSerializer and RequestPoller.
The DataStore is primarily responsible for retrieving or storing an idempotent request. By default, requests
are stored in a custom database table. This could be substituted for a custom driver by implementing the DataStore
interface. For example a Redis server.
The RequestHasher produces a unique hash for a WP_REST_Request object. This hash is based off of the contents of
the request, not for the object via spl_object_hash or similar. This can also be substituted by implementing the
RequestHasher interface.
The ResponseSerializer converts a WP_REST_Response or WP_Error object back and forth from a string representation.
The default JSON serializer supports filtering the serialization process using the wp_api_idempotence_serialized_response_data
and wp_api_idempotence_attach_serialized_response_data filters. See src/ResponseSerializer/Filtered.php.
The entire serializer can be substituted by implementing the ResponseSerializer interface.
Finally, the RequestPoller class polls the data store for a response if it is determined that an idempotent request
is currently being processed when another request with the same key arrives. By default, the data store is polled
every seconds a maximum of 15 times to try and retrieve a response object. If no response is found, an error with code
rest_duplicate_idempotency_key will be returned. This can be adjusted by overwriting the poll.sleepSeconds and
poll.maxQueries in the DIC. The RequestPoller can also be entirely subsituted by implementing the RequestPoller
interface.
Contributing
This plugin is hosted on GitHub. Issues and pull requests are welcomed.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon