Client.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace Transmission;
  3. use Buzz\Message\Request;
  4. use Buzz\Message\Response;
  5. use Buzz\Client\Curl;
  6. use Buzz\Client\ClientInterface;
  7. /**
  8. * The Client class is used to make API calls to the Transmission server
  9. *
  10. * @author Ramon Kleiss <ramon@cubilon.nl>
  11. */
  12. class Client
  13. {
  14. /**
  15. * @var string
  16. */
  17. const DEFAULT_HOST = 'localhost';
  18. /**
  19. * @var integer
  20. */
  21. const DEFAULT_PORT = 9091;
  22. /**
  23. * @var string
  24. */
  25. const DEFAULT_PATH = '/transmission/rpc';
  26. /**
  27. * @var string
  28. */
  29. const TOKEN_HEADER = 'X-Transmission-Session-Id';
  30. /**
  31. * @var string
  32. */
  33. protected $host;
  34. /**
  35. * @var integer
  36. */
  37. protected $port;
  38. /**
  39. * @var string
  40. */
  41. protected $path;
  42. /**
  43. * @var string
  44. */
  45. protected $token;
  46. /**
  47. * @var Buzz\Client\ClientInterface
  48. */
  49. protected $client;
  50. /**
  51. * @var string
  52. */
  53. protected $auth;
  54. /**
  55. * Constructor
  56. *
  57. * @param string $host The hostname of the Transmission server
  58. * @param integer $port The port the Transmission server is listening on
  59. * @param string $path The path to Transmission server rpc api
  60. */
  61. public function __construct($host = null, $port = null, $path = null)
  62. {
  63. $this->setHost($host ?: self::DEFAULT_HOST);
  64. $this->setPort($port ?: self::DEFAULT_PORT);
  65. $this->setPath($path ?: self::DEFAULT_PATH);
  66. $this->setToken(null);
  67. $this->setClient(new Curl());
  68. }
  69. /**
  70. * Authenticate against the Transmission server
  71. *
  72. * @param string $username
  73. * @param string $password
  74. */
  75. public function authenticate($username, $password)
  76. {
  77. $this->auth = base64_encode($username .':'. $password);
  78. }
  79. /**
  80. * Make an API call
  81. *
  82. * @param string $method
  83. * @param array $arguments
  84. * @return stdClass
  85. * @throws RuntimeException
  86. */
  87. public function call($method, array $arguments)
  88. {
  89. $request = new Request('POST', $this->getPath(), $this->getUrl());
  90. $response = new Response();
  91. $content = array('method' => $method, 'arguments' => $arguments);
  92. $request->addHeader(sprintf('%s: %s', self::TOKEN_HEADER, $this->getToken()));
  93. $request->setContent(json_encode($content));
  94. if (is_string($this->auth)) {
  95. $request->addHeader(sprintf('Authorization: Basic %s', $this->auth));
  96. }
  97. try {
  98. $this->getClient()->send($request, $response);
  99. } catch (\Exception $e) {
  100. throw new \RuntimeException(
  101. 'Could not connect to Transmission',
  102. 0,
  103. $e
  104. );
  105. }
  106. if ($response->getStatusCode() != 200 &&
  107. $response->getStatusCode() != 401 &&
  108. $response->getStatusCode() != 409) {
  109. throw new \RuntimeException('Unexpected response received from Transmission');
  110. }
  111. if ($response->getStatusCode() == 401) {
  112. throw new \RuntimeException('Access to Transmission requires authentication');
  113. }
  114. if ($response->getStatusCode() == 409) {
  115. $this->setToken($response->getHeader(self::TOKEN_HEADER));
  116. return $this->call($method, $arguments);
  117. }
  118. return json_decode($response->getContent());
  119. }
  120. /**
  121. * Get the URL used to connect to Transmission
  122. *
  123. * @return string
  124. */
  125. public function getUrl()
  126. {
  127. return sprintf(
  128. 'http://%s:%d',
  129. $this->getHost(),
  130. $this->getPort()
  131. );
  132. }
  133. /**
  134. * Set the hostname of the Transmission server
  135. *
  136. * @param string $host
  137. */
  138. public function setHost($host)
  139. {
  140. $this->host = (string) $host;
  141. }
  142. /**
  143. * Get the hostname of the Transmission server
  144. *
  145. * @return string
  146. */
  147. public function getHost()
  148. {
  149. return $this->host;
  150. }
  151. /**
  152. * Set the port the Transmission server is listening on
  153. *
  154. * @param integer $port
  155. */
  156. public function setPort($port)
  157. {
  158. $this->port = (integer) $port;
  159. }
  160. /**
  161. * Get the port the Transmission server is listening on
  162. *
  163. * @return integer
  164. */
  165. public function getPort()
  166. {
  167. return $this->port;
  168. }
  169. /**
  170. * Set the path to Transmission server rpc api
  171. *
  172. * @param string $path
  173. */
  174. public function setPath($path)
  175. {
  176. return $this->path = (string) $path;
  177. }
  178. /**
  179. * Get the path to Transmission server rpc api
  180. */
  181. public function getPath()
  182. {
  183. return $this->path;
  184. }
  185. /**
  186. * Set the CSRF-token of the Transmission client
  187. *
  188. * @param string $token
  189. */
  190. public function setToken($token)
  191. {
  192. $this->token = (string) $token;
  193. }
  194. /**
  195. * Get the CSRF-token for the Transmission client
  196. *
  197. * @return string
  198. */
  199. public function getToken()
  200. {
  201. return $this->token;
  202. }
  203. /**
  204. * Set the Buzz client used to connect to Transmission
  205. *
  206. * @param Buzz\Client\ClientInterface $client
  207. */
  208. public function setClient(ClientInterface $client)
  209. {
  210. $this->client = $client;
  211. }
  212. /**
  213. * Get the Buzz client used to connect to Transmission
  214. *
  215. * @return Buzz\Client\ClientInterface
  216. */
  217. public function getClient()
  218. {
  219. return $this->client;
  220. }
  221. }