/** * Get API response. * * @param string $call * @param string $type * @param array $parameters */ public function json($call, $type, $parameters = []) { try { $content = $this->plain($call, $parameters); return $this->serializer->deserialize($content, $type, 'json'); } catch (\Exception $e) { throw new Exception\ApiException($e->getMessage(), $call); } }
/** * Iterates all configured api requests, fetches the result and tries to * deserialize the response to the mapped entities chosen for the * corresponding api request. * * Notes on your entities: * * your entities must implement VirtualIdentity\TwitterBundle\Interfaces\TwitterEntity * * the attributes must define the JMS\Serializer\Annotation\Type annotation * * the result from twitter is flattened before its deserialized * * What does the last point mean? This means that if your response has a key * $entry['id_str'] the property "idStr" is populated. If the response has a * key $entry['user']['screen_name'] the property "userScreenName" is * populated. Therefore you can persist whatever information you want to * persist from the direct response by using the correct name for the * entity-field. * * Hint (1): * If your fields don't get deserialized correctly, make use of the * JMS\Serializer\Annotation\SerializedName annotation. * * Hint (2): * If you have special code executed in your setters, you must use the * JMS\Serializer\Annotation\AccessType annotation! Per default reflection * is used and the properties are set directly! * * Warning: * You may not want that the id is populated by deserializing because you * want it to be an incremental value. In this case use a combination of * JMS\Serializer\Annotation\ExclusionPolicy and * JMS\Serializer\Annotation\Exclude or JMS\Serializer\Annotation\Expose * annotations. * * @param array requestIds which requestIds should be executed * @return void */ public function syncDatabase(array $requestIds = array()) { if (!$this->api) { throw new ApiException('Api not initialized! Use setAuthentication to implicitly initialize the api.'); } foreach ($this->apiRequests as $twitterRequestEntity) { if (count($requestIds) && !in_array($twitterRequestEntity->getId(), $requestIds)) { continue; } if (count($requestIds) == 0 && $twitterRequestEntity->getLastExecutionTime() + $twitterRequestEntity->getRefreshLifeTime() > time()) { // we only execute requests if their lifetime is over continue; } $socialEntityClass = $twitterRequestEntity->getMappedEntity(); if (!class_exists($socialEntityClass) && !in_array('VirtualIdentity\\TwitterBundle\\Interfaces\\TwitterEntityInterface', class_implements($socialEntityClass))) { continue; } $start = microtime(true); $url = $twitterRequestEntity->getUrl(); $params = array(); $query = parse_url($url, PHP_URL_QUERY); parse_str($query, $params); $lastMaxId = $twitterRequestEntity->getLastMaxId(); if ($twitterRequestEntity->getUseSinceId() && is_numeric($lastMaxId)) { $params['since_id'] = $lastMaxId; } // twitter limits the maximum amount of tweets that can be fetched // per api request to 200, so making this configurable for the user // seems to make no sense $params['count'] = 200; $status = $this->api->request('GET', $this->api->url($url), $params); if ($status == 200) { $response = json_decode($this->api->response['response'], true); if (isset($response['statuses'])) { $response = $response['statuses']; } $repository = $this->em->getRepository($socialEntityClass); foreach ($response as $rawTweet) { if (!isset($rawTweet['id_str'])) { throw new ApiException('Tweet could not be recognized! There was no id_str in the entity: ' . print_r($rawTweet, 1)); } if (!count($repository->findOneByIdStr($rawTweet['id_str']))) { $flattened = self::flatten($rawTweet); $rawData = json_encode($flattened); $twitterEntity = $this->serializer->deserialize($rawData, $socialEntityClass, 'json'); $twitterEntity->setRequestId($twitterRequestEntity->getId()); $twitterEntity->setRaw(json_encode($rawTweet)); $twitterEntity->setApproved($this->autoApprove); $this->em->persist($twitterEntity); if ($rawTweet['id_str'] > $lastMaxId) { $lastMaxId = $rawTweet['id_str']; } } else { continue; } } $this->em->flush(); } else { $twitterRequestEntity->setLastExecutionDuration(-1); $twitterRequestEntity->setLastMaxId($lastMaxId); $this->em->persist($twitterRequestEntity); $this->em->flush(); throw new ApiException('Request was unsuccessful! Status code: ' . $status . '. Response was: ' . $this->api->response['response']); } $timeTaken = microtime(true) - $start; $twitterRequestEntity->setLastExecutionDuration($timeTaken); $twitterRequestEntity->setLastMaxId($lastMaxId); $twitterRequestEntity->setLastExecutionTime(time()); $this->em->persist($twitterRequestEntity); $this->em->flush(); } }
/** * @param string $jsonString * * @return RedashApiClient\Response */ private function deserialize($jsonString) { return $this->serializer->deserialize($jsonString, Response::class, 'json'); }