GlossariesApi.php 17 KB

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