TipitakaChannelsApi.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /**
  3. * TipitakaChannelsApi
  4. * PHP version 8.1
  5. *
  6. * @category Class
  7. * @package WikipaliApi
  8. * @author OpenAPI Generator team
  9. * @link https://openapi-generator.tech
  10. */
  11. /**
  12. * MINT API
  13. *
  14. * International Academy Of Pali Tipitaka(国际巴利三藏学院) 由 `php openapi/scripts/generate.php` 依据 api-v13 的路由与控制器源码生成。 响应统一封装为 `{ok, message, data}`;`ok=false` 时 `message` 为错误信息。
  15. *
  16. * The version of the OpenAPI document: 1.0.0
  17. * Generated by: https://openapi-generator.tech
  18. * Generator version: 7.25.0
  19. */
  20. /**
  21. * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
  22. * https://openapi-generator.tech
  23. * Do not edit the class manually.
  24. */
  25. namespace WikipaliApi\Api;
  26. use GuzzleHttp\Client;
  27. use GuzzleHttp\ClientInterface;
  28. use GuzzleHttp\Exception\ConnectException;
  29. use GuzzleHttp\Exception\RequestException;
  30. use GuzzleHttp\Psr7\MultipartStream;
  31. use GuzzleHttp\Psr7\Request;
  32. use GuzzleHttp\RequestOptions;
  33. use Psr\Http\Message\RequestInterface;
  34. use Psr\Http\Message\ResponseInterface;
  35. use WikipaliApi\ApiException;
  36. use WikipaliApi\Configuration;
  37. use WikipaliApi\FormDataProcessor;
  38. use WikipaliApi\HeaderSelector;
  39. use WikipaliApi\ObjectSerializer;
  40. /**
  41. * TipitakaChannelsApi Class Doc Comment
  42. *
  43. * @category Class
  44. * @package WikipaliApi
  45. * @author OpenAPI Generator team
  46. * @link https://openapi-generator.tech
  47. */
  48. class TipitakaChannelsApi
  49. {
  50. /**
  51. * @var ClientInterface
  52. */
  53. protected $client;
  54. /**
  55. * @var Configuration
  56. */
  57. protected $config;
  58. /**
  59. * @var HeaderSelector
  60. */
  61. protected $headerSelector;
  62. /**
  63. * @var int Host index
  64. */
  65. protected $hostIndex;
  66. /** @var string[] $contentTypes **/
  67. public const contentTypes = [
  68. 'getApiV3TipitakaChannels' => [
  69. 'application/json',
  70. ],
  71. ];
  72. /**
  73. * @param ClientInterface $client
  74. * @param Configuration $config
  75. * @param HeaderSelector $selector
  76. * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
  77. */
  78. public function __construct(
  79. ?ClientInterface $client = null,
  80. ?Configuration $config = null,
  81. ?HeaderSelector $selector = null,
  82. int $hostIndex = 0
  83. ) {
  84. $this->client = $client ?: new Client();
  85. $this->config = $config ?: Configuration::getDefaultConfiguration();
  86. $this->headerSelector = $selector ?: new HeaderSelector();
  87. $this->hostIndex = $hostIndex;
  88. }
  89. /**
  90. * Set the host index
  91. *
  92. * @param int $hostIndex Host index (required)
  93. */
  94. public function setHostIndex($hostIndex): void
  95. {
  96. $this->hostIndex = $hostIndex;
  97. }
  98. /**
  99. * Get the host index
  100. *
  101. * @return int Host index
  102. */
  103. public function getHostIndex()
  104. {
  105. return $this->hostIndex;
  106. }
  107. /**
  108. * @return Configuration
  109. */
  110. public function getConfig()
  111. {
  112. return $this->config;
  113. }
  114. /**
  115. * Operation getApiV3TipitakaChannels
  116. *
  117. * 列出有译文的公开 channel
  118. *
  119. * @param int|null $per_page 每页数量,最大 200 (optional, default to 20)
  120. * @param string|null $type channel 类型 (optional)
  121. * @param string|null $lang 译文语族(progress_chapters.lang,zh 涵盖简繁) (optional)
  122. * @param float|null $min_progress 章节完成度门槛 0–1 (optional, default to 0.8)
  123. * @param int|null $page 页码 (optional, default to 1)
  124. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChannels'] to see the possible values for this operation
  125. *
  126. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  127. * @throws \InvalidArgumentException
  128. * @return \WikipaliApi\Model\GetApiV3TipitakaChannels200Response|\WikipaliApi\Model\ProblemDetails
  129. */
  130. public function getApiV3TipitakaChannels($per_page = 20, $type = null, $lang = null, $min_progress = 0.8, $page = 1, string $contentType = self::contentTypes['getApiV3TipitakaChannels'][0])
  131. {
  132. list($response) = $this->getApiV3TipitakaChannelsWithHttpInfo($per_page, $type, $lang, $min_progress, $page, $contentType);
  133. return $response;
  134. }
  135. /**
  136. * Operation getApiV3TipitakaChannelsWithHttpInfo
  137. *
  138. * 列出有译文的公开 channel
  139. *
  140. * @param int|null $per_page 每页数量,最大 200 (optional, default to 20)
  141. * @param string|null $type channel 类型 (optional)
  142. * @param string|null $lang 译文语族(progress_chapters.lang,zh 涵盖简繁) (optional)
  143. * @param float|null $min_progress 章节完成度门槛 0–1 (optional, default to 0.8)
  144. * @param int|null $page 页码 (optional, default to 1)
  145. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChannels'] to see the possible values for this operation
  146. *
  147. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  148. * @throws \InvalidArgumentException
  149. * @return array of \WikipaliApi\Model\GetApiV3TipitakaChannels200Response|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  150. */
  151. public function getApiV3TipitakaChannelsWithHttpInfo($per_page = 20, $type = null, $lang = null, $min_progress = 0.8, $page = 1, string $contentType = self::contentTypes['getApiV3TipitakaChannels'][0])
  152. {
  153. $request = $this->getApiV3TipitakaChannelsRequest($per_page, $type, $lang, $min_progress, $page, $contentType);
  154. try {
  155. $options = $this->createHttpClientOption();
  156. try {
  157. $response = $this->client->send($request, $options);
  158. } catch (RequestException $e) {
  159. throw new ApiException(
  160. "[{$e->getCode()}] {$e->getMessage()}",
  161. (int) $e->getCode(),
  162. $e->getResponse() ? $e->getResponse()->getHeaders() : null,
  163. $e->getResponse() ? (string) $e->getResponse()->getBody() : null
  164. );
  165. } catch (ConnectException $e) {
  166. throw new ApiException(
  167. "[{$e->getCode()}] {$e->getMessage()}",
  168. (int) $e->getCode(),
  169. null,
  170. null
  171. );
  172. }
  173. $statusCode = $response->getStatusCode();
  174. switch($statusCode) {
  175. case 200:
  176. return $this->handleResponseWithDataType(
  177. '\WikipaliApi\Model\GetApiV3TipitakaChannels200Response',
  178. $request,
  179. $response,
  180. );
  181. case 422:
  182. return $this->handleResponseWithDataType(
  183. '\WikipaliApi\Model\ProblemDetails',
  184. $request,
  185. $response,
  186. );
  187. }
  188. if ($statusCode < 200 || $statusCode > 299) {
  189. throw new ApiException(
  190. sprintf(
  191. '[%d] Error connecting to the API (%s)',
  192. $statusCode,
  193. (string) $request->getUri()
  194. ),
  195. $statusCode,
  196. $response->getHeaders(),
  197. (string) $response->getBody()
  198. );
  199. }
  200. return $this->handleResponseWithDataType(
  201. '\WikipaliApi\Model\GetApiV3TipitakaChannels200Response',
  202. $request,
  203. $response,
  204. );
  205. } catch (ApiException $e) {
  206. switch ($e->getCode()) {
  207. case 200:
  208. $data = ObjectSerializer::deserialize(
  209. $e->getResponseBody(),
  210. '\WikipaliApi\Model\GetApiV3TipitakaChannels200Response',
  211. $e->getResponseHeaders()
  212. );
  213. $e->setResponseObject($data);
  214. throw $e;
  215. case 422:
  216. $data = ObjectSerializer::deserialize(
  217. $e->getResponseBody(),
  218. '\WikipaliApi\Model\ProblemDetails',
  219. $e->getResponseHeaders()
  220. );
  221. $e->setResponseObject($data);
  222. throw $e;
  223. }
  224. throw $e;
  225. }
  226. }
  227. /**
  228. * Operation getApiV3TipitakaChannelsAsync
  229. *
  230. * 列出有译文的公开 channel
  231. *
  232. * @param int|null $per_page 每页数量,最大 200 (optional, default to 20)
  233. * @param string|null $type channel 类型 (optional)
  234. * @param string|null $lang 译文语族(progress_chapters.lang,zh 涵盖简繁) (optional)
  235. * @param float|null $min_progress 章节完成度门槛 0–1 (optional, default to 0.8)
  236. * @param int|null $page 页码 (optional, default to 1)
  237. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChannels'] to see the possible values for this operation
  238. *
  239. * @throws \InvalidArgumentException
  240. * @return \GuzzleHttp\Promise\PromiseInterface
  241. */
  242. public function getApiV3TipitakaChannelsAsync($per_page = 20, $type = null, $lang = null, $min_progress = 0.8, $page = 1, string $contentType = self::contentTypes['getApiV3TipitakaChannels'][0])
  243. {
  244. return $this->getApiV3TipitakaChannelsAsyncWithHttpInfo($per_page, $type, $lang, $min_progress, $page, $contentType)
  245. ->then(
  246. function ($response) {
  247. return $response[0];
  248. }
  249. );
  250. }
  251. /**
  252. * Operation getApiV3TipitakaChannelsAsyncWithHttpInfo
  253. *
  254. * 列出有译文的公开 channel
  255. *
  256. * @param int|null $per_page 每页数量,最大 200 (optional, default to 20)
  257. * @param string|null $type channel 类型 (optional)
  258. * @param string|null $lang 译文语族(progress_chapters.lang,zh 涵盖简繁) (optional)
  259. * @param float|null $min_progress 章节完成度门槛 0–1 (optional, default to 0.8)
  260. * @param int|null $page 页码 (optional, default to 1)
  261. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChannels'] to see the possible values for this operation
  262. *
  263. * @throws \InvalidArgumentException
  264. * @return \GuzzleHttp\Promise\PromiseInterface
  265. */
  266. public function getApiV3TipitakaChannelsAsyncWithHttpInfo($per_page = 20, $type = null, $lang = null, $min_progress = 0.8, $page = 1, string $contentType = self::contentTypes['getApiV3TipitakaChannels'][0])
  267. {
  268. $returnType = '\WikipaliApi\Model\GetApiV3TipitakaChannels200Response';
  269. $request = $this->getApiV3TipitakaChannelsRequest($per_page, $type, $lang, $min_progress, $page, $contentType);
  270. return $this->client
  271. ->sendAsync($request, $this->createHttpClientOption())
  272. ->then(
  273. function ($response) use ($returnType) {
  274. if ($returnType === '\SplFileObject') {
  275. $content = $response->getBody(); //stream goes to serializer
  276. } else {
  277. $content = (string) $response->getBody();
  278. if ($returnType !== 'string') {
  279. $content = json_decode($content);
  280. }
  281. }
  282. return [
  283. ObjectSerializer::deserialize($content, $returnType, []),
  284. $response->getStatusCode(),
  285. $response->getHeaders()
  286. ];
  287. },
  288. function ($exception) {
  289. $response = $exception->getResponse();
  290. $statusCode = $response->getStatusCode();
  291. throw new ApiException(
  292. sprintf(
  293. '[%d] Error connecting to the API (%s)',
  294. $statusCode,
  295. $exception->getRequest()->getUri()
  296. ),
  297. $statusCode,
  298. $response->getHeaders(),
  299. (string) $response->getBody()
  300. );
  301. }
  302. );
  303. }
  304. /**
  305. * Create request for operation 'getApiV3TipitakaChannels'
  306. *
  307. * @param int|null $per_page 每页数量,最大 200 (optional, default to 20)
  308. * @param string|null $type channel 类型 (optional)
  309. * @param string|null $lang 译文语族(progress_chapters.lang,zh 涵盖简繁) (optional)
  310. * @param float|null $min_progress 章节完成度门槛 0–1 (optional, default to 0.8)
  311. * @param int|null $page 页码 (optional, default to 1)
  312. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChannels'] to see the possible values for this operation
  313. *
  314. * @throws \InvalidArgumentException
  315. * @return \GuzzleHttp\Psr7\Request
  316. */
  317. public function getApiV3TipitakaChannelsRequest($per_page = 20, $type = null, $lang = null, $min_progress = 0.8, $page = 1, string $contentType = self::contentTypes['getApiV3TipitakaChannels'][0])
  318. {
  319. $resourcePath = '/v3/tipitaka-channels';
  320. $formParams = [];
  321. $queryParams = [];
  322. $headerParams = [];
  323. $httpBody = '';
  324. $multipart = false;
  325. // query params
  326. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  327. $per_page,
  328. 'per_page', // param base name
  329. 'integer', // openApiType
  330. 'form', // style
  331. true, // explode
  332. false // required
  333. ) ?? []);
  334. // query params
  335. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  336. $type,
  337. 'type', // param base name
  338. 'string', // openApiType
  339. 'form', // style
  340. true, // explode
  341. false // required
  342. ) ?? []);
  343. // query params
  344. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  345. $lang,
  346. 'lang', // param base name
  347. 'string', // openApiType
  348. 'form', // style
  349. true, // explode
  350. false // required
  351. ) ?? []);
  352. // query params
  353. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  354. $min_progress,
  355. 'min_progress', // param base name
  356. 'number', // openApiType
  357. 'form', // style
  358. true, // explode
  359. false // required
  360. ) ?? []);
  361. // query params
  362. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  363. $page,
  364. 'page', // param base name
  365. 'integer', // openApiType
  366. 'form', // style
  367. true, // explode
  368. false // required
  369. ) ?? []);
  370. $headers = $this->headerSelector->selectHeaders(
  371. ['application/json', 'application/problem+json', ],
  372. $contentType,
  373. $multipart
  374. );
  375. // for model (json/xml)
  376. if (count($formParams) > 0) {
  377. if ($multipart) {
  378. $multipartContents = [];
  379. foreach ($formParams as $formParamName => $formParamValue) {
  380. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  381. foreach ($formParamValueItems as $formParamValueItem) {
  382. $multipartContents[] = [
  383. 'name' => $formParamName,
  384. 'contents' => $formParamValueItem
  385. ];
  386. }
  387. }
  388. // for HTTP post (form)
  389. $httpBody = new MultipartStream($multipartContents);
  390. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  391. # if Content-Type contains "application/json", json_encode the form parameters
  392. try {
  393. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  394. } catch (\JsonException $e) {
  395. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  396. }
  397. } else {
  398. // for HTTP post (form)
  399. $httpBody = ObjectSerializer::buildQuery($formParams);
  400. }
  401. }
  402. // this endpoint requires Bearer authentication (access token)
  403. if (!empty($this->config->getAccessToken())) {
  404. $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
  405. }
  406. $defaultHeaders = [];
  407. if ($this->config->getUserAgent()) {
  408. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  409. }
  410. $headers = array_merge(
  411. $defaultHeaders,
  412. $headerParams,
  413. $headers
  414. );
  415. $operationHost = $this->config->getHost();
  416. $query = ObjectSerializer::buildQuery($queryParams);
  417. return new Request(
  418. 'GET',
  419. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  420. $headers,
  421. $httpBody
  422. );
  423. }
  424. /**
  425. * Create http client option
  426. *
  427. * @throws \RuntimeException on file opening failure
  428. * @return array of http client options
  429. */
  430. protected function createHttpClientOption()
  431. {
  432. $options = [];
  433. if ($this->config->getDebug()) {
  434. $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
  435. if (!$options[RequestOptions::DEBUG]) {
  436. throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
  437. }
  438. }
  439. if ($this->config->getCertFile()) {
  440. $options[RequestOptions::CERT] = $this->config->getCertFile();
  441. }
  442. if ($this->config->getKeyFile()) {
  443. $options[RequestOptions::SSL_KEY] = $this->config->getKeyFile();
  444. }
  445. return $options;
  446. }
  447. private function handleResponseWithDataType(
  448. string $dataType,
  449. RequestInterface $request,
  450. ResponseInterface $response
  451. ): array {
  452. if ($dataType === '\SplFileObject') {
  453. $content = $response->getBody(); //stream goes to serializer
  454. } else {
  455. $content = (string) $response->getBody();
  456. if ($dataType !== 'string') {
  457. try {
  458. $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
  459. } catch (\JsonException $exception) {
  460. throw new ApiException(
  461. sprintf(
  462. 'Error JSON decoding server response (%s)',
  463. $request->getUri()
  464. ),
  465. $response->getStatusCode(),
  466. $response->getHeaders(),
  467. $content
  468. );
  469. }
  470. }
  471. }
  472. return [
  473. ObjectSerializer::deserialize($content, $dataType, []),
  474. $response->getStatusCode(),
  475. $response->getHeaders()
  476. ];
  477. }
  478. private function responseWithinRangeCode(
  479. string $rangeCode,
  480. int $statusCode
  481. ): bool {
  482. $left = (int) ($rangeCode[0].'00');
  483. $right = (int) ($rangeCode[0].'99');
  484. return $statusCode >= $left && $statusCode <= $right;
  485. }
  486. }