ReadingTimeApi.php 18 KB

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