TipitakaPageNumbersApi.php 18 KB

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