このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
psr:psr18 [2020/08/25 11:37] tanaka [ClientExceptionInterface] |
psr:psr18 [2020/08/28 14:28] y2sunlight [ClientExceptionInterface] |
||
---|---|---|---|
行 31: | 行 31: | ||
--- // 原文より翻訳 [[https:// | --- // 原文より翻訳 [[https:// | ||
- | |||
- | This document describes a common interface for sending HTTP requests and receiving HTTP responses. | ||
このドキュメントでは、HTTPリクエストを送信し、HTTPレスポンスを受信するための一般的なインターフェースについて説明します。 | このドキュメントでは、HTTPリクエストを送信し、HTTPレスポンスを受信するための一般的なインターフェースについて説明します。 | ||
- | |||
- | The key words “MUST”, “MUST NOT”, “REQUIRED”, | ||
このドキュメントのキーワード '' | このドキュメントのキーワード '' | ||
行 50: | 行 46: | ||
===== 目標 ====== | ===== 目標 ====== | ||
- | |||
- | The goal of this PSR is to allow developers to create libraries decoupled from HTTP client implementations. This will make libraries more reusable as it reduces the number of dependencies and lowers the likelihood of version conflicts. | ||
このPSRの目標は、開発者がHTTPクライアントの実装から分離されたライブラリを作成できるようにすることです。これにより、依存関係の数が減り、バージョンの競合の可能性が低くなるため、ライブラリの再利用性が高まります。 | このPSRの目標は、開発者がHTTPクライアントの実装から分離されたライブラリを作成できるようにすることです。これにより、依存関係の数が減り、バージョンの競合の可能性が低くなるため、ライブラリの再利用性が高まります。 | ||
- | A second goal is that HTTP clients can be replaced as per the Liskov substitution principle. This means that all clients MUST behave in the same way when sending a request. | + | 2番目の目標は、[[https:// |
- | + | ||
- | 2番目の目標は、[[https:// | + | |
\\ | \\ | ||
行 63: | 行 55: | ||
===== 定義 ====== | ===== 定義 ====== | ||
- | * クライアント - クライアントは、PSR-7互換のHTTPリクエストメッセージを送信し、PSR-7互換のHTTPレスポンスメッセージを呼び出し側ライブラリに返す目的でこの仕様を実装するライブラリです。 | + | * '' |
- | * 呼び出しライブラリ - 呼び出しライブラリは、このPSRで定義されているHTTP'' | + | * 呼び出しライブラリ |
\\ | \\ | ||
===== クライアント ====== | ===== クライアント ====== | ||
- | |||
- | A Client is an object implementing ClientInterface. | ||
クライアントは、'' | クライアントは、'' | ||
- | A Client MAY: | + | **クライアントの選択**( '' |
- | + | ||
- | クライアントのMAY-DO: | + | |
- | + | ||
- | * Choose to send an altered HTTP request from the one it was provided. For example, it could compress an outgoing message body. | + | |
- | | + | |
- | + | ||
- | | + | |
- | | + | |
- | + | ||
- | If a Client chooses to alter either the HTTP request or HTTP response, it MUST ensure that the object remains internally consistent. For example, if a Client chooses to decompress the message body then it MUST also remove the Content-Encoding header and adjust the Content-Length header. | + | |
- | クライアントがHTTPリクエストまたはHTTPレスポンスのいずれかを変更することを選択した場合、オブジェクトが内部的に一貫していることを保証する必要があります( | + | * クライアントは提供されたものから変更されたHTTPリクエストを送信することを選択できます( |
+ | * クライアントは呼び出したライブラリに返す前に、受信したHTTPレスポンスを変更することを選択できます( | ||
- | Note that as a result, since PSR-7 objects are immutable, the Calling Library | + | クライアントがHTTPリクエストまたはHTTPレスポンスのいずれかを変更することを選択した場合、オブジェクトが内部的に一貫していることを保証する必要があります( '' |
その結果、[[https:// | その結果、[[https:// | ||
- | A Client | + | **クライアントの義務**( '' |
- | クライアントのMUST-DO: | + | |
- | + | ||
- | * Reassemble a multi-step HTTP 1xx response itself so that what is returned to the Calling Library is a valid HTTP response of status code 200 or higher. | + | |
- | + | ||
- | | + | |
\\ | \\ | ||
===== エラー処理 ====== | ===== エラー処理 ====== | ||
- | |||
- | A Client MUST NOT treat a well-formed HTTP request or HTTP response as an error condition. For example, response status codes in the 400 and 500 range MUST NOT cause an exception and MUST be returned to the Calling Library as normal. | ||
クライアントは、整形式のHTTPリクエストまたはHTTPレスポンスをエラー状態として扱わないでください( '' | クライアントは、整形式のHTTPリクエストまたはHTTPレスポンスをエラー状態として扱わないでください( '' | ||
- | A Client MUST throw an instance of Psr\Http\Client\ClientExceptionInterface if and only if it is unable to send the HTTP request at all or if the HTTP response could not be parsed into a PSR-7 response object. | + | クライアントは、HTTPリクエストをまったく送信できない場合、または HTTPレスポンスを PSR-7 レスポンスオブジェクトにパースできなかった場合にのみ、'' |
- | + | ||
- | クライアントは、HTTPリクエストをまったく送信できない場合、またはHTTPレスポンスをPSR-7レスポンスオブジェクトにパースできなかった場合にのみ、'' | + | |
- | + | ||
- | If a request cannot be sent because the request message is not a well-formed HTTP request or is missing some critical piece of information (such as a Host or Method), the Client MUST throw an instance of Psr\Http\Client\RequestExceptionInterface. | + | |
リクエストメッセージが整形式のHTTPリクエストではないか、重要な情報(ホストやメソッドなど)がないためにリクエストを送信できない場合、クライアントは '' | リクエストメッセージが整形式のHTTPリクエストではないか、重要な情報(ホストやメソッドなど)がないためにリクエストを送信できない場合、クライアントは '' | ||
- | |||
- | If the request cannot be sent due to a network failure of any kind, including a timeout, the Client MUST throw an instance of Psr\Http\Client\NetworkExceptionInterface. | ||
タイムアウトを含むあらゆる種類のネットワーク障害が原因でリクエストを送信できない場合、クライアントは '' | タイムアウトを含むあらゆる種類のネットワーク障害が原因でリクエストを送信できない場合、クライアントは '' | ||
- | |||
- | Clients MAY throw more specific exceptions than those defined here (a TimeOutException or HostNotFoundException for example), provided they implement the appropriate interface defined above. | ||
クライアントは、上記で定義された適切なインターフェースを実装している場合、ここで定義されたものよりも具体的な例外(例えば、 '' | クライアントは、上記で定義された適切なインターフェースを実装している場合、ここで定義されたものよりも具体的な例外(例えば、 '' | ||
行 139: | 行 106: | ||
{ | { | ||
/** | /** | ||
- | * Sends a PSR-7 request and returns a PSR-7 response. | ||
* PSR-7リクエストを送信し、PSR-7レスポンスを返します。 | * PSR-7リクエストを送信し、PSR-7レスポンスを返します。 | ||
* | * | ||
* @param RequestInterface $request | * @param RequestInterface $request | ||
- | * | ||
* @return ResponseInterface | * @return ResponseInterface | ||
* | * | ||
- | * @throws \Psr\Http\Client\ClientExceptionInterface | + | * @throws \Psr\Http\Client\ClientExceptionInterface リクエストの処理中にエラーが発生した場合。 |
- | | + | |
*/ | */ | ||
public function sendRequest(RequestInterface $request): ResponseInterface; | public function sendRequest(RequestInterface $request): ResponseInterface; | ||
行 161: | 行 126: | ||
/** | /** | ||
- | * Every HTTP client related exception MUST implement this interface. | ||
* すべてのHTTPクライアント関連の例外は、このインターフェイスを実装する必要があります。 | * すべてのHTTPクライアント関連の例外は、このインターフェイスを実装する必要があります。 | ||
*/ | */ | ||
行 174: | 行 138: | ||
<code php RequestExceptionInterface.php> | <code php RequestExceptionInterface.php> | ||
+ | namespace Psr\Http\Client; | ||
+ | |||
+ | use Psr\Http\Message\RequestInterface; | ||
+ | |||
+ | /** | ||
+ | * Exception for when a request failed. | ||
+ | * リクエストが失敗した場合の例外。 | ||
+ | * | ||
+ | * Examples: | ||
+ | | ||
+ | | ||
+ | * 例: | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | interface RequestExceptionInterface extends ClientExceptionInterface | ||
+ | { | ||
+ | /** | ||
+ | * Returns the request. | ||
+ | * リクエストを返します。 | ||
+ | * | ||
+ | * The request object MAY be a different object from the one passed to ClientInterface:: | ||
+ | * リクエストオブジェクトは、ClientInterface:: | ||
+ | * 異なるオブジェクトである可能性があります | ||
+ | * | ||
+ | * @return RequestInterface | ||
+ | */ | ||
+ | public function getRequest(): | ||
+ | } | ||
</ | </ | ||
行 181: | 行 174: | ||
<code php NetworkExceptionInterface.php> | <code php NetworkExceptionInterface.php> | ||
+ | namespace Psr\Http\Client; | ||
+ | |||
+ | use Psr\Http\Message\RequestInterface; | ||
+ | |||
+ | /** | ||
+ | * Thrown when the request cannot be completed because of network issues. | ||
+ | * ネットワークの問題のためにリクエストを完了できない場合にスローされます。 | ||
+ | * | ||
+ | * There is no response object as this exception is thrown when no response has been received. | ||
+ | * この例外は、応答が受信されなかった場合にスローされるため、応答オブジェクトはありません。 | ||
+ | * | ||
+ | * Example: the target host name can not be resolved or the connection failed. | ||
+ | * 例:ターゲットホスト名を解決できないか、接続が失敗しました。 | ||
+ | */ | ||
+ | interface NetworkExceptionInterface extends ClientExceptionInterface | ||
+ | { | ||
+ | /** | ||
+ | * Returns the request. | ||
+ | * リクエストを返します。 | ||
+ | * | ||
+ | * The request object MAY be a different object from the one passed to ClientInterface:: | ||
+ | * リクエストオブジェクトは、ClientInterface:: | ||
+ | * 異なるオブジェクトである可能性があります | ||
+ | * | ||
+ | * @return RequestInterface | ||
+ | */ | ||
+ | public function getRequest(): | ||
+ | } | ||
</ | </ | ||