-
Notifications
You must be signed in to change notification settings - Fork 1
ja Requester class
USAMI Kenta edited this page May 5, 2017
·
2 revisions
名前の妥当性はよくわからん。
APIリクエストするための手続きを静的メソッドとしてを集めたクラスです。いや、別にクラスではなくて名前空間入りの函数でも良いんですけど、クラスなら自動で遅延ロードできますからね。
I'm a static guy.
class Requester
{
/**
* Fetching an account
*
* @see https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#fetching-an-account
* @param Client $client
* @param SessionStorage $session
* @param int $id
* @return Entity\Account ← 返値の型(クラス名)を明示する
*/
public static function getAccount(Client $client, SessionStorage $session, $id)
{
// check type ($id > 0)
v::intVal()->min(0)->assert($id);
// return
return static::map(
Entity\Account::class, // ← クラス名文字列
$client->requestAPI('GET', sprintf('/api/v1/accounts/%d', $id), [], $session)
);
}
// ...
}
Mastodon\Client $client
はGuzzle6の引数をそのまま受け付けるので、HTTPリクエストの構築方法を学ぶにはRequest Options — Guzzle Documentationを読む必要があります。
さっき気付いたんですけど、全部のメソッドにClient $client, SessionStorage $session
って書くのもそれはそれでめんどくさいので、コンストラクタに分けて、手続きはやっぱりstatic
にしなくてもいいですね。どーしようかな。