MeApi.php 15 KB

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