[ 'application/json', ], ]; /** * @param ClientInterface $client * @param Configuration $config * @param HeaderSelector $selector * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec */ public function __construct( ?ClientInterface $client = null, ?Configuration $config = null, ?HeaderSelector $selector = null, int $hostIndex = 0 ) { $this->client = $client ?: new Client(); $this->config = $config ?: Configuration::getDefaultConfiguration(); $this->headerSelector = $selector ?: new HeaderSelector(); $this->hostIndex = $hostIndex; } /** * Set the host index * * @param int $hostIndex Host index (required) */ public function setHostIndex($hostIndex): void { $this->hostIndex = $hostIndex; } /** * Get the host index * * @return int Host index */ public function getHostIndex() { return $this->hostIndex; } /** * @return Configuration */ public function getConfig() { return $this->config; } /** * Operation getApiV3TipitakaReadingChannel * * 读取译文 * * @param string $channel 译文 channel 的 uuid (required) * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional) * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional) * @param int|null $para 起始段落号 (optional) * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional) * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional) * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit=para 时 200、 unit=byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10) * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para') * @param string|null $format 内容格式 (optional, default to 'html') * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 `view=` 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display') * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation * * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return \WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response|\WikipaliApi\Model\ProblemDetails|\WikipaliApi\Model\ProblemDetails */ 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]) { list($response) = $this->getApiV3TipitakaReadingChannelWithHttpInfo($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType); return $response; } /** * Operation getApiV3TipitakaReadingChannelWithHttpInfo * * 读取译文 * * @param string $channel 译文 channel 的 uuid (required) * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional) * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional) * @param int|null $para 起始段落号 (optional) * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional) * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional) * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit=para 时 200、 unit=byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10) * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para') * @param string|null $format 内容格式 (optional, default to 'html') * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 `view=` 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display') * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation * * @throws \WikipaliApi\ApiException on non-2xx response or if the response body is not in the expected format * @throws \InvalidArgumentException * @return array of \WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response|\WikipaliApi\Model\ProblemDetails|\WikipaliApi\Model\ProblemDetails, HTTP status code, HTTP response headers (array of strings) */ 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]) { $request = $this->getApiV3TipitakaReadingChannelRequest($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType); try { $options = $this->createHttpClientOption(); try { $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", (int) $e->getCode(), $e->getResponse() ? $e->getResponse()->getHeaders() : null, $e->getResponse() ? (string) $e->getResponse()->getBody() : null ); } catch (ConnectException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", (int) $e->getCode(), null, null ); } $statusCode = $response->getStatusCode(); switch($statusCode) { case 200: return $this->handleResponseWithDataType( '\WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response', $request, $response, ); case 404: return $this->handleResponseWithDataType( '\WikipaliApi\Model\ProblemDetails', $request, $response, ); case 422: return $this->handleResponseWithDataType( '\WikipaliApi\Model\ProblemDetails', $request, $response, ); } if ($statusCode < 200 || $statusCode > 299) { throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', $statusCode, (string) $request->getUri() ), $statusCode, $response->getHeaders(), (string) $response->getBody() ); } return $this->handleResponseWithDataType( '\WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response', $request, $response, ); } catch (ApiException $e) { switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( $e->getResponseBody(), '\WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response', $e->getResponseHeaders() ); $e->setResponseObject($data); throw $e; case 404: $data = ObjectSerializer::deserialize( $e->getResponseBody(), '\WikipaliApi\Model\ProblemDetails', $e->getResponseHeaders() ); $e->setResponseObject($data); throw $e; case 422: $data = ObjectSerializer::deserialize( $e->getResponseBody(), '\WikipaliApi\Model\ProblemDetails', $e->getResponseHeaders() ); $e->setResponseObject($data); throw $e; } throw $e; } } /** * Operation getApiV3TipitakaReadingChannelAsync * * 读取译文 * * @param string $channel 译文 channel 的 uuid (required) * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional) * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional) * @param int|null $para 起始段落号 (optional) * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional) * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional) * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit=para 时 200、 unit=byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10) * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para') * @param string|null $format 内容格式 (optional, default to 'html') * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 `view=` 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display') * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ 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]) { return $this->getApiV3TipitakaReadingChannelAsyncWithHttpInfo($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType) ->then( function ($response) { return $response[0]; } ); } /** * Operation getApiV3TipitakaReadingChannelAsyncWithHttpInfo * * 读取译文 * * @param string $channel 译文 channel 的 uuid (required) * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional) * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional) * @param int|null $para 起始段落号 (optional) * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional) * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional) * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit=para 时 200、 unit=byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10) * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para') * @param string|null $format 内容格式 (optional, default to 'html') * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 `view=` 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display') * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ 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]) { $returnType = '\WikipaliApi\Model\GetApiV3TipitakaReadingChannel200Response'; $request = $this->getApiV3TipitakaReadingChannelRequest($channel, $book, $chapter, $para, $to, $after, $page_size, $unit, $format, $include, $contentType); return $this->client ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { $content = $response->getBody(); //stream goes to serializer } else { $content = (string) $response->getBody(); if ($returnType !== 'string') { $content = json_decode($content); } } return [ ObjectSerializer::deserialize($content, $returnType, []), $response->getStatusCode(), $response->getHeaders() ]; }, function ($exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', $statusCode, $exception->getRequest()->getUri() ), $statusCode, $response->getHeaders(), (string) $response->getBody() ); } ); } /** * Create request for operation 'getApiV3TipitakaReadingChannel' * * @param string $channel 译文 channel 的 uuid (required) * @param int|null $book 典籍 id。与 chapter / para / to 一起用时必填 (optional) * @param int|null $chapter 章节起始段落号,服务端按 chapter_len 展开成段落区间。 与 para / to 互斥 (optional) * @param int|null $para 起始段落号 (optional) * @param int|null $to 结束段落号(含)。不传则等于 para,即只取一段 (optional) * @param string|null $after 游标,取 meta.next_cursor 原样回传 (optional) * @param int|null $page_size 每块大小,含义由 unit 决定。上限 unit=para 时 200、 unit=byte 时 5000,超出不报错按上限算,meta.page_size 回实际生效的值 (optional, default to 10) * @param string|null $unit 每块大小的单位:para 按段落数,byte 按段落长度累加。 两种单位下每块都至少一段、至多 200 段 (optional, default to 'para') * @param string|null $format 内容格式 (optional, default to 'html') * @param string|null $include 输出口径:display 整段合并,sentences 逐句,all 两者都给。 不叫 view 是因为 v2 的 `view=` 是必填互斥的业务路径开关,名字撞上会误导 (optional, default to 'display') * @param string $contentType The value for the Content-Type header. Check self::contentTypes['getApiV3TipitakaReadingChannel'] to see the possible values for this operation * * @throws \InvalidArgumentException * @return \GuzzleHttp\Psr7\Request */ 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]) { // verify the required parameter 'channel' is set if ($channel === null || (is_array($channel) && count($channel) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $channel when calling getApiV3TipitakaReadingChannel' ); } $resourcePath = '/v3/tipitaka-reading/{channel}'; $formParams = []; $queryParams = []; $headerParams = []; $httpBody = ''; $multipart = false; // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $book, 'book', // param base name 'integer', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $chapter, 'chapter', // param base name 'integer', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $para, 'para', // param base name 'integer', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $to, 'to', // param base name 'integer', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $after, 'after', // param base name 'string', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $page_size, 'page_size', // param base name 'integer', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $unit, 'unit', // param base name 'string', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $format, 'format', // param base name 'string', // openApiType 'form', // style true, // explode false // required ) ?? []); // query params $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue( $include, 'include', // param base name 'string', // openApiType 'form', // style true, // explode false // required ) ?? []); // path params if ($channel !== null) { $resourcePath = str_replace( '{channel}', ObjectSerializer::toPathValue($channel), $resourcePath ); } $headers = $this->headerSelector->selectHeaders( ['application/json', 'application/problem+json', ], $contentType, $multipart ); // for model (json/xml) if (count($formParams) > 0) { if ($multipart) { $multipartContents = []; foreach ($formParams as $formParamName => $formParamValue) { $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue]; foreach ($formParamValueItems as $formParamValueItem) { $multipartContents[] = [ 'name' => $formParamName, 'contents' => $formParamValueItem ]; } } // for HTTP post (form) $httpBody = new MultipartStream($multipartContents); } elseif (stripos($headers['Content-Type'], 'application/json') !== false) { # if Content-Type contains "application/json", json_encode the form parameters try { $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } catch (\JsonException $e) { throw new \InvalidArgumentException('json_encode error: ' . $e->getMessage(), 0, $e); } } else { // for HTTP post (form) $httpBody = ObjectSerializer::buildQuery($formParams); } } // this endpoint requires Bearer authentication (access token) if (!empty($this->config->getAccessToken())) { $headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken(); } $defaultHeaders = []; if ($this->config->getUserAgent()) { $defaultHeaders['User-Agent'] = $this->config->getUserAgent(); } $headers = array_merge( $defaultHeaders, $headerParams, $headers ); $operationHost = $this->config->getHost(); $query = ObjectSerializer::buildQuery($queryParams); return new Request( 'GET', $operationHost . $resourcePath . ($query ? "?{$query}" : ''), $headers, $httpBody ); } /** * Create http client option * * @throws \RuntimeException on file opening failure * @return array of http client options */ protected function createHttpClientOption() { $options = []; if ($this->config->getDebug()) { $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); if (!$options[RequestOptions::DEBUG]) { throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); } } if ($this->config->getCertFile()) { $options[RequestOptions::CERT] = $this->config->getCertFile(); } if ($this->config->getKeyFile()) { $options[RequestOptions::SSL_KEY] = $this->config->getKeyFile(); } return $options; } private function handleResponseWithDataType( string $dataType, RequestInterface $request, ResponseInterface $response ): array { if ($dataType === '\SplFileObject') { $content = $response->getBody(); //stream goes to serializer } else { $content = (string) $response->getBody(); if ($dataType !== 'string') { try { $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR); } catch (\JsonException $exception) { throw new ApiException( sprintf( 'Error JSON decoding server response (%s)', $request->getUri() ), $response->getStatusCode(), $response->getHeaders(), $content ); } } } return [ ObjectSerializer::deserialize($content, $dataType, []), $response->getStatusCode(), $response->getHeaders() ]; } private function responseWithinRangeCode( string $rangeCode, int $statusCode ): bool { $left = (int) ($rangeCode[0].'00'); $right = (int) ($rangeCode[0].'99'); return $statusCode >= $left && $statusCode <= $right; } }