lib/Api/LiveApi.php (498 lines of code) (raw):

<?php /** * LiveApi * PHP version 5 * * @category Class * @package Yjopenapi\Client */ /** Package client * * * YuanJing OpenAPI SDK for PHP * * * */ namespace Yjopenapi\Client\Api; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; use GuzzleHttp\RequestOptions; use Yjopenapi\Client\Api\ApiException; use Yjopenapi\Client\Api\Configuration; use Yjopenapi\Client\Api\HeaderSelector; use Yjopenapi\Client\Api\ObjectSerializer; /** * LiveApi Class Doc Comment * * @category Class * @package Yjopenapi\Client */ class LiveApi { /** * @var ClientInterface */ protected $client; /** * @var Configuration */ protected $config; /** * @var HeaderSelector */ protected $headerSelector; /** * @param ClientInterface $client * @param Configuration $config * @param HeaderSelector $selector */ public function __construct( ClientInterface $client = null, Configuration $config = null, HeaderSelector $selector = null ) { $this->client = $client ?: new Client(); $this->config = $config ?: new Configuration(); $this->headerSelector = $selector ?: new HeaderSelector(); } /** * @return Configuration */ public function getConfig() { return $this->config; } /** * Operation queryStatus * * @param \Yjopenapi\Client\Model\LiveQueryStatusForms $varForms * * @throws \Yjopenapi\Client\Api\ApiException on non-2xx response * @throws \InvalidArgumentException * @return \Yjopenapi\Client\Model\LiveQueryStatusResult */ public function queryStatus($varForms) { list($response) = $this->queryStatusWithHttpInfo($varForms); return $response; } /** * Operation queryStatusWithHttpInfo * * @param \Yjopenapi\Client\Model\LiveQueryStatusForms $varForms * * @throws \Yjopenapi\Client\Api\ApiException on non-2xx response * @throws \InvalidArgumentException * @return array of \Yjopenapi\Client\Model\LiveQueryStatusResult, HTTP status code, HTTP response headers (array of strings) */ public function queryStatusWithHttpInfo($varForms) { $returnType = '\Yjopenapi\Client\Model\LiveQueryStatusResult'; $request = $this->queryStatusRequest($varForms); try { $options = $this->createHttpClientOption(); try { $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", $e->getCode(), $e->getResponse() ? $e->getResponse()->getHeaders() : null, $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null ); } $statusCode = $response->getStatusCode(); if ($statusCode < 200 || $statusCode > 299) { throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', $statusCode, $request->getUri() ), $statusCode, $response->getHeaders(), $response->getBody() ); } $responseBody = $response->getBody(); $content = $responseBody->getContents(); if (!in_array($returnType, ['string','integer','bool'])) { $content = json_decode($content); } return [ ObjectSerializer::deserialize($content, $returnType, []), $response->getStatusCode(), $response->getHeaders() ]; } catch (ApiException $e) { switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( $e->getResponseBody(), '\Yjopenapi\Client\Model\LiveQueryStatusResult', $e->getResponseHeaders() ); $e->setResponseObject($data); break; } throw $e; } } /** * Operation queryStatusAsync * * * * @param \Yjopenapi\Client\Model\LiveQueryStatusForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ public function queryStatusAsync($varForms) { return $this->queryStatusAsyncWithHttpInfo($varForms) ->then( function ($response) { return $response[0]; } ); } /** * Operation queryStatusAsyncWithHttpInfo * * * * @param \Yjopenapi\Client\Model\LiveQueryStatusForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ public function queryStatusAsyncWithHttpInfo($varForms) { $returnType = '\Yjopenapi\Client\Model\LiveQueryStatusResult'; $request = $this->queryStatusRequest($varForms); return $this->client ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); $content = $responseBody->getContents(); 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(), $response->getBody() ); } ); } /** * Create request for operation 'queryStatus' * * @param \Yjopenapi\Client\Model\LiveQueryStatusForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Psr7\Request */ protected function queryStatusRequest($varForms) { // verify the required parameter 'app_key' is set if ($varForms['app_key'] === null || (is_array($varForms['app_key']) && count($varForms['app_key']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'app_key\'] when calling queryStatus' ); } // verify the required parameter 'game_session' is set if ($varForms['game_session'] === null || (is_array($varForms['game_session']) && count($varForms['game_session']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'game_session\'] when calling queryStatus' ); } $resourcePath = '/live/queryStatus'; $formParams = []; $queryParams = []; $headerParams = []; $httpBody = ''; // form params $formParams['appKey'] = ObjectSerializer::toFormValue($varForms['app_key']); $formParams['gameSession'] = ObjectSerializer::toFormValue($varForms['game_session']); if ($varForms['live_id'] !== null) { $formParams['liveId'] = ObjectSerializer::toFormValue($varForms['live_id']); } $headers = $this->headerSelector->selectHeaders( ['application/json'], ['application/x-www-form-urlencoded'] ); // for model (json/xml) if (count($formParams) > 0) { if ($headers['Content-Type'] === 'application/json') { $httpBody = \GuzzleHttp\json_encode($formParams); } else { // for HTTP post (form) $httpBody = \GuzzleHttp\Psr7\build_query($formParams); } } $signHeaders = $this->headerSelector->prepareSignHeader($formParams, $queryParams, 'POST', $this->config); $headers = array_merge( ['User-Agent' => 'cgw-client/1.0.0/php'], $signHeaders, $headerParams, $headers ); $query = \GuzzleHttp\Psr7\build_query($queryParams); return new Request( 'POST', $this->config->getScheme() . '://' . $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), $headers, $httpBody ); } /** * Operation startGameLive * * @param \Yjopenapi\Client\Model\LiveStartGameLiveForms $varForms * * @throws \Yjopenapi\Client\Api\ApiException on non-2xx response * @throws \InvalidArgumentException * @return \Yjopenapi\Client\Model\LiveStartGameLiveResult */ public function startGameLive($varForms) { list($response) = $this->startGameLiveWithHttpInfo($varForms); return $response; } /** * Operation startGameLiveWithHttpInfo * * @param \Yjopenapi\Client\Model\LiveStartGameLiveForms $varForms * * @throws \Yjopenapi\Client\Api\ApiException on non-2xx response * @throws \InvalidArgumentException * @return array of \Yjopenapi\Client\Model\LiveStartGameLiveResult, HTTP status code, HTTP response headers (array of strings) */ public function startGameLiveWithHttpInfo($varForms) { $returnType = '\Yjopenapi\Client\Model\LiveStartGameLiveResult'; $request = $this->startGameLiveRequest($varForms); try { $options = $this->createHttpClientOption(); try { $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", $e->getCode(), $e->getResponse() ? $e->getResponse()->getHeaders() : null, $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null ); } $statusCode = $response->getStatusCode(); if ($statusCode < 200 || $statusCode > 299) { throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', $statusCode, $request->getUri() ), $statusCode, $response->getHeaders(), $response->getBody() ); } $responseBody = $response->getBody(); $content = $responseBody->getContents(); if (!in_array($returnType, ['string','integer','bool'])) { $content = json_decode($content); } return [ ObjectSerializer::deserialize($content, $returnType, []), $response->getStatusCode(), $response->getHeaders() ]; } catch (ApiException $e) { switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( $e->getResponseBody(), '\Yjopenapi\Client\Model\LiveStartGameLiveResult', $e->getResponseHeaders() ); $e->setResponseObject($data); break; } throw $e; } } /** * Operation startGameLiveAsync * * * * @param \Yjopenapi\Client\Model\LiveStartGameLiveForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ public function startGameLiveAsync($varForms) { return $this->startGameLiveAsyncWithHttpInfo($varForms) ->then( function ($response) { return $response[0]; } ); } /** * Operation startGameLiveAsyncWithHttpInfo * * * * @param \Yjopenapi\Client\Model\LiveStartGameLiveForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ public function startGameLiveAsyncWithHttpInfo($varForms) { $returnType = '\Yjopenapi\Client\Model\LiveStartGameLiveResult'; $request = $this->startGameLiveRequest($varForms); return $this->client ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); $content = $responseBody->getContents(); 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(), $response->getBody() ); } ); } /** * Create request for operation 'startGameLive' * * @param \Yjopenapi\Client\Model\LiveStartGameLiveForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Psr7\Request */ protected function startGameLiveRequest($varForms) { // verify the required parameter 'app_key' is set if ($varForms['app_key'] === null || (is_array($varForms['app_key']) && count($varForms['app_key']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'app_key\'] when calling startGameLive' ); } // verify the required parameter 'game_session' is set if ($varForms['game_session'] === null || (is_array($varForms['game_session']) && count($varForms['game_session']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'game_session\'] when calling startGameLive' ); } // verify the required parameter 'server_url' is set if ($varForms['server_url'] === null || (is_array($varForms['server_url']) && count($varForms['server_url']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'server_url\'] when calling startGameLive' ); } // verify the required parameter 'stream_key' is set if ($varForms['stream_key'] === null || (is_array($varForms['stream_key']) && count($varForms['stream_key']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'stream_key\'] when calling startGameLive' ); } $resourcePath = '/live/startGameLive'; $formParams = []; $queryParams = []; $headerParams = []; $httpBody = ''; // form params $formParams['appKey'] = ObjectSerializer::toFormValue($varForms['app_key']); $formParams['gameSession'] = ObjectSerializer::toFormValue($varForms['game_session']); $formParams['serverUrl'] = ObjectSerializer::toFormValue($varForms['server_url']); $formParams['streamKey'] = ObjectSerializer::toFormValue($varForms['stream_key']); if ($varForms['config'] !== null) { $formParams['config'] = ObjectSerializer::toFormValue($varForms['config']); } $headers = $this->headerSelector->selectHeaders( ['application/json'], ['application/x-www-form-urlencoded'] ); // for model (json/xml) if (count($formParams) > 0) { if ($headers['Content-Type'] === 'application/json') { $httpBody = \GuzzleHttp\json_encode($formParams); } else { // for HTTP post (form) $httpBody = \GuzzleHttp\Psr7\build_query($formParams); } } $signHeaders = $this->headerSelector->prepareSignHeader($formParams, $queryParams, 'POST', $this->config); $headers = array_merge( ['User-Agent' => 'cgw-client/1.0.0/php'], $signHeaders, $headerParams, $headers ); $query = \GuzzleHttp\Psr7\build_query($queryParams); return new Request( 'POST', $this->config->getScheme() . '://' . $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), $headers, $httpBody ); } /** * Operation stopGameLive * * @param \Yjopenapi\Client\Model\LiveStopGameLiveForms $varForms * * @throws \Yjopenapi\Client\Api\ApiException on non-2xx response * @throws \InvalidArgumentException * @return \Yjopenapi\Client\Model\LiveStopGameLiveResult */ public function stopGameLive($varForms) { list($response) = $this->stopGameLiveWithHttpInfo($varForms); return $response; } /** * Operation stopGameLiveWithHttpInfo * * @param \Yjopenapi\Client\Model\LiveStopGameLiveForms $varForms * * @throws \Yjopenapi\Client\Api\ApiException on non-2xx response * @throws \InvalidArgumentException * @return array of \Yjopenapi\Client\Model\LiveStopGameLiveResult, HTTP status code, HTTP response headers (array of strings) */ public function stopGameLiveWithHttpInfo($varForms) { $returnType = '\Yjopenapi\Client\Model\LiveStopGameLiveResult'; $request = $this->stopGameLiveRequest($varForms); try { $options = $this->createHttpClientOption(); try { $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", $e->getCode(), $e->getResponse() ? $e->getResponse()->getHeaders() : null, $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null ); } $statusCode = $response->getStatusCode(); if ($statusCode < 200 || $statusCode > 299) { throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', $statusCode, $request->getUri() ), $statusCode, $response->getHeaders(), $response->getBody() ); } $responseBody = $response->getBody(); $content = $responseBody->getContents(); if (!in_array($returnType, ['string','integer','bool'])) { $content = json_decode($content); } return [ ObjectSerializer::deserialize($content, $returnType, []), $response->getStatusCode(), $response->getHeaders() ]; } catch (ApiException $e) { switch ($e->getCode()) { case 200: $data = ObjectSerializer::deserialize( $e->getResponseBody(), '\Yjopenapi\Client\Model\LiveStopGameLiveResult', $e->getResponseHeaders() ); $e->setResponseObject($data); break; } throw $e; } } /** * Operation stopGameLiveAsync * * * * @param \Yjopenapi\Client\Model\LiveStopGameLiveForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ public function stopGameLiveAsync($varForms) { return $this->stopGameLiveAsyncWithHttpInfo($varForms) ->then( function ($response) { return $response[0]; } ); } /** * Operation stopGameLiveAsyncWithHttpInfo * * * * @param \Yjopenapi\Client\Model\LiveStopGameLiveForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Promise\PromiseInterface */ public function stopGameLiveAsyncWithHttpInfo($varForms) { $returnType = '\Yjopenapi\Client\Model\LiveStopGameLiveResult'; $request = $this->stopGameLiveRequest($varForms); return $this->client ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); $content = $responseBody->getContents(); 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(), $response->getBody() ); } ); } /** * Create request for operation 'stopGameLive' * * @param \Yjopenapi\Client\Model\LiveStopGameLiveForms $varForms * * @throws \InvalidArgumentException * @return \GuzzleHttp\Psr7\Request */ protected function stopGameLiveRequest($varForms) { // verify the required parameter 'app_key' is set if ($varForms['app_key'] === null || (is_array($varForms['app_key']) && count($varForms['app_key']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'app_key\'] when calling stopGameLive' ); } // verify the required parameter 'game_session' is set if ($varForms['game_session'] === null || (is_array($varForms['game_session']) && count($varForms['game_session']) === 0)) { throw new \InvalidArgumentException( 'Missing the required parameter $varForms[\'game_session\'] when calling stopGameLive' ); } $resourcePath = '/live/stopGameLive'; $formParams = []; $queryParams = []; $headerParams = []; $httpBody = ''; // form params $formParams['appKey'] = ObjectSerializer::toFormValue($varForms['app_key']); $formParams['gameSession'] = ObjectSerializer::toFormValue($varForms['game_session']); if ($varForms['live_id'] !== null) { $formParams['liveId'] = ObjectSerializer::toFormValue($varForms['live_id']); } $headers = $this->headerSelector->selectHeaders( ['application/json'], ['application/x-www-form-urlencoded'] ); // for model (json/xml) if (count($formParams) > 0) { if ($headers['Content-Type'] === 'application/json') { $httpBody = \GuzzleHttp\json_encode($formParams); } else { // for HTTP post (form) $httpBody = \GuzzleHttp\Psr7\build_query($formParams); } } $signHeaders = $this->headerSelector->prepareSignHeader($formParams, $queryParams, 'POST', $this->config); $headers = array_merge( ['User-Agent' => 'cgw-client/1.0.0/php'], $signHeaders, $headerParams, $headers ); $query = \GuzzleHttp\Psr7\build_query($queryParams); return new Request( 'POST', $this->config->getScheme() . '://' . $this->config->getHost() . $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()); // } //} return $options; } }