close

ㄋThis document describes a common interface for sending HTTP requests and receiving HTTP responses.

Goal

這個PSR是為了讓開發者可以decouple HTTP client implementations 的 create libraries,讓libraries可以重用性高,且降低依賴的關係和版本衝突

另外一個目標是讓HTTP clients符合 Liskov substitution principle,子類別可以完全取代父類別,且不會有副作用。 所以所有的HTTP client在發送request時,必須表現一致

Definitions

Client - Client 是一個library,實作了這個規格,為了發送與PSR-7相符的HTTP request message和 returning a PSR-7相符的HTTP response message 給calling library。

Calling library - 在這裡是指任何程式碼,使用Client, 他並沒有實作規格的介面,但是負責呼叫實作client的object  

Client

A Client is an object implementing ClientInterface. ​​​​​​​

Client 可選擇性的

發送被更改過的HTTP request, example 他可以壓縮發送出去的message body。

可以選擇性的修改接收的HTTP response ,在回傳給calling libraries之前,例如它可以解壓縮傳送進來的message body。

 

假設client要做上述兩件事情時,他必須確保object 保持一致姓,而不會破壞到他原本的資料。 舉例來說,假設Client想要解壓縮這個message body他必須要移除content-encoding的header,因為他已經解壓縮過了,並且調整request的content-length header,因為解壓縮過後它的長度肯定會改變。

雖然筆記上說,PSR-7 objects是不可更動的,Calling library必須不能預設說object pass to ClientInterface::sendRequest() 出來之後還會是一樣的,舉例來說 the Reuqest Object 是從exception中回傳的,

可能會因為這樣而變得與當初傳進去 sendRequest()方法的object不一樣,所以comparison by reference (===) is not possible

A Client MUST:

自己重新組裝多個HTTP1XX response,所以這樣回傳回來給calling birary 才會是一個有效的http response,of 200 or higher

Error handling

  Client針對錯誤的機制,在面對一個well-formed的HTTP request或者是HTTP response時,不能當作是error,舉例來說如果今天的response status code是400~500 不能造成任何exception, 因為他是一個Well-formed 的response,SO must return to the calling library as normal.

Client發生error Psr\Http\Client\ClientExceptionInterface 的條件必須為無法發送HTTP request at all或者是HTTP response不能被轉換成PSR-7 response object

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.

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.

 

參考連結

https://www.php-fig.org/psr/psr-18/

 

 

 

arrow
arrow
    全站熱搜

    蕭瑞文 發表在 痞客邦 留言(0) 人氣()