AgentQuotaApi.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * AgentQuotaApi
  4. * PHP version 8.1
  5. *
  6. * @category Class
  7. * @package WikipaliApi
  8. * @author OpenAPI Generator team
  9. * @link https://openapi-generator.tech
  10. */
  11. /**
  12. * MINT API
  13. *
  14. * International Academy Of Pali Tipitaka(国际巴利三藏学院) 由 `php openapi/scripts/generate.php` 依据 api-v13 的路由与控制器源码生成。 响应统一封装为 `{ok, message, data}`;`ok=false` 时 `message` 为错误信息。
  15. *
  16. * The version of the OpenAPI document: 1.0.0
  17. * Generated by: https://openapi-generator.tech
  18. * Generator version: 7.25.0
  19. */
  20. /**
  21. * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
  22. * https://openapi-generator.tech
  23. * Do not edit the class manually.
  24. */
  25. namespace WikipaliApi\Api;
  26. use GuzzleHttp\Client;
  27. use GuzzleHttp\ClientInterface;
  28. use GuzzleHttp\Exception\ConnectException;
  29. use GuzzleHttp\Exception\RequestException;
  30. use GuzzleHttp\Psr7\MultipartStream;
  31. use GuzzleHttp\Psr7\Request;
  32. use GuzzleHttp\RequestOptions;
  33. use Psr\Http\Message\RequestInterface;
  34. use Psr\Http\Message\ResponseInterface;
  35. use WikipaliApi\ApiException;
  36. use WikipaliApi\Configuration;
  37. use WikipaliApi\FormDataProcessor;
  38. use WikipaliApi\HeaderSelector;
  39. use WikipaliApi\ObjectSerializer;
  40. /**
  41. * AgentQuotaApi Class Doc Comment
  42. *
  43. * @category Class
  44. * @package WikipaliApi
  45. * @author OpenAPI Generator team
  46. * @link https://openapi-generator.tech
  47. */
  48. class AgentQuotaApi
  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. 'getApiV3AgentQuota' => [
  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 getApiV3AgentQuota
  116. *
  117. * 查看我的 AI 对话额度
  118. *
  119. * @param string|null $device_id 设备匿名 id(uuid)。未登录时必填 (optional)
  120. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3AgentQuota'] 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\GetApiV3AgentQuota200Response|\WikipaliApi\Model\ProblemDetails
  125. */
  126. public function getApiV3AgentQuota($device_id = null, string $contentType = self::contentTypes['getApiV3AgentQuota'][0])
  127. {
  128. list($response) = $this->getApiV3AgentQuotaWithHttpInfo($device_id, $contentType);
  129. return $response;
  130. }
  131. /**
  132. * Operation getApiV3AgentQuotaWithHttpInfo
  133. *
  134. * 查看我的 AI 对话额度
  135. *
  136. * @param string|null $device_id 设备匿名 id(uuid)。未登录时必填 (optional)
  137. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3AgentQuota'] 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\GetApiV3AgentQuota200Response|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  142. */
  143. public function getApiV3AgentQuotaWithHttpInfo($device_id = null, string $contentType = self::contentTypes['getApiV3AgentQuota'][0])
  144. {
  145. $request = $this->getApiV3AgentQuotaRequest($device_id, $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\GetApiV3AgentQuota200Response',
  170. $request,
  171. $response,
  172. );
  173. case 422:
  174. return $this->handleResponseWithDataType(
  175. '\WikipaliApi\Model\ProblemDetails',
  176. $request,
  177. $response,
  178. );
  179. }
  180. if ($statusCode < 200 || $statusCode > 299) {
  181. throw new ApiException(
  182. sprintf(
  183. '[%d] Error connecting to the API (%s)',
  184. $statusCode,
  185. (string) $request->getUri()
  186. ),
  187. $statusCode,
  188. $response->getHeaders(),
  189. (string) $response->getBody()
  190. );
  191. }
  192. return $this->handleResponseWithDataType(
  193. '\WikipaliApi\Model\GetApiV3AgentQuota200Response',
  194. $request,
  195. $response,
  196. );
  197. } catch (ApiException $e) {
  198. switch ($e->getCode()) {
  199. case 200:
  200. $data = ObjectSerializer::deserialize(
  201. $e->getResponseBody(),
  202. '\WikipaliApi\Model\GetApiV3AgentQuota200Response',
  203. $e->getResponseHeaders()
  204. );
  205. $e->setResponseObject($data);
  206. throw $e;
  207. case 422:
  208. $data = ObjectSerializer::deserialize(
  209. $e->getResponseBody(),
  210. '\WikipaliApi\Model\ProblemDetails',
  211. $e->getResponseHeaders()
  212. );
  213. $e->setResponseObject($data);
  214. throw $e;
  215. }
  216. throw $e;
  217. }
  218. }
  219. /**
  220. * Operation getApiV3AgentQuotaAsync
  221. *
  222. * 查看我的 AI 对话额度
  223. *
  224. * @param string|null $device_id 设备匿名 id(uuid)。未登录时必填 (optional)
  225. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3AgentQuota'] to see the possible values for this operation
  226. *
  227. * @throws \InvalidArgumentException
  228. * @return \GuzzleHttp\Promise\PromiseInterface
  229. */
  230. public function getApiV3AgentQuotaAsync($device_id = null, string $contentType = self::contentTypes['getApiV3AgentQuota'][0])
  231. {
  232. return $this->getApiV3AgentQuotaAsyncWithHttpInfo($device_id, $contentType)
  233. ->then(
  234. function ($response) {
  235. return $response[0];
  236. }
  237. );
  238. }
  239. /**
  240. * Operation getApiV3AgentQuotaAsyncWithHttpInfo
  241. *
  242. * 查看我的 AI 对话额度
  243. *
  244. * @param string|null $device_id 设备匿名 id(uuid)。未登录时必填 (optional)
  245. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3AgentQuota'] to see the possible values for this operation
  246. *
  247. * @throws \InvalidArgumentException
  248. * @return \GuzzleHttp\Promise\PromiseInterface
  249. */
  250. public function getApiV3AgentQuotaAsyncWithHttpInfo($device_id = null, string $contentType = self::contentTypes['getApiV3AgentQuota'][0])
  251. {
  252. $returnType = '\WikipaliApi\Model\GetApiV3AgentQuota200Response';
  253. $request = $this->getApiV3AgentQuotaRequest($device_id, $contentType);
  254. return $this->client
  255. ->sendAsync($request, $this->createHttpClientOption())
  256. ->then(
  257. function ($response) use ($returnType) {
  258. if ($returnType === '\SplFileObject') {
  259. $content = $response->getBody(); //stream goes to serializer
  260. } else {
  261. $content = (string) $response->getBody();
  262. if ($returnType !== 'string') {
  263. $content = json_decode($content);
  264. }
  265. }
  266. return [
  267. ObjectSerializer::deserialize($content, $returnType, []),
  268. $response->getStatusCode(),
  269. $response->getHeaders()
  270. ];
  271. },
  272. function ($exception) {
  273. $response = $exception->getResponse();
  274. $statusCode = $response->getStatusCode();
  275. throw new ApiException(
  276. sprintf(
  277. '[%d] Error connecting to the API (%s)',
  278. $statusCode,
  279. $exception->getRequest()->getUri()
  280. ),
  281. $statusCode,
  282. $response->getHeaders(),
  283. (string) $response->getBody()
  284. );
  285. }
  286. );
  287. }
  288. /**
  289. * Create request for operation 'getApiV3AgentQuota'
  290. *
  291. * @param string|null $device_id 设备匿名 id(uuid)。未登录时必填 (optional)
  292. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3AgentQuota'] to see the possible values for this operation
  293. *
  294. * @throws \InvalidArgumentException
  295. * @return \GuzzleHttp\Psr7\Request
  296. */
  297. public function getApiV3AgentQuotaRequest($device_id = null, string $contentType = self::contentTypes['getApiV3AgentQuota'][0])
  298. {
  299. $resourcePath = '/v3/agent-quota';
  300. $formParams = [];
  301. $queryParams = [];
  302. $headerParams = [];
  303. $httpBody = '';
  304. $multipart = false;
  305. // query params
  306. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  307. $device_id,
  308. 'device_id', // param base name
  309. 'string', // openApiType
  310. 'form', // style
  311. true, // explode
  312. false // required
  313. ) ?? []);
  314. $headers = $this->headerSelector->selectHeaders(
  315. ['application/json', 'application/problem+json', ],
  316. $contentType,
  317. $multipart
  318. );
  319. // for model (json/xml)
  320. if (count($formParams) > 0) {
  321. if ($multipart) {
  322. $multipartContents = [];
  323. foreach ($formParams as $formParamName => $formParamValue) {
  324. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  325. foreach ($formParamValueItems as $formParamValueItem) {
  326. $multipartContents[] = [
  327. 'name' => $formParamName,
  328. 'contents' => $formParamValueItem
  329. ];
  330. }
  331. }
  332. // for HTTP post (form)
  333. $httpBody = new MultipartStream($multipartContents);
  334. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  335. # if Content-Type contains "application/json", json_encode the form parameters
  336. try {
  337. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  338. } catch (\JsonException $e) {
  339. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  340. }
  341. } else {
  342. // for HTTP post (form)
  343. $httpBody = ObjectSerializer::buildQuery($formParams);
  344. }
  345. }
  346. // this endpoint requires Bearer authentication (access token)
  347. if (!empty($this->config->getAccessToken())) {
  348. $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
  349. }
  350. $defaultHeaders = [];
  351. if ($this->config->getUserAgent()) {
  352. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  353. }
  354. $headers = array_merge(
  355. $defaultHeaders,
  356. $headerParams,
  357. $headers
  358. );
  359. $operationHost = $this->config->getHost();
  360. $query = ObjectSerializer::buildQuery($queryParams);
  361. return new Request(
  362. 'GET',
  363. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  364. $headers,
  365. $httpBody
  366. );
  367. }
  368. /**
  369. * Create http client option
  370. *
  371. * @throws \RuntimeException on file opening failure
  372. * @return array of http client options
  373. */
  374. protected function createHttpClientOption()
  375. {
  376. $options = [];
  377. if ($this->config->getDebug()) {
  378. $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
  379. if (!$options[RequestOptions::DEBUG]) {
  380. throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
  381. }
  382. }
  383. if ($this->config->getCertFile()) {
  384. $options[RequestOptions::CERT] = $this->config->getCertFile();
  385. }
  386. if ($this->config->getKeyFile()) {
  387. $options[RequestOptions::SSL_KEY] = $this->config->getKeyFile();
  388. }
  389. return $options;
  390. }
  391. private function handleResponseWithDataType(
  392. string $dataType,
  393. RequestInterface $request,
  394. ResponseInterface $response
  395. ): array {
  396. if ($dataType === '\SplFileObject') {
  397. $content = $response->getBody(); //stream goes to serializer
  398. } else {
  399. $content = (string) $response->getBody();
  400. if ($dataType !== 'string') {
  401. try {
  402. $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
  403. } catch (\JsonException $exception) {
  404. throw new ApiException(
  405. sprintf(
  406. 'Error JSON decoding server response (%s)',
  407. $request->getUri()
  408. ),
  409. $response->getStatusCode(),
  410. $response->getHeaders(),
  411. $content
  412. );
  413. }
  414. }
  415. }
  416. return [
  417. ObjectSerializer::deserialize($content, $dataType, []),
  418. $response->getStatusCode(),
  419. $response->getHeaders()
  420. ];
  421. }
  422. private function responseWithinRangeCode(
  423. string $rangeCode,
  424. int $statusCode
  425. ): bool {
  426. $left = (int) ($rangeCode[0].'00');
  427. $right = (int) ($rangeCode[0].'99');
  428. return $statusCode >= $left && $statusCode <= $right;
  429. }
  430. }