PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/scormcloud/vendor/rustici-software/scormcloud-api-v2-client-php/src/Api/AuthenticationApi.php

https://github.com/RusticiSoftware/SCORMCloud_WordPressPlugin
PHP | 394 lines | 236 code | 37 blank | 121 comment | 27 complexity | 868d96025cd1182813ed4d9a527f29de MD5 | raw file
  1. <?php
  2. /**
  3. * AuthenticationApi
  4. * PHP version 7
  5. *
  6. * @category Class
  7. * @package RusticiSoftware\Cloud\V2
  8. * @author Swagger Codegen team
  9. * @link https://github.com/swagger-api/swagger-codegen
  10. */
  11. /**
  12. * SCORM Cloud Rest API
  13. *
  14. * REST API used for SCORM Cloud integrations.
  15. *
  16. * OpenAPI spec version: 2.0
  17. *
  18. * Generated by: https://github.com/swagger-api/swagger-codegen.git
  19. * Swagger Codegen version: 2.4.12
  20. */
  21. /**
  22. * NOTE: This class is auto generated by the swagger code generator program.
  23. * https://github.com/swagger-api/swagger-codegen
  24. * Do not edit the class manually.
  25. */
  26. namespace RusticiSoftware\Cloud\V2\Api;
  27. use GuzzleHttp\Client;
  28. use GuzzleHttp\ClientInterface;
  29. use GuzzleHttp\Exception\RequestException;
  30. use GuzzleHttp\Psr7\MultipartStream;
  31. use GuzzleHttp\Psr7\Request;
  32. use GuzzleHttp\RequestOptions;
  33. use RusticiSoftware\Cloud\V2\ApiException;
  34. use RusticiSoftware\Cloud\V2\Configuration;
  35. use RusticiSoftware\Cloud\V2\HeaderSelector;
  36. use RusticiSoftware\Cloud\V2\ObjectSerializer;
  37. /**
  38. * AuthenticationApi Class Doc Comment
  39. *
  40. * @category Class
  41. * @package RusticiSoftware\Cloud\V2
  42. * @author Swagger Codegen team
  43. * @link https://github.com/swagger-api/swagger-codegen
  44. */
  45. class AuthenticationApi
  46. {
  47. /**
  48. * @var ClientInterface
  49. */
  50. protected $client;
  51. /**
  52. * @var Configuration
  53. */
  54. protected $config;
  55. /**
  56. * @var HeaderSelector
  57. */
  58. protected $headerSelector;
  59. /**
  60. * @param ClientInterface $client
  61. * @param Configuration $config
  62. * @param HeaderSelector $selector
  63. */
  64. public function __construct(
  65. ClientInterface $client = null,
  66. Configuration $config = null,
  67. HeaderSelector $selector = null
  68. ) {
  69. $this->client = $client ?: new Client();
  70. $this->config = $config ?: Configuration::getDefaultConfiguration();
  71. $this->headerSelector = $selector ?: new HeaderSelector();
  72. }
  73. /**
  74. * @return Configuration
  75. */
  76. public function getConfig()
  77. {
  78. return $this->config;
  79. }
  80. /**
  81. * Operation getAppToken
  82. *
  83. * Obtain an OAuth token for scoped access to an Application
  84. *
  85. * @param string $scope Space separated string of OAuth scopes, e.g. \&quot;write:course read:registration\&quot;. (required)
  86. * @param int $expiration Amount of seconds until the OAuth token expires. (optional, default to 300)
  87. *
  88. * @throws \RusticiSoftware\Cloud\V2\ApiException on non-2xx response
  89. * @throws \InvalidArgumentException
  90. * @return \RusticiSoftware\Cloud\V2\Model\ApplicationToken
  91. */
  92. public function getAppToken($scope, $expiration = '300')
  93. {
  94. list($response) = $this->getAppTokenWithHttpInfo($scope, $expiration);
  95. return $response;
  96. }
  97. /**
  98. * Operation getAppTokenWithHttpInfo
  99. *
  100. * Obtain an OAuth token for scoped access to an Application
  101. *
  102. * @param string $scope Space separated string of OAuth scopes, e.g. \&quot;write:course read:registration\&quot;. (required)
  103. * @param int $expiration Amount of seconds until the OAuth token expires. (optional, default to 300)
  104. *
  105. * @throws \RusticiSoftware\Cloud\V2\ApiException on non-2xx response
  106. * @throws \InvalidArgumentException
  107. * @return array of \RusticiSoftware\Cloud\V2\Model\ApplicationToken, HTTP status code, HTTP response headers (array of strings)
  108. */
  109. public function getAppTokenWithHttpInfo($scope, $expiration = '300')
  110. {
  111. $returnType = '\RusticiSoftware\Cloud\V2\Model\ApplicationToken';
  112. $request = $this->getAppTokenRequest($scope, $expiration);
  113. try {
  114. $options = $this->createHttpClientOption();
  115. try {
  116. $response = $this->client->send($request, $options);
  117. } catch (RequestException $e) {
  118. throw new ApiException(
  119. "[{$e->getCode()}] {$e->getMessage()}",
  120. $e->getCode(),
  121. $e->getResponse() ? $e->getResponse()->getHeaders() : null,
  122. $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null
  123. );
  124. }
  125. $statusCode = $response->getStatusCode();
  126. if ($statusCode < 200 || $statusCode > 299) {
  127. throw new ApiException(
  128. sprintf(
  129. '[%d] Error connecting to the API (%s)',
  130. $statusCode,
  131. $request->getUri()
  132. ),
  133. $statusCode,
  134. $response->getHeaders(),
  135. $response->getBody()
  136. );
  137. }
  138. $responseBody = $response->getBody();
  139. if ($returnType === '\SplFileObject') {
  140. $content = $responseBody; //stream goes to serializer
  141. } else {
  142. $content = $responseBody->getContents();
  143. if ($returnType !== 'string') {
  144. $content = json_decode($content);
  145. }
  146. }
  147. return [
  148. ObjectSerializer::deserialize($content, $returnType, []),
  149. $response->getStatusCode(),
  150. $response->getHeaders()
  151. ];
  152. } catch (ApiException $e) {
  153. switch ($e->getCode()) {
  154. case 200:
  155. $data = ObjectSerializer::deserialize(
  156. $e->getResponseBody(),
  157. '\RusticiSoftware\Cloud\V2\Model\ApplicationToken',
  158. $e->getResponseHeaders()
  159. );
  160. $e->setResponseObject($data);
  161. break;
  162. case 400:
  163. $data = ObjectSerializer::deserialize(
  164. $e->getResponseBody(),
  165. '\RusticiSoftware\Cloud\V2\Model\MessageSchema',
  166. $e->getResponseHeaders()
  167. );
  168. $e->setResponseObject($data);
  169. break;
  170. }
  171. throw $e;
  172. }
  173. }
  174. /**
  175. * Operation getAppTokenAsync
  176. *
  177. * Obtain an OAuth token for scoped access to an Application
  178. *
  179. * @param string $scope Space separated string of OAuth scopes, e.g. \&quot;write:course read:registration\&quot;. (required)
  180. * @param int $expiration Amount of seconds until the OAuth token expires. (optional, default to 300)
  181. *
  182. * @throws \InvalidArgumentException
  183. * @return \GuzzleHttp\Promise\PromiseInterface
  184. */
  185. public function getAppTokenAsync($scope, $expiration = '300')
  186. {
  187. return $this->getAppTokenAsyncWithHttpInfo($scope, $expiration)
  188. ->then(
  189. function ($response) {
  190. return $response[0];
  191. }
  192. );
  193. }
  194. /**
  195. * Operation getAppTokenAsyncWithHttpInfo
  196. *
  197. * Obtain an OAuth token for scoped access to an Application
  198. *
  199. * @param string $scope Space separated string of OAuth scopes, e.g. \&quot;write:course read:registration\&quot;. (required)
  200. * @param int $expiration Amount of seconds until the OAuth token expires. (optional, default to 300)
  201. *
  202. * @throws \InvalidArgumentException
  203. * @return \GuzzleHttp\Promise\PromiseInterface
  204. */
  205. public function getAppTokenAsyncWithHttpInfo($scope, $expiration = '300')
  206. {
  207. $returnType = '\RusticiSoftware\Cloud\V2\Model\ApplicationToken';
  208. $request = $this->getAppTokenRequest($scope, $expiration);
  209. return $this->client
  210. ->sendAsync($request, $this->createHttpClientOption())
  211. ->then(
  212. function ($response) use ($returnType) {
  213. $responseBody = $response->getBody();
  214. if ($returnType === '\SplFileObject') {
  215. $content = $responseBody; //stream goes to serializer
  216. } else {
  217. $content = $responseBody->getContents();
  218. if ($returnType !== 'string') {
  219. $content = json_decode($content);
  220. }
  221. }
  222. return [
  223. ObjectSerializer::deserialize($content, $returnType, []),
  224. $response->getStatusCode(),
  225. $response->getHeaders()
  226. ];
  227. },
  228. function ($exception) {
  229. $response = $exception->getResponse();
  230. $statusCode = $response->getStatusCode();
  231. throw new ApiException(
  232. sprintf(
  233. '[%d] Error connecting to the API (%s)',
  234. $statusCode,
  235. $exception->getRequest()->getUri()
  236. ),
  237. $statusCode,
  238. $response->getHeaders(),
  239. $response->getBody()
  240. );
  241. }
  242. );
  243. }
  244. /**
  245. * Create request for operation 'getAppToken'
  246. *
  247. * @param string $scope Space separated string of OAuth scopes, e.g. \&quot;write:course read:registration\&quot;. (required)
  248. * @param int $expiration Amount of seconds until the OAuth token expires. (optional, default to 300)
  249. *
  250. * @throws \InvalidArgumentException
  251. * @return \GuzzleHttp\Psr7\Request
  252. */
  253. protected function getAppTokenRequest($scope, $expiration = '300')
  254. {
  255. // verify the required parameter 'scope' is set
  256. if ($scope === null || (is_array($scope) && count($scope) === 0)) {
  257. throw new \InvalidArgumentException(
  258. 'Missing the required parameter $scope when calling getAppToken'
  259. );
  260. }
  261. $resourcePath = '/oauth/authenticate/application/token';
  262. $formParams = [];
  263. $queryParams = [];
  264. $headerParams = [];
  265. $httpBody = '';
  266. $multipart = false;
  267. // form params
  268. if ($scope !== null) {
  269. $formParams['scope'] = ObjectSerializer::toFormValue($scope);
  270. }
  271. // form params
  272. if ($expiration !== null) {
  273. $formParams['expiration'] = ObjectSerializer::toFormValue($expiration);
  274. }
  275. // body params
  276. $_tempBody = null;
  277. if ($multipart) {
  278. $headers = $this->headerSelector->selectHeadersForMultipart(
  279. ['application/json']
  280. );
  281. } else {
  282. $headers = $this->headerSelector->selectHeaders(
  283. ['application/json'],
  284. ['application/x-www-form-urlencoded']
  285. );
  286. }
  287. // for model (json/xml)
  288. if (isset($_tempBody)) {
  289. // $_tempBody is the method argument, if present
  290. $httpBody = $_tempBody;
  291. if($headers['Content-Type'] === 'application/json') {
  292. // \stdClass has no __toString(), so we should encode it manually
  293. if ($httpBody instanceof \stdClass) {
  294. $httpBody = \GuzzleHttp\json_encode($httpBody);
  295. }
  296. // array has no __toString(), so we should encode it manually
  297. if(is_array($httpBody)) {
  298. $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($httpBody));
  299. }
  300. }
  301. } elseif (count($formParams) > 0) {
  302. if ($multipart) {
  303. $multipartContents = [];
  304. foreach ($formParams as $formParamName => $formParamValue) {
  305. $multipartContents[] = [
  306. 'name' => $formParamName,
  307. 'contents' => $formParamValue
  308. ];
  309. }
  310. // for HTTP post (form)
  311. $httpBody = new MultipartStream($multipartContents);
  312. } elseif ($headers['Content-Type'] === 'application/json') {
  313. $httpBody = \GuzzleHttp\json_encode($formParams);
  314. } else {
  315. // for HTTP post (form)
  316. $httpBody = \GuzzleHttp\Psr7\Query::build($formParams);
  317. }
  318. }
  319. // this endpoint requires HTTP basic authentication
  320. if ($this->config->getUsername() !== null || $this->config->getPassword() !== null) {
  321. $headers['Authorization'] = 'Basic ' . base64_encode($this->config->getUsername() . ":" . $this->config->getPassword());
  322. }
  323. $defaultHeaders = [];
  324. if ($this->config->getUserAgent()) {
  325. $defaultHeaders['User-Agent'] = $this->config->getUserAgent();
  326. }
  327. $headers = array_merge(
  328. $defaultHeaders,
  329. $headerParams,
  330. $headers
  331. );
  332. $query = \GuzzleHttp\Psr7\Query::build($queryParams);
  333. return new Request(
  334. 'POST',
  335. $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''),
  336. $headers,
  337. $httpBody
  338. );
  339. }
  340. /**
  341. * Create http client option
  342. *
  343. * @throws \RuntimeException on file opening failure
  344. * @return array of http client options
  345. */
  346. protected function createHttpClientOption()
  347. {
  348. $options = [];
  349. if ($this->config->getDebug()) {
  350. $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
  351. if (!$options[RequestOptions::DEBUG]) {
  352. throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
  353. }
  354. }
  355. return $options;
  356. }
  357. }