Ejemplo n.º 1
0
 public function __construct(array $authorization, array $apiAuth, Builder $builder)
 {
     if (empty($authorization['oauth_api']['credentials'])) {
         throw new UserException("OAuth API credentials not supplied in config");
     }
     $oauthApiDetails = $authorization['oauth_api']['credentials'];
     foreach (['#data', 'appKey', '#appSecret'] as $key) {
         if (empty($oauthApiDetails[$key])) {
             throw new UserException("Missing '{$key}' for OAuth 2.0 authorization");
         }
     }
     if (!empty($apiAuth['format'])) {
         switch ($apiAuth['format']) {
             case 'json':
                 // authorization: { data: key }
                 $this->data = Utils::json_decode($oauthApiDetails['#data']);
                 break;
             default:
                 throw new UserException("Unknown OAuth data format '{$apiAuth['format']}'");
         }
     } else {
         // authorization: data
         $this->data = $oauthApiDetails['#data'];
     }
     // authorization: clientId
     $this->clientId = $oauthApiDetails['appKey'];
     // authorization: clientSecret
     $this->clientSecret = $oauthApiDetails['#appSecret'];
     $this->headers = empty($apiAuth['headers']) ? [] : $apiAuth['headers'];
     $this->query = empty($apiAuth['query']) ? [] : $apiAuth['query'];
     $this->builder = $builder;
 }
Ejemplo n.º 2
0
 public function __construct(array $authorization)
 {
     if (empty($authorization['oauth_api']['credentials'])) {
         throw new UserException("OAuth API credentials not supplied in config");
     }
     $oauthApiDetails = $authorization['oauth_api']['credentials'];
     foreach (['#data', 'appKey', '#appSecret'] as $key) {
         if (empty($oauthApiDetails[$key])) {
             throw new UserException("Missing '{$key}' for OAuth 1.0 authorization");
         }
     }
     $data = Utils::json_decode($oauthApiDetails['#data']);
     $this->token = $data->oauth_token;
     $this->tokenSecret = $data->oauth_token_secret;
     $this->consumerKey = $oauthApiDetails['appKey'];
     $this->consumerSecret = $oauthApiDetails['#appSecret'];
 }
Ejemplo n.º 3
0
 public function __construct($authorization, array $api)
 {
     if (empty($authorization['oauth_api']['credentials'])) {
         throw new UserException("OAuth API credentials not supplied in config");
     }
     $oauthApiDetails = $authorization['oauth_api']['credentials'];
     foreach (['#data', 'appKey', '#appSecret'] as $key) {
         if (empty($oauthApiDetails[$key])) {
             throw new UserException("Missing '{$key}' for OAuth 2.0 authorization");
         }
     }
     try {
         $oAuthData = Utils::json_decode($oauthApiDetails['#data'], true);
     } catch (JsonDecodeException $e) {
         throw new UserException("The OAuth data is not a valid JSON");
     }
     $consumerData = ['client_id' => $oauthApiDetails['appKey'], 'client_secret' => $oauthApiDetails['#appSecret']];
     $this->params = ['consumer' => $consumerData, 'user' => $oAuthData];
     $this->auth = $api['authentication'];
 }
Ejemplo n.º 4
0
 public function testNoStrictScalarChange()
 {
     $parser = $this->getParser();
     $data = Utils::json_decode('[
         {"field": 128},
         {"field": "string"},
         {"field": true}
     ]');
     $parser->process($data, 'threepack');
     self::assertEquals(['"field"' . PHP_EOL, '"128"' . PHP_EOL, '"string"' . PHP_EOL, '"1"' . PHP_EOL], file($parser->getCsvFiles()['threepack']->getPathname()));
 }
Ejemplo n.º 5
0
 protected function loadJson($fileName)
 {
     $testFilesPath = $this->getDataDir() . $fileName . ".json";
     $file = file_get_contents($testFilesPath);
     return Utils::json_decode($file);
 }
Ejemplo n.º 6
0
 /**
  * @param Response $response
  * @return array|object Should be anything that can result from json_decode
  * @throws ApplicationException
  * @throws UserException
  */
 protected function getObjectFromResponse(Response $response)
 {
     // Format the response
     switch ($this->responseFormat) {
         case self::JSON:
             // Sanitize the JSON
             $body = iconv($this->responseEncoding, 'UTF-8//IGNORE', $response->getBody());
             try {
                 $decoded = Utils::json_decode($body, false, 512, 0, true, true);
             } catch (JsonDecodeException $e) {
                 throw new UserException("Invalid JSON response from API: " . $e->getMessage(), 0, null, $e->getData());
             }
             return $decoded;
         case self::XML:
             try {
                 $xml = new \SimpleXMLElement($response->getBody());
             } catch (\Exception $e) {
                 throw new UserException("Error decoding the XML response: " . $e->getMessage(), 400, $e, ['body' => (string) $response->getBody()]);
             }
             // TODO must be a \stdClass
             return $xml;
         case self::RAW:
             // Or could this be a string?
             $object = new \stdClass();
             $object->body = (string) $response->getBody();
             return $object;
         default:
             throw new ApplicationException("Data format {$this->responseFormat} not supported.");
     }
 }
 /**
  * @expectedException \Keboola\Juicer\Exception\UserException
  * @expectedExceptionMessage User script error: date() expects at least 1 parameter, 0 given
  */
 public function testBuildParamsException()
 {
     $cfg = new JobConfig(1, ['params' => Utils::json_decode('{
             "filters": {
                 "function": "date"
             }
         }')]);
     $job = $this->getJob($cfg);
     $job->setAttributes(['das.attribute' => "something interesting"]);
     $job->setMetadata(['time' => ['previousStart' => 0, 'currentStart' => time()]]);
     $job->setBuilder(new Builder());
     self::callMethod($job, 'buildParams', [$cfg]);
 }