SearchSuggestApi.php 19 KB

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