TipitakaChaptersApi.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. /**
  3. * TipitakaChaptersApi
  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: 2026.11.11
  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. * TipitakaChaptersApi Class Doc Comment
  42. *
  43. * @category Class
  44. * @package WikipaliApi
  45. * @author OpenAPI Generator team
  46. * @link https://openapi-generator.tech
  47. */
  48. class TipitakaChaptersApi
  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. 'getApiV3TipitakaChapters' => [
  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 getApiV3TipitakaChapters
  116. *
  117. * 找章节
  118. *
  119. * @param int|null $page 页码 (optional, default to 1)
  120. * @param int|null $per_page 每页数量,最大 1000 (optional, default to 50)
  121. * @param string|null $q 章节名:巴利名或译名,不给 book 时必填 (optional)
  122. * @param int|null $book 书号,不给 q 时必填 (optional)
  123. * @param int|null $para 只列 book 里这一章下面的标题(含它自己),要和 book 一起给 (optional)
  124. * @param string|null $layer 只要某几层,逗号分隔 (optional)
  125. * @param int|null $max_level 只要层级不深于它的标题(1 是书名) (optional)
  126. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChapters'] to see the possible values for this operation
  127. *
  128. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  129. * @throws \InvalidArgumentException
  130. * @return \WikipaliApi\Model\GetApiV3TipitakaChapters200Response|\WikipaliApi\Model\ProblemDetails
  131. */
  132. public function getApiV3TipitakaChapters($page = 1, $per_page = 50, $q = null, $book = null, $para = null, $layer = null, $max_level = null, string $contentType = self::contentTypes['getApiV3TipitakaChapters'][0])
  133. {
  134. list($response) = $this->getApiV3TipitakaChaptersWithHttpInfo($page, $per_page, $q, $book, $para, $layer, $max_level, $contentType);
  135. return $response;
  136. }
  137. /**
  138. * Operation getApiV3TipitakaChaptersWithHttpInfo
  139. *
  140. * 找章节
  141. *
  142. * @param int|null $page 页码 (optional, default to 1)
  143. * @param int|null $per_page 每页数量,最大 1000 (optional, default to 50)
  144. * @param string|null $q 章节名:巴利名或译名,不给 book 时必填 (optional)
  145. * @param int|null $book 书号,不给 q 时必填 (optional)
  146. * @param int|null $para 只列 book 里这一章下面的标题(含它自己),要和 book 一起给 (optional)
  147. * @param string|null $layer 只要某几层,逗号分隔 (optional)
  148. * @param int|null $max_level 只要层级不深于它的标题(1 是书名) (optional)
  149. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChapters'] to see the possible values for this operation
  150. *
  151. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  152. * @throws \InvalidArgumentException
  153. * @return array of \WikipaliApi\Model\GetApiV3TipitakaChapters200Response|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  154. */
  155. public function getApiV3TipitakaChaptersWithHttpInfo($page = 1, $per_page = 50, $q = null, $book = null, $para = null, $layer = null, $max_level = null, string $contentType = self::contentTypes['getApiV3TipitakaChapters'][0])
  156. {
  157. $request = $this->getApiV3TipitakaChaptersRequest($page, $per_page, $q, $book, $para, $layer, $max_level, $contentType);
  158. try {
  159. $options = $this->createHttpClientOption();
  160. try {
  161. $response = $this->client->send($request, $options);
  162. } catch (RequestException $e) {
  163. throw new ApiException(
  164. "[{$e->getCode()}] {$e->getMessage()}",
  165. (int) $e->getCode(),
  166. $e->getResponse() ? $e->getResponse()->getHeaders() : null,
  167. $e->getResponse() ? (string) $e->getResponse()->getBody() : null
  168. );
  169. } catch (ConnectException $e) {
  170. throw new ApiException(
  171. "[{$e->getCode()}] {$e->getMessage()}",
  172. (int) $e->getCode(),
  173. null,
  174. null
  175. );
  176. }
  177. $statusCode = $response->getStatusCode();
  178. switch($statusCode) {
  179. case 200:
  180. return $this->handleResponseWithDataType(
  181. '\WikipaliApi\Model\GetApiV3TipitakaChapters200Response',
  182. $request,
  183. $response,
  184. );
  185. case 422:
  186. return $this->handleResponseWithDataType(
  187. '\WikipaliApi\Model\ProblemDetails',
  188. $request,
  189. $response,
  190. );
  191. }
  192. if ($statusCode < 200 || $statusCode > 299) {
  193. throw new ApiException(
  194. sprintf(
  195. '[%d] Error connecting to the API (%s)',
  196. $statusCode,
  197. (string) $request->getUri()
  198. ),
  199. $statusCode,
  200. $response->getHeaders(),
  201. (string) $response->getBody()
  202. );
  203. }
  204. return $this->handleResponseWithDataType(
  205. '\WikipaliApi\Model\GetApiV3TipitakaChapters200Response',
  206. $request,
  207. $response,
  208. );
  209. } catch (ApiException $e) {
  210. switch ($e->getCode()) {
  211. case 200:
  212. $data = ObjectSerializer::deserialize(
  213. $e->getResponseBody(),
  214. '\WikipaliApi\Model\GetApiV3TipitakaChapters200Response',
  215. $e->getResponseHeaders()
  216. );
  217. $e->setResponseObject($data);
  218. throw $e;
  219. case 422:
  220. $data = ObjectSerializer::deserialize(
  221. $e->getResponseBody(),
  222. '\WikipaliApi\Model\ProblemDetails',
  223. $e->getResponseHeaders()
  224. );
  225. $e->setResponseObject($data);
  226. throw $e;
  227. }
  228. throw $e;
  229. }
  230. }
  231. /**
  232. * Operation getApiV3TipitakaChaptersAsync
  233. *
  234. * 找章节
  235. *
  236. * @param int|null $page 页码 (optional, default to 1)
  237. * @param int|null $per_page 每页数量,最大 1000 (optional, default to 50)
  238. * @param string|null $q 章节名:巴利名或译名,不给 book 时必填 (optional)
  239. * @param int|null $book 书号,不给 q 时必填 (optional)
  240. * @param int|null $para 只列 book 里这一章下面的标题(含它自己),要和 book 一起给 (optional)
  241. * @param string|null $layer 只要某几层,逗号分隔 (optional)
  242. * @param int|null $max_level 只要层级不深于它的标题(1 是书名) (optional)
  243. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChapters'] to see the possible values for this operation
  244. *
  245. * @throws \InvalidArgumentException
  246. * @return \GuzzleHttp\Promise\PromiseInterface
  247. */
  248. public function getApiV3TipitakaChaptersAsync($page = 1, $per_page = 50, $q = null, $book = null, $para = null, $layer = null, $max_level = null, string $contentType = self::contentTypes['getApiV3TipitakaChapters'][0])
  249. {
  250. return $this->getApiV3TipitakaChaptersAsyncWithHttpInfo($page, $per_page, $q, $book, $para, $layer, $max_level, $contentType)
  251. ->then(
  252. function ($response) {
  253. return $response[0];
  254. }
  255. );
  256. }
  257. /**
  258. * Operation getApiV3TipitakaChaptersAsyncWithHttpInfo
  259. *
  260. * 找章节
  261. *
  262. * @param int|null $page 页码 (optional, default to 1)
  263. * @param int|null $per_page 每页数量,最大 1000 (optional, default to 50)
  264. * @param string|null $q 章节名:巴利名或译名,不给 book 时必填 (optional)
  265. * @param int|null $book 书号,不给 q 时必填 (optional)
  266. * @param int|null $para 只列 book 里这一章下面的标题(含它自己),要和 book 一起给 (optional)
  267. * @param string|null $layer 只要某几层,逗号分隔 (optional)
  268. * @param int|null $max_level 只要层级不深于它的标题(1 是书名) (optional)
  269. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChapters'] to see the possible values for this operation
  270. *
  271. * @throws \InvalidArgumentException
  272. * @return \GuzzleHttp\Promise\PromiseInterface
  273. */
  274. public function getApiV3TipitakaChaptersAsyncWithHttpInfo($page = 1, $per_page = 50, $q = null, $book = null, $para = null, $layer = null, $max_level = null, string $contentType = self::contentTypes['getApiV3TipitakaChapters'][0])
  275. {
  276. $returnType = '\WikipaliApi\Model\GetApiV3TipitakaChapters200Response';
  277. $request = $this->getApiV3TipitakaChaptersRequest($page, $per_page, $q, $book, $para, $layer, $max_level, $contentType);
  278. return $this->client
  279. ->sendAsync($request, $this->createHttpClientOption())
  280. ->then(
  281. function ($response) use ($returnType) {
  282. if ($returnType === '\SplFileObject') {
  283. $content = $response->getBody(); //stream goes to serializer
  284. } else {
  285. $content = (string) $response->getBody();
  286. if ($returnType !== 'string') {
  287. $content = json_decode($content);
  288. }
  289. }
  290. return [
  291. ObjectSerializer::deserialize($content, $returnType, []),
  292. $response->getStatusCode(),
  293. $response->getHeaders()
  294. ];
  295. },
  296. function ($exception) {
  297. $response = $exception->getResponse();
  298. $statusCode = $response->getStatusCode();
  299. throw new ApiException(
  300. sprintf(
  301. '[%d] Error connecting to the API (%s)',
  302. $statusCode,
  303. $exception->getRequest()->getUri()
  304. ),
  305. $statusCode,
  306. $response->getHeaders(),
  307. (string) $response->getBody()
  308. );
  309. }
  310. );
  311. }
  312. /**
  313. * Create request for operation 'getApiV3TipitakaChapters'
  314. *
  315. * @param int|null $page 页码 (optional, default to 1)
  316. * @param int|null $per_page 每页数量,最大 1000 (optional, default to 50)
  317. * @param string|null $q 章节名:巴利名或译名,不给 book 时必填 (optional)
  318. * @param int|null $book 书号,不给 q 时必填 (optional)
  319. * @param int|null $para 只列 book 里这一章下面的标题(含它自己),要和 book 一起给 (optional)
  320. * @param string|null $layer 只要某几层,逗号分隔 (optional)
  321. * @param int|null $max_level 只要层级不深于它的标题(1 是书名) (optional)
  322. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaChapters'] to see the possible values for this operation
  323. *
  324. * @throws \InvalidArgumentException
  325. * @return \GuzzleHttp\Psr7\Request
  326. */
  327. public function getApiV3TipitakaChaptersRequest($page = 1, $per_page = 50, $q = null, $book = null, $para = null, $layer = null, $max_level = null, string $contentType = self::contentTypes['getApiV3TipitakaChapters'][0])
  328. {
  329. $resourcePath = '/v3/tipitaka-chapters';
  330. $formParams = [];
  331. $queryParams = [];
  332. $headerParams = [];
  333. $httpBody = '';
  334. $multipart = false;
  335. // query params
  336. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  337. $page,
  338. 'page', // param base name
  339. 'integer', // openApiType
  340. 'form', // style
  341. true, // explode
  342. false // required
  343. ) ?? []);
  344. // query params
  345. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  346. $per_page,
  347. 'per_page', // param base name
  348. 'integer', // openApiType
  349. 'form', // style
  350. true, // explode
  351. false // required
  352. ) ?? []);
  353. // query params
  354. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  355. $q,
  356. 'q', // param base name
  357. 'string', // openApiType
  358. 'form', // style
  359. true, // explode
  360. false // required
  361. ) ?? []);
  362. // query params
  363. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  364. $book,
  365. 'book', // param base name
  366. 'integer', // openApiType
  367. 'form', // style
  368. true, // explode
  369. false // required
  370. ) ?? []);
  371. // query params
  372. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  373. $para,
  374. 'para', // param base name
  375. 'integer', // openApiType
  376. 'form', // style
  377. true, // explode
  378. false // required
  379. ) ?? []);
  380. // query params
  381. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  382. $layer,
  383. 'layer', // param base name
  384. 'string', // openApiType
  385. 'form', // style
  386. true, // explode
  387. false // required
  388. ) ?? []);
  389. // query params
  390. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  391. $max_level,
  392. 'max_level', // param base name
  393. 'integer', // openApiType
  394. 'form', // style
  395. true, // explode
  396. false // required
  397. ) ?? []);
  398. $headers = $this->headerSelector->selectHeaders(
  399. ['application/json', 'application/problem+json', ],
  400. $contentType,
  401. $multipart
  402. );
  403. // for model (json/xml)
  404. if (count($formParams) > 0) {
  405. if ($multipart) {
  406. $multipartContents = [];
  407. foreach ($formParams as $formParamName => $formParamValue) {
  408. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  409. foreach ($formParamValueItems as $formParamValueItem) {
  410. $multipartContents[] = [
  411. 'name' => $formParamName,
  412. 'contents' => $formParamValueItem
  413. ];
  414. }
  415. }
  416. // for HTTP post (form)
  417. $httpBody = new MultipartStream($multipartContents);
  418. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  419. # if Content-Type contains "application/json", json_encode the form parameters
  420. try {
  421. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  422. } catch (\JsonException $e) {
  423. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  424. }
  425. } else {
  426. // for HTTP post (form)
  427. $httpBody = ObjectSerializer::buildQuery($formParams);
  428. }
  429. }
  430. // this endpoint requires Bearer authentication (access token)
  431. if (!empty($this->config->getAccessToken())) {
  432. $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
  433. }
  434. $defaultHeaders = [];
  435. if ($this->config->getUserAgent()) {
  436. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  437. }
  438. $headers = array_merge(
  439. $defaultHeaders,
  440. $headerParams,
  441. $headers
  442. );
  443. $operationHost = $this->config->getHost();
  444. $query = ObjectSerializer::buildQuery($queryParams);
  445. return new Request(
  446. 'GET',
  447. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  448. $headers,
  449. $httpBody
  450. );
  451. }
  452. /**
  453. * Create http client option
  454. *
  455. * @throws \RuntimeException on file opening failure
  456. * @return array of http client options
  457. */
  458. protected function createHttpClientOption()
  459. {
  460. $options = [];
  461. if ($this->config->getDebug()) {
  462. $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
  463. if (!$options[RequestOptions::DEBUG]) {
  464. throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
  465. }
  466. }
  467. if ($this->config->getCertFile()) {
  468. $options[RequestOptions::CERT] = $this->config->getCertFile();
  469. }
  470. if ($this->config->getKeyFile()) {
  471. $options[RequestOptions::SSL_KEY] = $this->config->getKeyFile();
  472. }
  473. return $options;
  474. }
  475. private function handleResponseWithDataType(
  476. string $dataType,
  477. RequestInterface $request,
  478. ResponseInterface $response
  479. ): array {
  480. if ($dataType === '\SplFileObject') {
  481. $content = $response->getBody(); //stream goes to serializer
  482. } else {
  483. $content = (string) $response->getBody();
  484. if ($dataType !== 'string') {
  485. try {
  486. $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
  487. } catch (\JsonException $exception) {
  488. throw new ApiException(
  489. sprintf(
  490. 'Error JSON decoding server response (%s)',
  491. $request->getUri()
  492. ),
  493. $response->getStatusCode(),
  494. $response->getHeaders(),
  495. $content
  496. );
  497. }
  498. }
  499. }
  500. return [
  501. ObjectSerializer::deserialize($content, $dataType, []),
  502. $response->getStatusCode(),
  503. $response->getHeaders()
  504. ];
  505. }
  506. private function responseWithinRangeCode(
  507. string $rangeCode,
  508. int $statusCode
  509. ): bool {
  510. $left = (int) ($rangeCode[0].'00');
  511. $right = (int) ($rangeCode[0].'99');
  512. return $statusCode >= $left && $statusCode <= $right;
  513. }
  514. }