2
0

AgentChargesApi.php 20 KB

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