TipitakaReadingApi.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. /**
  3. * TipitakaReadingApi
  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. * TipitakaReadingApi Class Doc Comment
  42. *
  43. * @category Class
  44. * @package WikipaliApi
  45. * @author OpenAPI Generator team
  46. * @link https://openapi-generator.tech
  47. */
  48. class TipitakaReadingApi
  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. 'getApiV3TipitakaReadingChannel' => [
  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 getApiV3TipitakaReadingChannel
  116. *
  117. * 读取译文
  118. *
  119. * @param string $channel 译文 channel 的 uuid (required)
  120. * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional)
  121. * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional)
  122. * @param int|null $para 起始段落号 (optional)
  123. * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional)
  124. * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional)
  125. * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit&#x3D;para 时 200、 unit&#x3D;byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10)
  126. * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para')
  127. * @param string|null $format 内容格式 (optional, default to 'html')
  128. * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 &#x60;view&#x3D;&#x60; 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display')
  129. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation
  130. *
  131. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  132. * @throws \InvalidArgumentException
  133. * @return \WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response|\WikipaliApi\Model\ProblemDetails|\WikipaliApi\Model\ProblemDetails
  134. */
  135. public function getApiV3TipitakaReadingChannel($channel, $book = null, $chapter = null, $para = null, $to = null, $after = null, $page_size = 10, $unit = 'para', $format = 'html', $include = 'display', string $contentType = self::contentTypes['getApiV3TipitakaReadingChannel'][0])
  136. {
  137. list($response) = $this->getApiV3TipitakaReadingChannelWithHttpInfo($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType);
  138. return $response;
  139. }
  140. /**
  141. * Operation getApiV3TipitakaReadingChannelWithHttpInfo
  142. *
  143. * 读取译文
  144. *
  145. * @param string $channel 译文 channel 的 uuid (required)
  146. * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional)
  147. * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional)
  148. * @param int|null $para 起始段落号 (optional)
  149. * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional)
  150. * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional)
  151. * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit&#x3D;para 时 200、 unit&#x3D;byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10)
  152. * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para')
  153. * @param string|null $format 内容格式 (optional, default to 'html')
  154. * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 &#x60;view&#x3D;&#x60; 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display')
  155. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation
  156. *
  157. * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format
  158. * @throws \InvalidArgumentException
  159. * @return array of \WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response|\WikipaliApi\Model\ProblemDetails|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings)
  160. */
  161. public function getApiV3TipitakaReadingChannelWithHttpInfo($channel, $book = null, $chapter = null, $para = null, $to = null, $after = null, $page_size = 10, $unit = 'para', $format = 'html', $include = 'display', string $contentType = self::contentTypes['getApiV3TipitakaReadingChannel'][0])
  162. {
  163. $request = $this->getApiV3TipitakaReadingChannelRequest($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType);
  164. try {
  165. $options = $this->createHttpClientOption();
  166. try {
  167. $response = $this->client->send($request, $options);
  168. } catch (RequestException $e) {
  169. throw new ApiException(
  170. "[{$e->getCode()}] {$e->getMessage()}",
  171. (int) $e->getCode(),
  172. $e->getResponse() ? $e->getResponse()->getHeaders() : null,
  173. $e->getResponse() ? (string) $e->getResponse()->getBody() : null
  174. );
  175. } catch (ConnectException $e) {
  176. throw new ApiException(
  177. "[{$e->getCode()}] {$e->getMessage()}",
  178. (int) $e->getCode(),
  179. null,
  180. null
  181. );
  182. }
  183. $statusCode = $response->getStatusCode();
  184. switch($statusCode) {
  185. case 200:
  186. return $this->handleResponseWithDataType(
  187. '\WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response',
  188. $request,
  189. $response,
  190. );
  191. case 404:
  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\GetApiV3TipitakaReadingChannel200Response',
  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\GetApiV3TipitakaReadingChannel200Response',
  227. $e->getResponseHeaders()
  228. );
  229. $e->setResponseObject($data);
  230. throw $e;
  231. case 404:
  232. $data = ObjectSerializer::deserialize(
  233. $e->getResponseBody(),
  234. '\WikipaliApi\Model\ProblemDetails',
  235. $e->getResponseHeaders()
  236. );
  237. $e->setResponseObject($data);
  238. throw $e;
  239. case 422:
  240. $data = ObjectSerializer::deserialize(
  241. $e->getResponseBody(),
  242. '\WikipaliApi\Model\ProblemDetails',
  243. $e->getResponseHeaders()
  244. );
  245. $e->setResponseObject($data);
  246. throw $e;
  247. }
  248. throw $e;
  249. }
  250. }
  251. /**
  252. * Operation getApiV3TipitakaReadingChannelAsync
  253. *
  254. * 读取译文
  255. *
  256. * @param string $channel 译文 channel 的 uuid (required)
  257. * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional)
  258. * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional)
  259. * @param int|null $para 起始段落号 (optional)
  260. * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional)
  261. * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional)
  262. * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit&#x3D;para 时 200、 unit&#x3D;byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10)
  263. * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para')
  264. * @param string|null $format 内容格式 (optional, default to 'html')
  265. * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 &#x60;view&#x3D;&#x60; 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display')
  266. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation
  267. *
  268. * @throws \InvalidArgumentException
  269. * @return \GuzzleHttp\Promise\PromiseInterface
  270. */
  271. public function getApiV3TipitakaReadingChannelAsync($channel, $book = null, $chapter = null, $para = null, $to = null, $after = null, $page_size = 10, $unit = 'para', $format = 'html', $include = 'display', string $contentType = self::contentTypes['getApiV3TipitakaReadingChannel'][0])
  272. {
  273. return $this->getApiV3TipitakaReadingChannelAsyncWithHttpInfo($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType)
  274. ->then(
  275. function ($response) {
  276. return $response[0];
  277. }
  278. );
  279. }
  280. /**
  281. * Operation getApiV3TipitakaReadingChannelAsyncWithHttpInfo
  282. *
  283. * 读取译文
  284. *
  285. * @param string $channel 译文 channel 的 uuid (required)
  286. * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional)
  287. * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional)
  288. * @param int|null $para 起始段落号 (optional)
  289. * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional)
  290. * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional)
  291. * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit&#x3D;para 时 200、 unit&#x3D;byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10)
  292. * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para')
  293. * @param string|null $format 内容格式 (optional, default to 'html')
  294. * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 &#x60;view&#x3D;&#x60; 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display')
  295. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation
  296. *
  297. * @throws \InvalidArgumentException
  298. * @return \GuzzleHttp\Promise\PromiseInterface
  299. */
  300. public function getApiV3TipitakaReadingChannelAsyncWithHttpInfo($channel, $book = null, $chapter = null, $para = null, $to = null, $after = null, $page_size = 10, $unit = 'para', $format = 'html', $include = 'display', string $contentType = self::contentTypes['getApiV3TipitakaReadingChannel'][0])
  301. {
  302. $returnType = '\WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response';
  303. $request = $this->getApiV3TipitakaReadingChannelRequest($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType);
  304. return $this->client
  305. ->sendAsync($request, $this->createHttpClientOption())
  306. ->then(
  307. function ($response) use ($returnType) {
  308. if ($returnType === '\SplFileObject') {
  309. $content = $response->getBody(); //stream goes to serializer
  310. } else {
  311. $content = (string) $response->getBody();
  312. if ($returnType !== 'string') {
  313. $content = json_decode($content);
  314. }
  315. }
  316. return [
  317. ObjectSerializer::deserialize($content, $returnType, []),
  318. $response->getStatusCode(),
  319. $response->getHeaders()
  320. ];
  321. },
  322. function ($exception) {
  323. $response = $exception->getResponse();
  324. $statusCode = $response->getStatusCode();
  325. throw new ApiException(
  326. sprintf(
  327. '[%d] Error connecting to the API (%s)',
  328. $statusCode,
  329. $exception->getRequest()->getUri()
  330. ),
  331. $statusCode,
  332. $response->getHeaders(),
  333. (string) $response->getBody()
  334. );
  335. }
  336. );
  337. }
  338. /**
  339. * Create request for operation 'getApiV3TipitakaReadingChannel'
  340. *
  341. * @param string $channel 译文 channel 的 uuid (required)
  342. * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional)
  343. * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional)
  344. * @param int|null $para 起始段落号 (optional)
  345. * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional)
  346. * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional)
  347. * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit&#x3D;para 时 200、 unit&#x3D;byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10)
  348. * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para')
  349. * @param string|null $format 内容格式 (optional, default to 'html')
  350. * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 &#x60;view&#x3D;&#x60; 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display')
  351. * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation
  352. *
  353. * @throws \InvalidArgumentException
  354. * @return \GuzzleHttp\Psr7\Request
  355. */
  356. public function getApiV3TipitakaReadingChannelRequest($channel, $book = null, $chapter = null, $para = null, $to = null, $after = null, $page_size = 10, $unit = 'para', $format = 'html', $include = 'display', string $contentType = self::contentTypes['getApiV3TipitakaReadingChannel'][0])
  357. {
  358. // verify the required parameter 'channel' is set
  359. if ($channel === null || (is_array($channel) && count($channel) === 0)) {
  360. throw new \InvalidArgumentException(
  361. 'Missing the required parameter $channel when calling getApiV3TipitakaReadingChannel'
  362. );
  363. }
  364. $resourcePath = '/v3/tipitaka-reading/{channel}';
  365. $formParams = [];
  366. $queryParams = [];
  367. $headerParams = [];
  368. $httpBody = '';
  369. $multipart = false;
  370. // query params
  371. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  372. $book,
  373. 'book', // param base name
  374. 'integer', // openApiType
  375. 'form', // style
  376. true, // explode
  377. false // required
  378. ) ?? []);
  379. // query params
  380. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  381. $chapter,
  382. 'chapter', // param base name
  383. 'integer', // openApiType
  384. 'form', // style
  385. true, // explode
  386. false // required
  387. ) ?? []);
  388. // query params
  389. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  390. $para,
  391. 'para', // param base name
  392. 'integer', // openApiType
  393. 'form', // style
  394. true, // explode
  395. false // required
  396. ) ?? []);
  397. // query params
  398. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  399. $to,
  400. 'to', // param base name
  401. 'integer', // openApiType
  402. 'form', // style
  403. true, // explode
  404. false // required
  405. ) ?? []);
  406. // query params
  407. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  408. $after,
  409. 'after', // param base name
  410. 'string', // openApiType
  411. 'form', // style
  412. true, // explode
  413. false // required
  414. ) ?? []);
  415. // query params
  416. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  417. $page_size,
  418. 'page_size', // param base name
  419. 'integer', // openApiType
  420. 'form', // style
  421. true, // explode
  422. false // required
  423. ) ?? []);
  424. // query params
  425. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  426. $unit,
  427. 'unit', // param base name
  428. 'string', // openApiType
  429. 'form', // style
  430. true, // explode
  431. false // required
  432. ) ?? []);
  433. // query params
  434. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  435. $format,
  436. 'format', // param base name
  437. 'string', // openApiType
  438. 'form', // style
  439. true, // explode
  440. false // required
  441. ) ?? []);
  442. // query params
  443. $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
  444. $include,
  445. 'include', // param base name
  446. 'string', // openApiType
  447. 'form', // style
  448. true, // explode
  449. false // required
  450. ) ?? []);
  451. // path params
  452. if ($channel !== null) {
  453. $resourcePath = str_replace(
  454. '{channel}',
  455. ObjectSerializer::toPathValue($channel),
  456. $resourcePath
  457. );
  458. }
  459. $headers = $this->headerSelector->selectHeaders(
  460. ['application/json', 'application/problem+json', ],
  461. $contentType,
  462. $multipart
  463. );
  464. // for model (json/xml)
  465. if (count($formParams) > 0) {
  466. if ($multipart) {
  467. $multipartContents = [];
  468. foreach ($formParams as $formParamName => $formParamValue) {
  469. $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
  470. foreach ($formParamValueItems as $formParamValueItem) {
  471. $multipartContents[] = [
  472. 'name' => $formParamName,
  473. 'contents' => $formParamValueItem
  474. ];
  475. }
  476. }
  477. // for HTTP post (form)
  478. $httpBody = new MultipartStream($multipartContents);
  479. } elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
  480. # if Content-Type contains "application/json", json_encode the form parameters
  481. try {
  482. $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
  483. } catch (\JsonException $e) {
  484. throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e);
  485. }
  486. } else {
  487. // for HTTP post (form)
  488. $httpBody = ObjectSerializer::buildQuery($formParams);
  489. }
  490. }
  491. // this endpoint requires Bearer authentication (access token)
  492. if (!empty($this->config->getAccessToken())) {
  493. $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
  494. }
  495. $defaultHeaders = [];
  496. if ($this->config->getUserAgent()) {
  497. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  498. }
  499. $headers = array_merge(
  500. $defaultHeaders,
  501. $headerParams,
  502. $headers
  503. );
  504. $operationHost = $this->config->getHost();
  505. $query = ObjectSerializer::buildQuery($queryParams);
  506. return new Request(
  507. 'GET',
  508. $operationHost . $resourcePath . ($query ? "?{$query}" : ''),
  509. $headers,
  510. $httpBody
  511. );
  512. }
  513. /**
  514. * Create http client option
  515. *
  516. * @throws \RuntimeException on file opening failure
  517. * @return array of http client options
  518. */
  519. protected function createHttpClientOption()
  520. {
  521. $options = [];
  522. if ($this->config->getDebug()) {
  523. $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
  524. if (!$options[RequestOptions::DEBUG]) {
  525. throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
  526. }
  527. }
  528. if ($this->config->getCertFile()) {
  529. $options[RequestOptions::CERT] = $this->config->getCertFile();
  530. }
  531. if ($this->config->getKeyFile()) {
  532. $options[RequestOptions::SSL_KEY] = $this->config->getKeyFile();
  533. }
  534. return $options;
  535. }
  536. private function handleResponseWithDataType(
  537. string $dataType,
  538. RequestInterface $request,
  539. ResponseInterface $response
  540. ): array {
  541. if ($dataType === '\SplFileObject') {
  542. $content = $response->getBody(); //stream goes to serializer
  543. } else {
  544. $content = (string) $response->getBody();
  545. if ($dataType !== 'string') {
  546. try {
  547. $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
  548. } catch (\JsonException $exception) {
  549. throw new ApiException(
  550. sprintf(
  551. 'Error JSON decoding server response (%s)',
  552. $request->getUri()
  553. ),
  554. $response->getStatusCode(),
  555. $response->getHeaders(),
  556. $content
  557. );
  558. }
  559. }
  560. }
  561. return [
  562. ObjectSerializer::deserialize($content, $dataType, []),
  563. $response->getStatusCode(),
  564. $response->getHeaders()
  565. ];
  566. }
  567. private function responseWithinRangeCode(
  568. string $rangeCode,
  569. int $statusCode
  570. ): bool {
  571. $left = (int) ($rangeCode[0].'00');
  572. $right = (int) ($rangeCode[0].'99');
  573. return $statusCode >= $left && $statusCode <= $right;
  574. }
  575. }