protected function execute(InputInterface $input, OutputInterface $output) { $sapiToken = $input->getArgument('sapiToken'); $storageApi = new Client(['token' => $sapiToken, 'userAgent' => $this->componentName]); $encryptionKey = $input->getArgument('encryptionKey'); $encryptor = new Encryptor($encryptionKey); // Init new SYS bucket $configuration = new Configuration($this->componentName, $encryptor); $configuration->setStorageApi($storageApi); try { $configuration->create(); } catch (\Exception $e) { // do nothing, bucket probably exists } // Get configuration $sysBucketId = 'sys.c-ex-google-analytics'; if (!$storageApi->bucketExists($sysBucketId)) { throw new \Exception("No SYS bucket found"); } $accounts = $configuration->getAccounts(); /** @var Account $account */ foreach ($accounts as $account) { $cfg = $this->renameItems($account->getConfiguration()); $account->setConfiguration($cfg); $account->save(); } }
public function uploadCsv($file, $tableId, $incremental = false) { $sapi = $this->configuration->getStorageApi(); $table = new Table($sapi, $tableId, $file, 'id', false, ',', '"', $incremental); try { $table->save(true); } catch (ClientException $e) { throw new UserException('Error while uploading data to StorageAPI: ' . $e->getMessage(), $e); } }
protected function getOutputTable(Account $account, $tableName) { $outputBucket = $this->configuration->getInBucketId($account->getAccountId()); if ($account->getAttribute('outputBucket') != null) { $outputBucket = $account->getAttribute('outputBucket'); } return $outputBucket . '.' . $tableName; }
/** * @param Request $request * @return JsonResponse */ public function postConfigsAction(Request $request) { $params = $this->getPostJson($request); $this->checkParams(array('name'), $params); try { $this->getConfiguration()->exists(); } catch (ConfigurationException $e) { $this->configuration->create(); } $params['accountName'] = $params['name']; unset($params['name']); if (null != $this->getConfiguration()->getAccountBy('accountId', $this->configuration->getIdFromName($params['accountName']))) { throw new ConfigurationException('Account already exists'); } $account = $this->getConfiguration()->addAccount($params); return $this->createJsonResponse(array('id' => $account->getAccountId(), 'name' => $account->getAccountName(), 'description' => $account->getDescription())); }
/** * Profiles */ public function testPostProfiles() { $this->createConfig(); $this->createAccount(); self::$client->request('POST', '/ex-google-analytics/profiles/test', array(), array(), array(), json_encode(array(array('googleId' => '987654321', 'accountId' => '567890', 'accountName' => 'accountTest', 'name' => 'testProfile', 'webPropertyId' => 'web-property-id', 'webPropertyName' => 'web-property-name')))); /* @var Response $responseJson */ $responseJson = self::$client->getResponse()->getContent(); $response = json_decode($responseJson, true); $this->assertEquals("ok", $response['status']); $account = $this->configuration->getAccountBy('accountId', 'test'); $profiles = $account->getProfiles(); $this->assertNotEmpty($profiles); $this->assertCount(1, $profiles); /** @var Profile $profile */ $profile = $profiles[0]; $this->assertEquals('987654321', $profile->getGoogleId()); $this->assertEquals('testProfile', $profile->getName()); }
protected function initConfiguration() { $this->configuration->setStorageApi($this->storageApi); return $this->configuration; }
public function getInBucketId() { return $this->configuration->getInBucketId($this->accountId); }
public function oauthCallbackAction() { /** @var Session $session */ $session = $this->get('session'); $googleApi = $this->getGoogleApi(); $token = $session->get('token'); $accountId = $session->get('account'); $referrer = $session->get('referrer'); $session->clear(); $code = $this->get('request')->query->get('code'); if (empty($token) || empty($accountId)) { throw new UserException('Auth session expired'); } if (empty($code)) { throw new UserException('Could not read from Google API'); } try { $storageApi = new StorageApi(['token' => $token, 'userAgent' => 'ex-google-analytics']); $tokenData = $storageApi->verifyToken(); /** @var EncryptorInterface $encryptor */ $encryptor = $this->get('syrup.encryptor'); $configuration = new Configuration('ex-google-analytics', $encryptor); $configuration->setStorageApi($storageApi); $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_analytics_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL)); $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']); $userData = json_decode($googleApi->request('/oauth2/v2/userinfo')->getBody(), true); /** @var Account $account */ $account = $configuration->getAccountBy('accountId', $accountId); if (null == $account) { throw new ConfigurationException("Account doesn't exist"); } $userName = isset($userData['name']) ? $userData['name'] : $userData['displayName']; $userEmail = isset($userData['email']) ? $userData['email'] : $userData['emails'][0]['value']; $account->setGoogleId($userData['id'])->setGoogleName($userName)->setEmail($userEmail)->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->setOwner($tokenData['description']); if ($account->isExternal()) { if ($userEmail == $tokenData['description'] || !isset($tokenData['creatorToken'])) { // user generated an external link for himself or is reauthorizing himself into his config which was external before $account->setExternal(false); } else { $account->setOwner($tokenData['creatorToken']['description']); } } $account->save(); if ($referrer) { return new RedirectResponse($referrer); } else { return new JsonResponse(array('status' => 'ok')); } } catch (ClientException $e) { // SAPI exception - probably invalid access token throw new UserException($e->getMessage()); } catch (\Exception $e) { // any other exception throw new ApplicationException('Error finishing authorization: ' . $e->getMessage(), $e); } }