Пример #1
0
 /**
  * This is the endpoint to build your API, and is a static method.
  * If your API is set to "public" or "open", you can instantiate your Api object just like this:
  * Api::get('http://idofyourrepository.prismic.io/api')
  *
  * @api
  *
  * @param  string $action the URL of your repository API's endpoint
  * @param  string $accessToken a permanent access token to use to access your content, for instance if your repository API is set to private
  * @param  HttpAdapterInterface $httpAdapter by default, the HTTP adapter uses CURL with a certain configuration, but you can override it here
  * @param  CacheInterface $cache Cache implementation
  * @throws \RuntimeException
  * @return Api                     the Api object, useable to perform queries
  */
 public static function get($action, $accessToken = null, HttpAdapterInterface $httpAdapter = null, CacheInterface $cache = null)
 {
     $cache = is_null($cache) ? self::defaultCache() : $cache;
     $cacheKey = $action . (is_null($accessToken) ? "" : "#" . $accessToken);
     $apiData = $cache->get($cacheKey);
     $api = $apiData ? new Api(unserialize($apiData), $accessToken, $httpAdapter, $cache) : null;
     if ($api) {
         return $api;
     } else {
         $url = $action . ($accessToken ? '?access_token=' . $accessToken : '');
         $httpAdapter = is_null($httpAdapter) ? self::defaultHttpAdapter() : $httpAdapter;
         $response = $httpAdapter->get($url);
         $response = json_decode($response->getBody(true));
         $experiments = isset($response->experiments) ? Experiments::parse($response->experiments) : new Experiments(array(), array());
         if (!$response) {
             throw new \RuntimeException('Unable to decode the json response');
         }
         $apiData = new ApiData(array_map(function ($ref) {
             return Ref::parse($ref);
         }, $response->refs), (array) $response->bookmarks, (array) $response->types, $response->tags, (array) $response->forms, $experiments, $response->oauth_initiate, $response->oauth_token);
         $api = new Api($apiData, $accessToken, $httpAdapter, $cache);
         $cache->set($cacheKey, serialize($apiData), 5);
         return $api;
     }
 }
Пример #2
0
 protected function setUp()
 {
     $experimentsJson = json_decode(file_get_contents(__DIR__ . '/../fixtures/experiments.json'));
     $this->experiments = Experiments::parse($experimentsJson);
 }