TipitakaReadingApi.php 28 KB

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