PaliWordSuggestionsApi.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /**
  3. * PaliWordSuggestionsApi
  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. * PaliWordSuggestionsApi Class Doc Comment
  42. *
  43. * @category Class
  44. * @package WikipaliApi
  45. * @author OpenAPI Generator team
  46. * @link https://openapi-generator.tech
  47. */
  48. class PaliWordSuggestionsApi
  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. 'getApiV3PaliWordSuggestions' => [
  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 getApiV3PaliWordSuggestions
  116. *
  117. * 巴利词补全
  118. *
  119. * @param string $q 已输入的前缀 (required)
  120. * @param int|null $limit 最多返回几条,上限 50 (optional, default to 10)
  121. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3PaliWordSuggestions'] 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\GetApiV3PaliWordSuggestions200Response|\WikipaliApi\Model\ProblemDetails
  126. */
  127. public function getApiV3PaliWordSuggestions($q, $limit = 10, string $contentType = self::contentTypes['getApiV3PaliWordSuggestions'][0])
  128. {
  129. list($response) = $this->getApiV3PaliWordSuggestionsWithHttpInfo($q, $limit, $contentType);
  130. return $response;
  131. }
  132. /**
  133. * Operation getApiV3PaliWordSuggestionsWithHttpInfo
  134. *
  135. * 巴利词补全
  136. *
  137. * @param string $q 已输入的前缀 (required)
  138. * @param int|null $limit 最多返回几条,上限 50 (optional, default to 10)
  139. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3PaliWordSuggestions'] 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\GetApiV3PaliWordSuggestions200Response|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  144. */
  145. public function getApiV3PaliWordSuggestionsWithHttpInfo($q, $limit = 10, string $contentType = self::contentTypes['getApiV3PaliWordSuggestions'][0])
  146. {
  147. $request = $this->getApiV3PaliWordSuggestionsRequest($q, $limit, $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\GetApiV3PaliWordSuggestions200Response',
  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\GetApiV3PaliWordSuggestions200Response',
  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\GetApiV3PaliWordSuggestions200Response',
  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 getApiV3PaliWordSuggestionsAsync
  223. *
  224. * 巴利词补全
  225. *
  226. * @param string $q 已输入的前缀 (required)
  227. * @param int|null $limit 最多返回几条,上限 50 (optional, default to 10)
  228. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3PaliWordSuggestions'] to see the possible values for this operation
  229. *
  230. * @throws \InvalidArgumentException
  231. * @return \GuzzleHttp\Promise\PromiseInterface
  232. */
  233. public function getApiV3PaliWordSuggestionsAsync($q, $limit = 10, string $contentType = self::contentTypes['getApiV3PaliWordSuggestions'][0])
  234. {
  235. return $this->getApiV3PaliWordSuggestionsAsyncWithHttpInfo($q, $limit, $contentType)
  236. ->then(
  237. function ($response) {
  238. return $response[0];
  239. }
  240. );
  241. }
  242. /**
  243. * Operation getApiV3PaliWordSuggestionsAsyncWithHttpInfo
  244. *
  245. * 巴利词补全
  246. *
  247. * @param string $q 已输入的前缀 (required)
  248. * @param int|null $limit 最多返回几条,上限 50 (optional, default to 10)
  249. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3PaliWordSuggestions'] to see the possible values for this operation
  250. *
  251. * @throws \InvalidArgumentException
  252. * @return \GuzzleHttp\Promise\PromiseInterface
  253. */
  254. public function getApiV3PaliWordSuggestionsAsyncWithHttpInfo($q, $limit = 10, string $contentType = self::contentTypes['getApiV3PaliWordSuggestions'][0])
  255. {
  256. $returnType = '\WikipaliApi\Model\GetApiV3PaliWordSuggestions200Response';
  257. $request = $this->getApiV3PaliWordSuggestionsRequest($q, $limit, $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 'getApiV3PaliWordSuggestions'
  294. *
  295. * @param string $q 已输入的前缀 (required)
  296. * @param int|null $limit 最多返回几条,上限 50 (optional, default to 10)
  297. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3PaliWordSuggestions'] to see the possible values for this operation
  298. *
  299. * @throws \InvalidArgumentException
  300. * @return \GuzzleHttp\Psr7\Request
  301. */
  302. public function getApiV3PaliWordSuggestionsRequest($q, $limit = 10, string $contentType = self::contentTypes['getApiV3PaliWordSuggestions'][0])
  303. {
  304. // verify the required parameter 'q' is set
  305. if ($q === null || (is_array($q) && count($q) === 0)) {
  306. throw new \InvalidArgumentException(
  307. 'Missing the required parameter $q when calling getApiV3PaliWordSuggestions'
  308. );
  309. }
  310. $resourcePath = '/v3/pali-word-suggestions';
  311. $formParams = [];
  312. $queryParams = [];
  313. $headerParams = [];
  314. $httpBody = '';
  315. $multipart = false;
  316. // query params
  317. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  318. $limit,
  319. 'limit', // param base name
  320. 'integer', // openApiType
  321. 'form', // style
  322. true, // explode
  323. false // required
  324. ) ?? []);
  325. // query params
  326. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  327. $q,
  328. 'q', // param base name
  329. 'string', // openApiType
  330. 'form', // style
  331. true, // explode
  332. true // required
  333. ) ?? []);
  334. $headers = $this->headerSelector->selectHeaders(
  335. ['application/json', 'application/problem+json', ],
  336. $contentType,
  337. $multipart
  338. );
  339. // for model (json/xml)
  340. if (count($formParams) > 0) {
  341. if ($multipart) {
  342. $multipartContents = [];
  343. foreach ($formParams as $formParamName => $formParamValue) {
  344. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  345. foreach ($formParamValueItems as $formParamValueItem) {
  346. $multipartContents[] = [
  347. 'name' => $formParamName,
  348. 'contents' => $formParamValueItem
  349. ];
  350. }
  351. }
  352. // for HTTP post (form)
  353. $httpBody = new MultipartStream($multipartContents);
  354. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  355. # if Content-Type contains "application/json", json_encode the form parameters
  356. try {
  357. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  358. } catch (\JsonException $e) {
  359. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  360. }
  361. } else {
  362. // for HTTP post (form)
  363. $httpBody = ObjectSerializer::buildQuery($formParams);
  364. }
  365. }
  366. // this endpoint requires Bearer authentication (access token)
  367. if (!empty($this->config->getAccessToken())) {
  368. $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
  369. }
  370. $defaultHeaders = [];
  371. if ($this->config->getUserAgent()) {
  372. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  373. }
  374. $headers = array_merge(
  375. $defaultHeaders,
  376. $headerParams,
  377. $headers
  378. );
  379. $operationHost = $this->config->getHost();
  380. $query = ObjectSerializer::buildQuery($queryParams);
  381. return new Request(
  382. 'GET',
  383. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  384. $headers,
  385. $httpBody
  386. );
  387. }
  388. /**
  389. * Create http client option
  390. *
  391. * @throws \RuntimeException on file opening failure
  392. * @return array of http client options
  393. */
  394. protected function createHttpClientOption()
  395. {
  396. $options = [];
  397. if ($this->config->getDebug()) {
  398. $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
  399. if (!$options[RequestOptions::DEBUG]) {
  400. throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
  401. }
  402. }
  403. if ($this->config->getCertFile()) {
  404. $options[RequestOptions::CERT] = $this->config->getCertFile();
  405. }
  406. if ($this->config->getKeyFile()) {
  407. $options[RequestOptions::SSL_KEY] = $this->config->getKeyFile();
  408. }
  409. return $options;
  410. }
  411. private function handleResponseWithDataType(
  412. string $dataType,
  413. RequestInterface $request,
  414. ResponseInterface $response
  415. ): array {
  416. if ($dataType === '\SplFileObject') {
  417. $content = $response->getBody(); //stream goes to serializer
  418. } else {
  419. $content = (string) $response->getBody();
  420. if ($dataType !== 'string') {
  421. try {
  422. $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
  423. } catch (\JsonException $exception) {
  424. throw new ApiException(
  425. sprintf(
  426. 'Error JSON decoding server response (%s)',
  427. $request->getUri()
  428. ),
  429. $response->getStatusCode(),
  430. $response->getHeaders(),
  431. $content
  432. );
  433. }
  434. }
  435. }
  436. return [
  437. ObjectSerializer::deserialize($content, $dataType, []),
  438. $response->getStatusCode(),
  439. $response->getHeaders()
  440. ];
  441. }
  442. private function responseWithinRangeCode(
  443. string $rangeCode,
  444. int $statusCode
  445. ): bool {
  446. $left = (int) ($rangeCode[0].'00');
  447. $right = (int) ($rangeCode[0].'99');
  448. return $statusCode >= $left && $statusCode <= $right;
  449. }
  450. }