UsersApi.php 18 KB

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