SearchApi.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. <?php
  2. /**
  3. * SearchApi
  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.9.28
  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. * SearchApi Class Doc Comment
  42. *
  43. * @category Class
  44. * @package WikipaliApi
  45. * @author OpenAPI Generator team
  46. * @link https://openapi-generator.tech
  47. */
  48. class SearchApi
  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. 'getApiV3Search' => [
  69. 'application/json',
  70. ],
  71. 'getApiV3SearchSearch' => [
  72. 'application/json',
  73. ],
  74. 'postApiV3Search' => [
  75. 'application/json',
  76. ],
  77. ];
  78. /**
  79. * @param ClientInterface $client
  80. * @param Configuration $config
  81. * @param HeaderSelector $selector
  82. * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
  83. */
  84. public function __construct(
  85. ?ClientInterface $client = null,
  86. ?Configuration $config = null,
  87. ?HeaderSelector $selector = null,
  88. int $hostIndex = 0
  89. ) {
  90. $this->client = $client ?: new Client();
  91. $this->config = $config ?: Configuration::getDefaultConfiguration();
  92. $this->headerSelector = $selector ?: new HeaderSelector();
  93. $this->hostIndex = $hostIndex;
  94. }
  95. /**
  96. * Set the host index
  97. *
  98. * @param int $hostIndex Host index (required)
  99. */
  100. public function setHostIndex($hostIndex): void
  101. {
  102. $this->hostIndex = $hostIndex;
  103. }
  104. /**
  105. * Get the host index
  106. *
  107. * @return int Host index
  108. */
  109. public function getHostIndex()
  110. {
  111. return $this->hostIndex;
  112. }
  113. /**
  114. * @return Configuration
  115. */
  116. public function getConfig()
  117. {
  118. return $this->config;
  119. }
  120. /**
  121. * Operation getApiV3Search
  122. *
  123. * 全文检索
  124. *
  125. * @param string $q 搜索关键词 (required)
  126. * @param int|null $page 页码 (optional, default to 1)
  127. * @param int|null $page_size 每页数量,上限 100 (optional, default to 20)
  128. * @param string|null $search_mode 检索模式 (optional, default to 'fuzzy')
  129. * @param string|null $resource_type 资源类型 (optional)
  130. * @param string|null $resource_id 限定某个资源 id (optional)
  131. * @param string|null $granularity 文档颗粒度 (optional)
  132. * @param string|null $language 语言 (optional)
  133. * @param string|null $category 文档分类,逗号分隔 (optional)
  134. * @param string|null $tags 标签过滤,逗号分隔 (optional)
  135. * @param string[]|null $page_refs 页码标记 (optional)
  136. * @param string[]|null $related_id 关联 id (optional)
  137. * @param string|null $author 按作者或译者过滤 (optional)
  138. * @param string|null $channel 按来源 channel 过滤 (optional)
  139. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3Search'] 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 \WikipaliApi\Model\GetApiV3Search200Response|\WikipaliApi\Model\ProblemDetails
  144. */
  145. public function getApiV3Search($q, $page = 1, $page_size = 20, $search_mode = 'fuzzy', $resource_type = null, $resource_id = null, $granularity = null, $language = null, $category = null, $tags = null, $page_refs = null, $related_id = null, $author = null, $channel = null, string $contentType = self::contentTypes['getApiV3Search'][0])
  146. {
  147. list($response) = $this->getApiV3SearchWithHttpInfo($q, $page, $page_size, $search_mode, $resource_type, $resource_id, $granularity, $language, $category, $tags, $page_refs, $related_id, $author, $channel, $contentType);
  148. return $response;
  149. }
  150. /**
  151. * Operation getApiV3SearchWithHttpInfo
  152. *
  153. * 全文检索
  154. *
  155. * @param string $q 搜索关键词 (required)
  156. * @param int|null $page 页码 (optional, default to 1)
  157. * @param int|null $page_size 每页数量,上限 100 (optional, default to 20)
  158. * @param string|null $search_mode 检索模式 (optional, default to 'fuzzy')
  159. * @param string|null $resource_type 资源类型 (optional)
  160. * @param string|null $resource_id 限定某个资源 id (optional)
  161. * @param string|null $granularity 文档颗粒度 (optional)
  162. * @param string|null $language 语言 (optional)
  163. * @param string|null $category 文档分类,逗号分隔 (optional)
  164. * @param string|null $tags 标签过滤,逗号分隔 (optional)
  165. * @param string[]|null $page_refs 页码标记 (optional)
  166. * @param string[]|null $related_id 关联 id (optional)
  167. * @param string|null $author 按作者或译者过滤 (optional)
  168. * @param string|null $channel 按来源 channel 过滤 (optional)
  169. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3Search'] to see the possible values for this operation
  170. *
  171. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  172. * @throws \InvalidArgumentException
  173. * @return array of \WikipaliApi\Model\GetApiV3Search200Response|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  174. */
  175. public function getApiV3SearchWithHttpInfo($q, $page = 1, $page_size = 20, $search_mode = 'fuzzy', $resource_type = null, $resource_id = null, $granularity = null, $language = null, $category = null, $tags = null, $page_refs = null, $related_id = null, $author = null, $channel = null, string $contentType = self::contentTypes['getApiV3Search'][0])
  176. {
  177. $request = $this->getApiV3SearchRequest($q, $page, $page_size, $search_mode, $resource_type, $resource_id, $granularity, $language, $category, $tags, $page_refs, $related_id, $author, $channel, $contentType);
  178. try {
  179. $options = $this->createHttpClientOption();
  180. try {
  181. $response = $this->client->send($request, $options);
  182. } catch (RequestException $e) {
  183. throw new ApiException(
  184. "[{$e->getCode()}] {$e->getMessage()}",
  185. (int) $e->getCode(),
  186. $e->getResponse() ? $e->getResponse()->getHeaders() : null,
  187. $e->getResponse() ? (string) $e->getResponse()->getBody() : null
  188. );
  189. } catch (ConnectException $e) {
  190. throw new ApiException(
  191. "[{$e->getCode()}] {$e->getMessage()}",
  192. (int) $e->getCode(),
  193. null,
  194. null
  195. );
  196. }
  197. $statusCode = $response->getStatusCode();
  198. switch($statusCode) {
  199. case 200:
  200. return $this->handleResponseWithDataType(
  201. '\WikipaliApi\Model\GetApiV3Search200Response',
  202. $request,
  203. $response,
  204. );
  205. case 422:
  206. return $this->handleResponseWithDataType(
  207. '\WikipaliApi\Model\ProblemDetails',
  208. $request,
  209. $response,
  210. );
  211. }
  212. if ($statusCode < 200 || $statusCode > 299) {
  213. throw new ApiException(
  214. sprintf(
  215. '[%d] Error connecting to the API (%s)',
  216. $statusCode,
  217. (string) $request->getUri()
  218. ),
  219. $statusCode,
  220. $response->getHeaders(),
  221. (string) $response->getBody()
  222. );
  223. }
  224. return $this->handleResponseWithDataType(
  225. '\WikipaliApi\Model\GetApiV3Search200Response',
  226. $request,
  227. $response,
  228. );
  229. } catch (ApiException $e) {
  230. switch ($e->getCode()) {
  231. case 200:
  232. $data = ObjectSerializer::deserialize(
  233. $e->getResponseBody(),
  234. '\WikipaliApi\Model\GetApiV3Search200Response',
  235. $e->getResponseHeaders()
  236. );
  237. $e->setResponseObject($data);
  238. throw $e;
  239. case 422:
  240. $data = ObjectSerializer::deserialize(
  241. $e->getResponseBody(),
  242. '\WikipaliApi\Model\ProblemDetails',
  243. $e->getResponseHeaders()
  244. );
  245. $e->setResponseObject($data);
  246. throw $e;
  247. }
  248. throw $e;
  249. }
  250. }
  251. /**
  252. * Operation getApiV3SearchAsync
  253. *
  254. * 全文检索
  255. *
  256. * @param string $q 搜索关键词 (required)
  257. * @param int|null $page 页码 (optional, default to 1)
  258. * @param int|null $page_size 每页数量,上限 100 (optional, default to 20)
  259. * @param string|null $search_mode 检索模式 (optional, default to 'fuzzy')
  260. * @param string|null $resource_type 资源类型 (optional)
  261. * @param string|null $resource_id 限定某个资源 id (optional)
  262. * @param string|null $granularity 文档颗粒度 (optional)
  263. * @param string|null $language 语言 (optional)
  264. * @param string|null $category 文档分类,逗号分隔 (optional)
  265. * @param string|null $tags 标签过滤,逗号分隔 (optional)
  266. * @param string[]|null $page_refs 页码标记 (optional)
  267. * @param string[]|null $related_id 关联 id (optional)
  268. * @param string|null $author 按作者或译者过滤 (optional)
  269. * @param string|null $channel 按来源 channel 过滤 (optional)
  270. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3Search'] to see the possible values for this operation
  271. *
  272. * @throws \InvalidArgumentException
  273. * @return \GuzzleHttp\Promise\PromiseInterface
  274. */
  275. public function getApiV3SearchAsync($q, $page = 1, $page_size = 20, $search_mode = 'fuzzy', $resource_type = null, $resource_id = null, $granularity = null, $language = null, $category = null, $tags = null, $page_refs = null, $related_id = null, $author = null, $channel = null, string $contentType = self::contentTypes['getApiV3Search'][0])
  276. {
  277. return $this->getApiV3SearchAsyncWithHttpInfo($q, $page, $page_size, $search_mode, $resource_type, $resource_id, $granularity, $language, $category, $tags, $page_refs, $related_id, $author, $channel, $contentType)
  278. ->then(
  279. function ($response) {
  280. return $response[0];
  281. }
  282. );
  283. }
  284. /**
  285. * Operation getApiV3SearchAsyncWithHttpInfo
  286. *
  287. * 全文检索
  288. *
  289. * @param string $q 搜索关键词 (required)
  290. * @param int|null $page 页码 (optional, default to 1)
  291. * @param int|null $page_size 每页数量,上限 100 (optional, default to 20)
  292. * @param string|null $search_mode 检索模式 (optional, default to 'fuzzy')
  293. * @param string|null $resource_type 资源类型 (optional)
  294. * @param string|null $resource_id 限定某个资源 id (optional)
  295. * @param string|null $granularity 文档颗粒度 (optional)
  296. * @param string|null $language 语言 (optional)
  297. * @param string|null $category 文档分类,逗号分隔 (optional)
  298. * @param string|null $tags 标签过滤,逗号分隔 (optional)
  299. * @param string[]|null $page_refs 页码标记 (optional)
  300. * @param string[]|null $related_id 关联 id (optional)
  301. * @param string|null $author 按作者或译者过滤 (optional)
  302. * @param string|null $channel 按来源 channel 过滤 (optional)
  303. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3Search'] to see the possible values for this operation
  304. *
  305. * @throws \InvalidArgumentException
  306. * @return \GuzzleHttp\Promise\PromiseInterface
  307. */
  308. public function getApiV3SearchAsyncWithHttpInfo($q, $page = 1, $page_size = 20, $search_mode = 'fuzzy', $resource_type = null, $resource_id = null, $granularity = null, $language = null, $category = null, $tags = null, $page_refs = null, $related_id = null, $author = null, $channel = null, string $contentType = self::contentTypes['getApiV3Search'][0])
  309. {
  310. $returnType = '\WikipaliApi\Model\GetApiV3Search200Response';
  311. $request = $this->getApiV3SearchRequest($q, $page, $page_size, $search_mode, $resource_type, $resource_id, $granularity, $language, $category, $tags, $page_refs, $related_id, $author, $channel, $contentType);
  312. return $this->client
  313. ->sendAsync($request, $this->createHttpClientOption())
  314. ->then(
  315. function ($response) use ($returnType) {
  316. if ($returnType === '\SplFileObject') {
  317. $content = $response->getBody(); //stream goes to serializer
  318. } else {
  319. $content = (string) $response->getBody();
  320. if ($returnType !== 'string') {
  321. $content = json_decode($content);
  322. }
  323. }
  324. return [
  325. ObjectSerializer::deserialize($content, $returnType, []),
  326. $response->getStatusCode(),
  327. $response->getHeaders()
  328. ];
  329. },
  330. function ($exception) {
  331. $response = $exception->getResponse();
  332. $statusCode = $response->getStatusCode();
  333. throw new ApiException(
  334. sprintf(
  335. '[%d] Error connecting to the API (%s)',
  336. $statusCode,
  337. $exception->getRequest()->getUri()
  338. ),
  339. $statusCode,
  340. $response->getHeaders(),
  341. (string) $response->getBody()
  342. );
  343. }
  344. );
  345. }
  346. /**
  347. * Create request for operation 'getApiV3Search'
  348. *
  349. * @param string $q 搜索关键词 (required)
  350. * @param int|null $page 页码 (optional, default to 1)
  351. * @param int|null $page_size 每页数量,上限 100 (optional, default to 20)
  352. * @param string|null $search_mode 检索模式 (optional, default to 'fuzzy')
  353. * @param string|null $resource_type 资源类型 (optional)
  354. * @param string|null $resource_id 限定某个资源 id (optional)
  355. * @param string|null $granularity 文档颗粒度 (optional)
  356. * @param string|null $language 语言 (optional)
  357. * @param string|null $category 文档分类,逗号分隔 (optional)
  358. * @param string|null $tags 标签过滤,逗号分隔 (optional)
  359. * @param string[]|null $page_refs 页码标记 (optional)
  360. * @param string[]|null $related_id 关联 id (optional)
  361. * @param string|null $author 按作者或译者过滤 (optional)
  362. * @param string|null $channel 按来源 channel 过滤 (optional)
  363. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3Search'] to see the possible values for this operation
  364. *
  365. * @throws \InvalidArgumentException
  366. * @return \GuzzleHttp\Psr7\Request
  367. */
  368. public function getApiV3SearchRequest($q, $page = 1, $page_size = 20, $search_mode = 'fuzzy', $resource_type = null, $resource_id = null, $granularity = null, $language = null, $category = null, $tags = null, $page_refs = null, $related_id = null, $author = null, $channel = null, string $contentType = self::contentTypes['getApiV3Search'][0])
  369. {
  370. // verify the required parameter 'q' is set
  371. if ($q === null || (is_array($q) && count($q) === 0)) {
  372. throw new \InvalidArgumentException(
  373. 'Missing the required parameter $q when calling getApiV3Search'
  374. );
  375. }
  376. $resourcePath = '/v3/search';
  377. $formParams = [];
  378. $queryParams = [];
  379. $headerParams = [];
  380. $httpBody = '';
  381. $multipart = false;
  382. // query params
  383. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  384. $q,
  385. 'q', // param base name
  386. 'string', // openApiType
  387. 'form', // style
  388. true, // explode
  389. true // required
  390. ) ?? []);
  391. // query params
  392. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  393. $page,
  394. 'page', // param base name
  395. 'integer', // openApiType
  396. 'form', // style
  397. true, // explode
  398. false // required
  399. ) ?? []);
  400. // query params
  401. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  402. $page_size,
  403. 'page_size', // param base name
  404. 'integer', // openApiType
  405. 'form', // style
  406. true, // explode
  407. false // required
  408. ) ?? []);
  409. // query params
  410. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  411. $search_mode,
  412. 'search_mode', // param base name
  413. 'string', // openApiType
  414. 'form', // style
  415. true, // explode
  416. false // required
  417. ) ?? []);
  418. // query params
  419. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  420. $resource_type,
  421. 'resource_type', // param base name
  422. 'string', // openApiType
  423. 'form', // style
  424. true, // explode
  425. false // required
  426. ) ?? []);
  427. // query params
  428. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  429. $resource_id,
  430. 'resource_id', // param base name
  431. 'string', // openApiType
  432. 'form', // style
  433. true, // explode
  434. false // required
  435. ) ?? []);
  436. // query params
  437. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  438. $granularity,
  439. 'granularity', // param base name
  440. 'string', // openApiType
  441. 'form', // style
  442. true, // explode
  443. false // required
  444. ) ?? []);
  445. // query params
  446. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  447. $language,
  448. 'language', // param base name
  449. 'string', // openApiType
  450. 'form', // style
  451. true, // explode
  452. false // required
  453. ) ?? []);
  454. // query params
  455. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  456. $category,
  457. 'category', // param base name
  458. 'string', // openApiType
  459. 'form', // style
  460. true, // explode
  461. false // required
  462. ) ?? []);
  463. // query params
  464. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  465. $tags,
  466. 'tags', // param base name
  467. 'string', // openApiType
  468. 'form', // style
  469. true, // explode
  470. false // required
  471. ) ?? []);
  472. // query params
  473. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  474. $page_refs,
  475. 'page_refs', // param base name
  476. 'array', // openApiType
  477. 'form', // style
  478. true, // explode
  479. false // required
  480. ) ?? []);
  481. // query params
  482. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  483. $related_id,
  484. 'related_id', // param base name
  485. 'array', // openApiType
  486. 'form', // style
  487. true, // explode
  488. false // required
  489. ) ?? []);
  490. // query params
  491. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  492. $author,
  493. 'author', // param base name
  494. 'string', // openApiType
  495. 'form', // style
  496. true, // explode
  497. false // required
  498. ) ?? []);
  499. // query params
  500. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  501. $channel,
  502. 'channel', // param base name
  503. 'string', // openApiType
  504. 'form', // style
  505. true, // explode
  506. false // required
  507. ) ?? []);
  508. $headers = $this->headerSelector->selectHeaders(
  509. ['application/json', 'application/problem+json', ],
  510. $contentType,
  511. $multipart
  512. );
  513. // for model (json/xml)
  514. if (count($formParams) > 0) {
  515. if ($multipart) {
  516. $multipartContents = [];
  517. foreach ($formParams as $formParamName => $formParamValue) {
  518. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  519. foreach ($formParamValueItems as $formParamValueItem) {
  520. $multipartContents[] = [
  521. 'name' => $formParamName,
  522. 'contents' => $formParamValueItem
  523. ];
  524. }
  525. }
  526. // for HTTP post (form)
  527. $httpBody = new MultipartStream($multipartContents);
  528. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  529. # if Content-Type contains "application/json", json_encode the form parameters
  530. try {
  531. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  532. } catch (\JsonException $e) {
  533. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  534. }
  535. } else {
  536. // for HTTP post (form)
  537. $httpBody = ObjectSerializer::buildQuery($formParams);
  538. }
  539. }
  540. $defaultHeaders = [];
  541. if ($this->config->getUserAgent()) {
  542. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  543. }
  544. $headers = array_merge(
  545. $defaultHeaders,
  546. $headerParams,
  547. $headers
  548. );
  549. $operationHost = $this->config->getHost();
  550. $query = ObjectSerializer::buildQuery($queryParams);
  551. return new Request(
  552. 'GET',
  553. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  554. $headers,
  555. $httpBody
  556. );
  557. }
  558. /**
  559. * Operation getApiV3SearchSearch
  560. *
  561. * 按 id 取单条检索结果
  562. *
  563. * @param string $search OpenSearch 文档 id (required)
  564. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3SearchSearch'] to see the possible values for this operation
  565. *
  566. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  567. * @throws \InvalidArgumentException
  568. * @return \WikipaliApi\Model\GetApiV3SearchSearch200Response|\WikipaliApi\Model\ProblemDetails
  569. */
  570. public function getApiV3SearchSearch($search, string $contentType = self::contentTypes['getApiV3SearchSearch'][0])
  571. {
  572. list($response) = $this->getApiV3SearchSearchWithHttpInfo($search, $contentType);
  573. return $response;
  574. }
  575. /**
  576. * Operation getApiV3SearchSearchWithHttpInfo
  577. *
  578. * 按 id 取单条检索结果
  579. *
  580. * @param string $search OpenSearch 文档 id (required)
  581. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3SearchSearch'] to see the possible values for this operation
  582. *
  583. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  584. * @throws \InvalidArgumentException
  585. * @return array of \WikipaliApi\Model\GetApiV3SearchSearch200Response|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  586. */
  587. public function getApiV3SearchSearchWithHttpInfo($search, string $contentType = self::contentTypes['getApiV3SearchSearch'][0])
  588. {
  589. $request = $this->getApiV3SearchSearchRequest($search, $contentType);
  590. try {
  591. $options = $this->createHttpClientOption();
  592. try {
  593. $response = $this->client->send($request, $options);
  594. } catch (RequestException $e) {
  595. throw new ApiException(
  596. "[{$e->getCode()}] {$e->getMessage()}",
  597. (int) $e->getCode(),
  598. $e->getResponse() ? $e->getResponse()->getHeaders() : null,
  599. $e->getResponse() ? (string) $e->getResponse()->getBody() : null
  600. );
  601. } catch (ConnectException $e) {
  602. throw new ApiException(
  603. "[{$e->getCode()}] {$e->getMessage()}",
  604. (int) $e->getCode(),
  605. null,
  606. null
  607. );
  608. }
  609. $statusCode = $response->getStatusCode();
  610. switch($statusCode) {
  611. case 200:
  612. return $this->handleResponseWithDataType(
  613. '\WikipaliApi\Model\GetApiV3SearchSearch200Response',
  614. $request,
  615. $response,
  616. );
  617. case 422:
  618. return $this->handleResponseWithDataType(
  619. '\WikipaliApi\Model\ProblemDetails',
  620. $request,
  621. $response,
  622. );
  623. }
  624. if ($statusCode < 200 || $statusCode > 299) {
  625. throw new ApiException(
  626. sprintf(
  627. '[%d] Error connecting to the API (%s)',
  628. $statusCode,
  629. (string) $request->getUri()
  630. ),
  631. $statusCode,
  632. $response->getHeaders(),
  633. (string) $response->getBody()
  634. );
  635. }
  636. return $this->handleResponseWithDataType(
  637. '\WikipaliApi\Model\GetApiV3SearchSearch200Response',
  638. $request,
  639. $response,
  640. );
  641. } catch (ApiException $e) {
  642. switch ($e->getCode()) {
  643. case 200:
  644. $data = ObjectSerializer::deserialize(
  645. $e->getResponseBody(),
  646. '\WikipaliApi\Model\GetApiV3SearchSearch200Response',
  647. $e->getResponseHeaders()
  648. );
  649. $e->setResponseObject($data);
  650. throw $e;
  651. case 422:
  652. $data = ObjectSerializer::deserialize(
  653. $e->getResponseBody(),
  654. '\WikipaliApi\Model\ProblemDetails',
  655. $e->getResponseHeaders()
  656. );
  657. $e->setResponseObject($data);
  658. throw $e;
  659. }
  660. throw $e;
  661. }
  662. }
  663. /**
  664. * Operation getApiV3SearchSearchAsync
  665. *
  666. * 按 id 取单条检索结果
  667. *
  668. * @param string $search OpenSearch 文档 id (required)
  669. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3SearchSearch'] to see the possible values for this operation
  670. *
  671. * @throws \InvalidArgumentException
  672. * @return \GuzzleHttp\Promise\PromiseInterface
  673. */
  674. public function getApiV3SearchSearchAsync($search, string $contentType = self::contentTypes['getApiV3SearchSearch'][0])
  675. {
  676. return $this->getApiV3SearchSearchAsyncWithHttpInfo($search, $contentType)
  677. ->then(
  678. function ($response) {
  679. return $response[0];
  680. }
  681. );
  682. }
  683. /**
  684. * Operation getApiV3SearchSearchAsyncWithHttpInfo
  685. *
  686. * 按 id 取单条检索结果
  687. *
  688. * @param string $search OpenSearch 文档 id (required)
  689. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3SearchSearch'] to see the possible values for this operation
  690. *
  691. * @throws \InvalidArgumentException
  692. * @return \GuzzleHttp\Promise\PromiseInterface
  693. */
  694. public function getApiV3SearchSearchAsyncWithHttpInfo($search, string $contentType = self::contentTypes['getApiV3SearchSearch'][0])
  695. {
  696. $returnType = '\WikipaliApi\Model\GetApiV3SearchSearch200Response';
  697. $request = $this->getApiV3SearchSearchRequest($search, $contentType);
  698. return $this->client
  699. ->sendAsync($request, $this->createHttpClientOption())
  700. ->then(
  701. function ($response) use ($returnType) {
  702. if ($returnType === '\SplFileObject') {
  703. $content = $response->getBody(); //stream goes to serializer
  704. } else {
  705. $content = (string) $response->getBody();
  706. if ($returnType !== 'string') {
  707. $content = json_decode($content);
  708. }
  709. }
  710. return [
  711. ObjectSerializer::deserialize($content, $returnType, []),
  712. $response->getStatusCode(),
  713. $response->getHeaders()
  714. ];
  715. },
  716. function ($exception) {
  717. $response = $exception->getResponse();
  718. $statusCode = $response->getStatusCode();
  719. throw new ApiException(
  720. sprintf(
  721. '[%d] Error connecting to the API (%s)',
  722. $statusCode,
  723. $exception->getRequest()->getUri()
  724. ),
  725. $statusCode,
  726. $response->getHeaders(),
  727. (string) $response->getBody()
  728. );
  729. }
  730. );
  731. }
  732. /**
  733. * Create request for operation 'getApiV3SearchSearch'
  734. *
  735. * @param string $search OpenSearch 文档 id (required)
  736. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3SearchSearch'] to see the possible values for this operation
  737. *
  738. * @throws \InvalidArgumentException
  739. * @return \GuzzleHttp\Psr7\Request
  740. */
  741. public function getApiV3SearchSearchRequest($search, string $contentType = self::contentTypes['getApiV3SearchSearch'][0])
  742. {
  743. // verify the required parameter 'search' is set
  744. if ($search === null || (is_array($search) && count($search) === 0)) {
  745. throw new \InvalidArgumentException(
  746. 'Missing the required parameter $search when calling getApiV3SearchSearch'
  747. );
  748. }
  749. $resourcePath = '/v3/search/{search}';
  750. $formParams = [];
  751. $queryParams = [];
  752. $headerParams = [];
  753. $httpBody = '';
  754. $multipart = false;
  755. // path params
  756. if ($search !== null) {
  757. $resourcePath = str_replace(
  758. '{search}',
  759. ObjectSerializer::toPathValue($search),
  760. $resourcePath
  761. );
  762. }
  763. $headers = $this->headerSelector->selectHeaders(
  764. ['application/json', 'application/problem+json', ],
  765. $contentType,
  766. $multipart
  767. );
  768. // for model (json/xml)
  769. if (count($formParams) > 0) {
  770. if ($multipart) {
  771. $multipartContents = [];
  772. foreach ($formParams as $formParamName => $formParamValue) {
  773. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  774. foreach ($formParamValueItems as $formParamValueItem) {
  775. $multipartContents[] = [
  776. 'name' => $formParamName,
  777. 'contents' => $formParamValueItem
  778. ];
  779. }
  780. }
  781. // for HTTP post (form)
  782. $httpBody = new MultipartStream($multipartContents);
  783. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  784. # if Content-Type contains "application/json", json_encode the form parameters
  785. try {
  786. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  787. } catch (\JsonException $e) {
  788. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  789. }
  790. } else {
  791. // for HTTP post (form)
  792. $httpBody = ObjectSerializer::buildQuery($formParams);
  793. }
  794. }
  795. $defaultHeaders = [];
  796. if ($this->config->getUserAgent()) {
  797. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  798. }
  799. $headers = array_merge(
  800. $defaultHeaders,
  801. $headerParams,
  802. $headers
  803. );
  804. $operationHost = $this->config->getHost();
  805. $query = ObjectSerializer::buildQuery($queryParams);
  806. return new Request(
  807. 'GET',
  808. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  809. $headers,
  810. $httpBody
  811. );
  812. }
  813. /**
  814. * Operation postApiV3Search
  815. *
  816. * 全文检索(POST)
  817. *
  818. * @param \WikipaliApi\Model\PostApiV3SearchRequest $post_api_v3_search_request post_api_v3_search_request (required)
  819. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['postApiV3Search'] to see the possible values for this operation
  820. *
  821. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  822. * @throws \InvalidArgumentException
  823. * @return \WikipaliApi\Model\GetApiV3Search200Response|\WikipaliApi\Model\ProblemDetails
  824. */
  825. public function postApiV3Search($post_api_v3_search_request, string $contentType = self::contentTypes['postApiV3Search'][0])
  826. {
  827. list($response) = $this->postApiV3SearchWithHttpInfo($post_api_v3_search_request, $contentType);
  828. return $response;
  829. }
  830. /**
  831. * Operation postApiV3SearchWithHttpInfo
  832. *
  833. * 全文检索(POST)
  834. *
  835. * @param \WikipaliApi\Model\PostApiV3SearchRequest $post_api_v3_search_request (required)
  836. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['postApiV3Search'] to see the possible values for this operation
  837. *
  838. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  839. * @throws \InvalidArgumentException
  840. * @return array of \WikipaliApi\Model\GetApiV3Search200Response|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  841. */
  842. public function postApiV3SearchWithHttpInfo($post_api_v3_search_request, string $contentType = self::contentTypes['postApiV3Search'][0])
  843. {
  844. $request = $this->postApiV3SearchRequest($post_api_v3_search_request, $contentType);
  845. try {
  846. $options = $this->createHttpClientOption();
  847. try {
  848. $response = $this->client->send($request, $options);
  849. } catch (RequestException $e) {
  850. throw new ApiException(
  851. "[{$e->getCode()}] {$e->getMessage()}",
  852. (int) $e->getCode(),
  853. $e->getResponse() ? $e->getResponse()->getHeaders() : null,
  854. $e->getResponse() ? (string) $e->getResponse()->getBody() : null
  855. );
  856. } catch (ConnectException $e) {
  857. throw new ApiException(
  858. "[{$e->getCode()}] {$e->getMessage()}",
  859. (int) $e->getCode(),
  860. null,
  861. null
  862. );
  863. }
  864. $statusCode = $response->getStatusCode();
  865. switch($statusCode) {
  866. case 200:
  867. return $this->handleResponseWithDataType(
  868. '\WikipaliApi\Model\GetApiV3Search200Response',
  869. $request,
  870. $response,
  871. );
  872. case 422:
  873. return $this->handleResponseWithDataType(
  874. '\WikipaliApi\Model\ProblemDetails',
  875. $request,
  876. $response,
  877. );
  878. }
  879. if ($statusCode < 200 || $statusCode > 299) {
  880. throw new ApiException(
  881. sprintf(
  882. '[%d] Error connecting to the API (%s)',
  883. $statusCode,
  884. (string) $request->getUri()
  885. ),
  886. $statusCode,
  887. $response->getHeaders(),
  888. (string) $response->getBody()
  889. );
  890. }
  891. return $this->handleResponseWithDataType(
  892. '\WikipaliApi\Model\GetApiV3Search200Response',
  893. $request,
  894. $response,
  895. );
  896. } catch (ApiException $e) {
  897. switch ($e->getCode()) {
  898. case 200:
  899. $data = ObjectSerializer::deserialize(
  900. $e->getResponseBody(),
  901. '\WikipaliApi\Model\GetApiV3Search200Response',
  902. $e->getResponseHeaders()
  903. );
  904. $e->setResponseObject($data);
  905. throw $e;
  906. case 422:
  907. $data = ObjectSerializer::deserialize(
  908. $e->getResponseBody(),
  909. '\WikipaliApi\Model\ProblemDetails',
  910. $e->getResponseHeaders()
  911. );
  912. $e->setResponseObject($data);
  913. throw $e;
  914. }
  915. throw $e;
  916. }
  917. }
  918. /**
  919. * Operation postApiV3SearchAsync
  920. *
  921. * 全文检索(POST)
  922. *
  923. * @param \WikipaliApi\Model\PostApiV3SearchRequest $post_api_v3_search_request (required)
  924. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['postApiV3Search'] to see the possible values for this operation
  925. *
  926. * @throws \InvalidArgumentException
  927. * @return \GuzzleHttp\Promise\PromiseInterface
  928. */
  929. public function postApiV3SearchAsync($post_api_v3_search_request, string $contentType = self::contentTypes['postApiV3Search'][0])
  930. {
  931. return $this->postApiV3SearchAsyncWithHttpInfo($post_api_v3_search_request, $contentType)
  932. ->then(
  933. function ($response) {
  934. return $response[0];
  935. }
  936. );
  937. }
  938. /**
  939. * Operation postApiV3SearchAsyncWithHttpInfo
  940. *
  941. * 全文检索(POST)
  942. *
  943. * @param \WikipaliApi\Model\PostApiV3SearchRequest $post_api_v3_search_request (required)
  944. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['postApiV3Search'] to see the possible values for this operation
  945. *
  946. * @throws \InvalidArgumentException
  947. * @return \GuzzleHttp\Promise\PromiseInterface
  948. */
  949. public function postApiV3SearchAsyncWithHttpInfo($post_api_v3_search_request, string $contentType = self::contentTypes['postApiV3Search'][0])
  950. {
  951. $returnType = '\WikipaliApi\Model\GetApiV3Search200Response';
  952. $request = $this->postApiV3SearchRequest($post_api_v3_search_request, $contentType);
  953. return $this->client
  954. ->sendAsync($request, $this->createHttpClientOption())
  955. ->then(
  956. function ($response) use ($returnType) {
  957. if ($returnType === '\SplFileObject') {
  958. $content = $response->getBody(); //stream goes to serializer
  959. } else {
  960. $content = (string) $response->getBody();
  961. if ($returnType !== 'string') {
  962. $content = json_decode($content);
  963. }
  964. }
  965. return [
  966. ObjectSerializer::deserialize($content, $returnType, []),
  967. $response->getStatusCode(),
  968. $response->getHeaders()
  969. ];
  970. },
  971. function ($exception) {
  972. $response = $exception->getResponse();
  973. $statusCode = $response->getStatusCode();
  974. throw new ApiException(
  975. sprintf(
  976. '[%d] Error connecting to the API (%s)',
  977. $statusCode,
  978. $exception->getRequest()->getUri()
  979. ),
  980. $statusCode,
  981. $response->getHeaders(),
  982. (string) $response->getBody()
  983. );
  984. }
  985. );
  986. }
  987. /**
  988. * Create request for operation 'postApiV3Search'
  989. *
  990. * @param \WikipaliApi\Model\PostApiV3SearchRequest $post_api_v3_search_request (required)
  991. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['postApiV3Search'] to see the possible values for this operation
  992. *
  993. * @throws \InvalidArgumentException
  994. * @return \GuzzleHttp\Psr7\Request
  995. */
  996. public function postApiV3SearchRequest($post_api_v3_search_request, string $contentType = self::contentTypes['postApiV3Search'][0])
  997. {
  998. // verify the required parameter 'post_api_v3_search_request' is set
  999. if ($post_api_v3_search_request === null || (is_array($post_api_v3_search_request) && count($post_api_v3_search_request) === 0)) {
  1000. throw new \InvalidArgumentException(
  1001. 'Missing the required parameter $post_api_v3_search_request when calling postApiV3Search'
  1002. );
  1003. }
  1004. $resourcePath = '/v3/search';
  1005. $formParams = [];
  1006. $queryParams = [];
  1007. $headerParams = [];
  1008. $httpBody = '';
  1009. $multipart = false;
  1010. $headers = $this->headerSelector->selectHeaders(
  1011. ['application/json', 'application/problem+json', ],
  1012. $contentType,
  1013. $multipart
  1014. );
  1015. // for model (json/xml)
  1016. if (isset($post_api_v3_search_request)) {
  1017. if (stripos($headers['Content-Type'], 'application/json') !== false) {
  1018. # if Content-Type contains "application/json", json_encode the body
  1019. try {
  1020. $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($post_api_v3_search_request), JSON_THROW_ON_ERROR);
  1021. } catch (\JsonException $e) {
  1022. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  1023. }
  1024. } else {
  1025. $httpBody = $post_api_v3_search_request;
  1026. }
  1027. } elseif (count($formParams) > 0) {
  1028. if ($multipart) {
  1029. $multipartContents = [];
  1030. foreach ($formParams as $formParamName => $formParamValue) {
  1031. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  1032. foreach ($formParamValueItems as $formParamValueItem) {
  1033. $multipartContents[] = [
  1034. 'name' => $formParamName,
  1035. 'contents' => $formParamValueItem
  1036. ];
  1037. }
  1038. }
  1039. // for HTTP post (form)
  1040. $httpBody = new MultipartStream($multipartContents);
  1041. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  1042. # if Content-Type contains "application/json", json_encode the form parameters
  1043. try {
  1044. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  1045. } catch (\JsonException $e) {
  1046. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  1047. }
  1048. } else {
  1049. // for HTTP post (form)
  1050. $httpBody = ObjectSerializer::buildQuery($formParams);
  1051. }
  1052. }
  1053. $defaultHeaders = [];
  1054. if ($this->config->getUserAgent()) {
  1055. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  1056. }
  1057. $headers = array_merge(
  1058. $defaultHeaders,
  1059. $headerParams,
  1060. $headers
  1061. );
  1062. $operationHost = $this->config->getHost();
  1063. $query = ObjectSerializer::buildQuery($queryParams);
  1064. return new Request(
  1065. 'POST',
  1066. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  1067. $headers,
  1068. $httpBody
  1069. );
  1070. }
  1071. /**
  1072. * Create http client option
  1073. *
  1074. * @throws \RuntimeException on file opening failure
  1075. * @return array of http client options
  1076. */
  1077. protected function createHttpClientOption()
  1078. {
  1079. $options = [];
  1080. if ($this->config->getDebug()) {
  1081. $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
  1082. if (!$options[RequestOptions::DEBUG]) {
  1083. throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
  1084. }
  1085. }
  1086. if ($this->config->getCertFile()) {
  1087. $options[RequestOptions::CERT] = $this->config->getCertFile();
  1088. }
  1089. if ($this->config->getKeyFile()) {
  1090. $options[RequestOptions::SSL_KEY] = $this->config->getKeyFile();
  1091. }
  1092. return $options;
  1093. }
  1094. private function handleResponseWithDataType(
  1095. string $dataType,
  1096. RequestInterface $request,
  1097. ResponseInterface $response
  1098. ): array {
  1099. if ($dataType === '\SplFileObject') {
  1100. $content = $response->getBody(); //stream goes to serializer
  1101. } else {
  1102. $content = (string) $response->getBody();
  1103. if ($dataType !== 'string') {
  1104. try {
  1105. $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
  1106. } catch (\JsonException $exception) {
  1107. throw new ApiException(
  1108. sprintf(
  1109. 'Error JSON decoding server response (%s)',
  1110. $request->getUri()
  1111. ),
  1112. $response->getStatusCode(),
  1113. $response->getHeaders(),
  1114. $content
  1115. );
  1116. }
  1117. }
  1118. }
  1119. return [
  1120. ObjectSerializer::deserialize($content, $dataType, []),
  1121. $response->getStatusCode(),
  1122. $response->getHeaders()
  1123. ];
  1124. }
  1125. private function responseWithinRangeCode(
  1126. string $rangeCode,
  1127. int $statusCode
  1128. ): bool {
  1129. $left = (int) ($rangeCode[0].'00');
  1130. $right = (int) ($rangeCode[0].'99');
  1131. return $statusCode >= $left && $statusCode <= $right;
  1132. }
  1133. }