Exemplo n.º 1
0
 public function render()
 {
     $handlers = $this->apiDecider->getHandlers();
     $this->getTemplate()->add('handlers', $this->sortHandlers($handlers));
     $this->getTemplate()->setFile(__DIR__ . '/api_listing.latte');
     $this->getTemplate()->render();
 }
Exemplo n.º 2
0
 /**
  * Create handler list for specified version
  *
  * @param integer $version
  *
  * @return array
  */
 private function getHandlersList($version)
 {
     $versionHandlers = array_filter($this->apiDecider->getHandlers(), function ($handler) use($version) {
         return $version == $handler['endpoint']->getVersion();
     });
     return array_map(function ($handler) {
         return ['method' => $handler['endpoint']->getMethod(), 'version' => $handler['endpoint']->getVersion(), 'package' => $handler['endpoint']->getPackage(), 'api_action' => $handler['endpoint']->getApiAction(), 'authorization' => get_class($handler['authorization']), 'url' => $this->apiLink->link($handler['endpoint']), 'params' => $this->createParamsList($handler['handler'])];
     }, $versionHandlers);
 }
Exemplo n.º 3
0
 public function testWithParams()
 {
     $apiDecider = new ApiDecider();
     $apiDecider->addApiHandler(new EndpointIdentifier('GET', 1, 'test', 'api'), new EchoHandler(), new NoAuthorization());
     $presenter = new ApiPresenter();
     $presenter->apiDecider = $apiDecider;
     $presenter->injectPrimary(new Container(), null, null, new HttpRequest(new UrlScript('')), new HttpResponse());
     $request = new Request('Api:Api:default', 'GET', ['version' => 1, 'package' => 'test', 'apiAction' => 'api']);
     $result = $presenter->run($request);
     $this->assertEquals(['status' => 'error', 'message' => 'wrong input'], $result->getPayload());
     $this->assertEquals('application/json', $result->getContentType());
 }
Exemplo n.º 4
0
 public function testHandlerWithParam()
 {
     $linkGenerator = new LinkGenerator(new SimpleRouter([]), new Url('http://test/'));
     $apiLink = new ApiLink($linkGenerator);
     $apiDecider = new ApiDecider($apiLink);
     $apiDecider->addApiHandler(new EndpointIdentifier('POST', 1, 'comments', 'list'), new EchoHandler(), new NoAuthorization());
     $apiDecider->addApiHandler(new EndpointIdentifier('GET', 1, 'endpoints'), new ApiListingHandler($apiDecider, $apiLink), new NoAuthorization());
     $result = $apiDecider->getApiHandler('GET', 1, 'endpoints');
     $handler = $result['handler'];
     $response = $handler->handle([]);
     $this->assertEquals(200, $response->getCode());
     $payload = $response->getPayload();
     $this->assertEquals(2, count($payload['endpoints']));
     $this->assertEquals(2, count($payload['endpoints'][0]['params']));
 }
Exemplo n.º 5
0
 public function testGlobalPreflight()
 {
     $linkGenerator = new LinkGenerator(new SimpleRouter([]), new Url('http://test/'));
     $apiLink = new ApiLink($linkGenerator);
     $apiDecider = new ApiDecider($apiLink);
     $apiDecider->enableGlobalPreflight();
     $this->assertEquals(0, count($apiDecider->getHandlers()));
     $apiDecider->addApiHandler(new EndpointIdentifier('POST', 2, 'comments', 'list'), new AlwaysOkHandler(), new NoAuthorization());
     $this->assertEquals(1, count($apiDecider->getHandlers()));
     $handler = $apiDecider->getApiHandler('OPTIONS', 2, 'comments', 'list');
     $this->assertInstanceOf('Tomaj\\NetteApi\\Handlers\\CorsPreflightHandler', $handler['handler']);
 }
Exemplo n.º 6
0
 /**
  * Get handler information triplet (endpoint, handler, authorization)
  *
  * @return array
  */
 private function getHandler()
 {
     return $this->apiDecider->getApiHandler($this->getRequest()->getMethod(), $this->params['version'], $this->params['package'], $this->params['apiAction']);
 }