-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,102 @@ | ||
# RESTer | ||
|
||
RESTer 是一個方便呼叫 REST API 的函式庫。此函式庫可以使用已經建好的 class 直接使用,開發者也可以自行組合內部各種元件,成為一個客製化的 SDK 。 | ||
RESTer 是一個方便呼叫 REST API 的函式庫。 | ||
|
||
此函式庫可以使用已經建好的 class 直接使用,開發者也可以自行組合內部各種元件,成為一個客製化的 SDK 。 | ||
|
||
## 快速開始 | ||
|
||
首先先使用 Composer 將套件加入: | ||
|
||
``` | ||
composer require 104corp/rester | ||
``` | ||
|
||
接著直接使用 `ResterClient` : | ||
|
||
```php | ||
use Corp104\Rester\ResterClient; | ||
use Corp104\Rester\Api\Path; | ||
|
||
// 初始化 ResterClient 物件 | ||
$resterClient = new ResterClient('http://127.0.0.1'); | ||
$resterClient->provisionMapping([ | ||
'foo' => new Path('GET', '/foo'), | ||
]); | ||
|
||
// GET http://127.0.0.1/foo | ||
$response = $resterClient->foo(); | ||
|
||
// PSR-7 Response | ||
echo (string)$response->getBody(); | ||
``` | ||
|
||
一切順利的話,將會看到 Response 的內容。 | ||
|
||
## ResterClient | ||
|
||
`ResterClient` 裡有一個 `Mapping` 物件,它負責存放名稱與 API 之間的對應關係。接著使用 Magic Method `__call` 來實作 method 名稱對應到 API 的方法,如此一來, ResterClient 就能有許多自定義的方法,來呼叫對應的 API。 | ||
|
||
`Mapping` 存放的 API 需實作 `ApiInterface` ,如 `Path` 、 `Endpoint` 或其他實作。 | ||
|
||
下面是一個混用的例子: | ||
|
||
```php | ||
$resterClient = new ResterClient('http://127.0.0.1'); | ||
$resterClient->provisionMapping([ | ||
'foo' => new Path('GET', '/foo'), | ||
'bar' => new Endpoint('POST', 'https://www.some-url.com/bar'), | ||
]); | ||
|
||
// GET http://127.0.0.1/foo | ||
$resterClient->foo(); | ||
|
||
// POST https://www.some-url.com/bar | ||
$resterClient->bar(); | ||
``` | ||
|
||
## Parameters | ||
|
||
呼叫 API 時,會需要帶三種參數: | ||
|
||
> 以下使用原始碼的名稱做說明 | ||
* Binding | ||
* QueryString | ||
* ParsedBody | ||
|
||
這三種參數的預設值都是空 array `[]` 。不同的地方如下: | ||
|
||
### Binding | ||
|
||
這個是指 path 中的變數,假如有個 API 是長這樣 `GET /users/{userId}` ,而 `Binding` 主要任務就是把 userId 替換成開發者想要的字串或內容。實際上,只要跟 ResterClient 說要把它換成想要的字即可。範例程式碼如下: | ||
|
||
```php | ||
// API 定義為 /foo/{bar}/{baz} | ||
$resterClient = new ResterClient('http://127.0.0.1'); | ||
$resterClient->provisionMapping([ | ||
'foo' => new Path('GET', '/foo/{bar}/{baz}'), | ||
]); | ||
|
||
// 使用 key-value 呼叫 API : GET http://127.0.0.1/str1/str2 | ||
$resterClient->foo([ | ||
'bar' => 'str1', | ||
'baz' => 'str2', | ||
]); | ||
|
||
// 按順序的方法代入 : GET http://127.0.0.1/str3/str4 | ||
$resterClient->foo([ | ||
'bar' => 'str3', | ||
'baz' => 'str4', | ||
]); | ||
``` | ||
|
||
### QueryString | ||
|
||
正常使用下,應該不大會有問題 | ||
|
||
### ParsedBody | ||
|
||
正常使用下,應該不大會有問題 | ||
|
||
## ResterCollection |