protected function setUp() { $cache = new ApcCache(); $cache->clear(); $search = json_decode(file_get_contents(__DIR__ . '/../fixtures/search.json')); $this->document = Document::parse($search[0]); $this->micro_api = Api::get(self::$testRepository, null, null, $cache); $this->linkResolver = new FakeLinkResolver(); }
public function get_api() { $url = $this->app->config('prismic.url'); $token = $this->app->config('prismic.token'); if ($this->api == null) { $this->api = Api::get($url, $token); } return $this->api; }
public function testFileLinksWork() { $api = Api::get(self::$testRepository); $doc = $api->getByID("UssvNAEAAPvPpbr0"); $linkExpected = 'https://prismic-io.s3.amazonaws.com/frontwebconf%2F48db2b33-5fd4-4cc5-809d-5ca76342beb4_become+a+sponsor+%28frontwebconf%29.pdf'; $htmlExpected = '<a href="https://prismic-io.s3.amazonaws.com/frontwebconf%2F48db2b33-5fd4-4cc5-809d-5ca76342beb4_become+a+sponsor+%28frontwebconf%29.pdf">Become a sponsor (FrontWebConf).pdf</a>'; $this->assertEquals($doc->get('footerlinks.link')->getUrl(), $linkExpected); $this->assertEquals($doc->get('footerlinks.link')->asText(), $linkExpected); $this->assertEquals($doc->get('footerlinks.link')->asHtml(), $htmlExpected); }
public function testFileLinksWork() { $api = Api::get(self::$testRepository); $masterRef = $api->master()->getRef(); $results = $api->forms()->everything->query('[[:d = at(document.id, "UssvNAEAAPvPpbr0")]]')->ref($masterRef)->submit()->getResults(); $linkExpected = 'https://prismic-io.s3.amazonaws.com/frontwebconf%2F48db2b33-5fd4-4cc5-809d-5ca76342beb4_become+a+sponsor+%28frontwebconf%29.pdf'; $htmlExpected = '<a href="https://prismic-io.s3.amazonaws.com/frontwebconf%2F48db2b33-5fd4-4cc5-809d-5ca76342beb4_become+a+sponsor+%28frontwebconf%29.pdf">Become a sponsor (FrontWebConf).pdf</a>'; $this->assertEquals($results[0]->get('footerlinks.link')->getUrl(), $linkExpected); $this->assertEquals($results[0]->get('footerlinks.link')->asText(), $linkExpected); $this->assertEquals($results[0]->get('footerlinks.link')->asHtml(), $htmlExpected); }
/** * Return Prismic Api Client * @param ServiceLocatorInterface $serviceLocator * @return Api * @throws \RuntimeException */ public function createService(ServiceLocatorInterface $serviceLocator) { $config = $serviceLocator->get('Config'); if (!isset($config['prismic']['api'])) { throw new \RuntimeException('No configuration has been provided in order to retrieve a Prismic API Client'); } $config = $config['prismic']; $httpClient = $cache = null; $url = $config['api']; $token = $configToken = isset($config['token']) ? $config['token'] : null; /** * Check the Session for a token and prefer that if it exists. * We cannot retrieve the Prismic Container from the service manager * because it depends on the Context, so we'd have a circular dependency */ $session = new PrismicContainer('Prismic'); if ($session->hasAccessToken()) { $token = $session->getAccessToken(); } /** * See if a cache service name has been provided and wrap it in the facade * if required */ if (!empty($config['cache'])) { $storage = $serviceLocator->get($config['cache']); if (!$storage instanceof CacheInterface) { $cache = new Facade($storage); } else { $cache = $storage; } } if (!empty($config['httpClient'])) { $httpClient = $serviceLocator->get($config['httpClient']); } /** * @see \Prismic\Api::get($apiUrl, $accesssToken, \Guzzle\Http\Client $client, \Prismic\Cache\CacheInterface $cache) */ /** * Wrap api initialisation in a try/catch in case the temporary token we got from the session * has expired */ try { $api = Api::get($url, $token, $httpClient, $cache); } catch (HttpAdapterException $e) { $api = Api::get($url, $configToken, $httpClient, $cache); } return $api; }
public function get_conf() { $apiEndpoint = $this->app->config('prismic.url'); $apiConfEndpoint = mb_substr($apiEndpoint, 0, count($apiEndpoint) - 4) . 'conf'; $token = $this->app->config('prismic.token'); if ($token && is_null($this->conf)) { try { $httpClient = \Prismic\Api::defaultHttpAdapter(); $queryString = http_build_query(array("access_token" => $token)); $response = $httpClient->get($apiConfEndpoint . '?' . $queryString); $this->conf = json_decode($response->getBody(), true); } catch (\Ivory\HttpAdapter\HttpAdapterException $e) { } } return $this->conf; }
/** * {@inheritDoc} */ public function query(Query $query) { $this->assertLoggedIn(); if (!$query instanceof QueryObjectModelInterface) { $parser = new Sql2ToQomQueryConverter($this->factory->get('Query\\QOM\\QueryObjectModelFactory')); try { $qom = $parser->parse($query->getStatement()); $qom->setLimit($query->getLimit()); $qom->setOffset($query->getOffset()); } catch (\Exception $e) { throw new InvalidQueryException('Invalid query: ' . $query->getStatement(), null, $e); } } else { $qom = $query; } $qomWalker = new QOMWalker($this->nodeTypeManager, $this->api, $this->getNamespaces()); list($selectors, $selectorAliases, $query) = $qomWalker->walkQOMQuery($qom); $primarySource = reset($selectors); $primaryType = $primarySource->getSelectorName() ?: $primarySource->getNodeTypeName(); $data = $this->api->forms()->everything->ref($this->ref)->query($query)->submit(); // TODO implement $results = array(); return $results; }
} } else { $app->response->setStatus(400); } }); $app->get('/disqus/posts/list', function () use($app, $prismic) { $cursor = $app->request->get('cursor'); $limit = $app->request->get('limit'); $threadIds = $app->request->get('thread'); $order = $app->request->get('order'); $apiKey = $prismic->config('disqus.apikey'); $apiSecret = $prismic->config('disqus.apisecret'); $accessToken = $prismic->config('disqus.accesstoken'); $forum = $prismic->config('disqus.forum'); if ($apiKey && $apiSecret && $accessToken && $forum) { $httpClient = \Prismic\Api::defaultHttpAdapter(); $param = array('api_key' => $apiKey, 'api_secret' => $apiSecret, 'access_token' => $accessToken, 'cursor' => $cursor, 'limit' => $limit, 'thread' => $threadIds, 'order' => $order, 'forum' => $forum); $queryString = http_build_query($param); $url = 'https://disqus.com/api/3.0/posts/list.json?' . $queryString; $url = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $url); $app->response->headers->set('Content-Type', 'application/json'); try { $response = $httpClient->get($url); $json = json_decode($response->getBody()); $app->response->setStatus($response->getStatusCode()); $app->response->setBody(json_encode($json)); } catch (\Ivory\HttpAdapter\HttpAdapterException $e) { $app->response->setStatus($e->getResponse()->getStatusCode()); } } });
public function testCache() { // startgist:56e7aa7b1be64e09f76f:prismic-cache.php // You can pass any class implementing the CacheInterface to the Api creation // http://prismicio.github.io/php-kit/classes/Prismic.Cache.CacheInterface.html $fileCache = new ApcCache(); $api = Api::get("https://lesbonneschoses.cdn.prismic.io/api", null, null, $fileCache); // endgist $this->assertNotNull($api); }
<?php include_once __DIR__ . '/../vendor/autoload.php'; use Prismic\Api; $options = array('prismic.ref' => false, 'prismic.api' => "https://lesbonneschoses.prismic.io/api", 'prismic.token' => "Your permanent token"); // retrieve the main information from the api, form and repositories ... $api = Api::get($options['prismic.api'], $options['prismic.token']); // retrieve the main repository reference $ref = $options['prismic.ref'] ?: $api->master()->getRef(); $searchForm = $api->forms()->everything->ref($ref); // create a new search form with the search query $queryForm = $searchForm->query('[[:d = at(document.type, "product")]]'); // retrieve the results from the API $results = $queryForm->submit();
public function testGetLink() { $api = Api::get('http://micro.prismic.io/api'); $masterRef = $api->master()->getRef(); $documents = $api->forms()->everything->ref($masterRef)->query('[[:d = at(document.id, "UvLDWgEAABoHHn1R")]]')->submit()->getResults(); $this->assertEquals($documents[0]->getLink('cta.link')->getId(), "U0w8OwEAACoAQEvB"); }
/** * Returns either the master ref, or the defined ref * * @param Api $api * * @return string */ private function getRef(Api $api) { if ($this->model->ref != null) { return $this->model->ref; } return $api->master()->getRef(); }
/** * @param string $customAccessToken * @return Api */ public function getApiHome($customAccessToken = null) { return Api::get($this->apiEndpoint, $customAccessToken ? $customAccessToken : $this->accessToken, $this->client, $this->cache); }
public static function apiHome($maybeAccessToken = null) { return Api::get(self::config('prismic.api'), $maybeAccessToken); }
/** * @param Api $api * * @depends testValidApiCall */ public function testForm(Api $api) { $forms = $api->forms(); $this->assertObjectHasAttribute('everything', $forms); // $forms->everything->submit(); }