示例#1
0
 /**
  * @covers Loco\Utils\Swizzle\SwaggerClient::factory
  * @returns SwaggerClient
  */
 public function testFactory()
 {
     $base_url = 'https://localise.biz/api/docs';
     $client = SwaggerClient::factory(compact('base_url'));
     $this->assertEquals($base_url, $client->getBaseUrl(), 'base_url not passed to client');
     return $client;
 }
示例#2
0
 /**
  * Build from a live endpoint
  * @param string Swagger compliant JSON endpoint for resource listing
  * @throws \Exception
  * @return Swizzle
  */
 public function build($base_url)
 {
     $this->service = null;
     $client = SwaggerClient::factory(compact('base_url'));
     $this->debug('pulling resource listing from %s', $base_url);
     /* @var $listing ResourceListing */
     $listing = $client->getResources();
     // check this looks like a resource listing
     if (!$listing->isSwagger()) {
         throw new \Exception("This doesn't look like a Swagger spec");
     }
     if (!$listing->getApis()) {
         $this->logger->addAlert("Resource listing doesn't define any APIs");
     }
     // check swagger version
     if (self::SWAGGER_VERSION !== $listing->getSwaggerVersion()) {
         throw new \Exception('Unsupported Swagger version, Swizzle expects ' . self::SWAGGER_VERSION);
     }
     // Declared version overrides anything we've set
     if ($version = $listing->getApiVersion()) {
         $this->debug('+ set apiVersion %s', $version);
         $this->setApiVersion($version);
     }
     // Set description if missing from constructor
     if (!$this->init['description']) {
         $info = $listing->getInfo();
         $this->init['description'] = $info['description'] ?: $this->init['title'];
     }
     // no more configs allowed now, Guzzle service gets constructed
     $service = $this->getServiceDescription();
     // set base path from docs location if not provided
     if (!$service->getBaseUrl()) {
         $service->setBaseUrl(self::mergeUrl('/', $base_url));
     }
     // ready to pull each api declaration
     foreach ($listing->getApiPaths() as $path) {
         if ($this->delay) {
             usleep($this->delay);
         }
         // @todo do proper path resolution here, allowing a cross-domain spec.
         $path = trim($path, '/ ');
         $this->debug('pulling /%s ...', $path);
         $declaration = $client->getDeclaration(compact('path'));
         foreach ($declaration->getModels() as $model) {
             $this->addModel($model);
         }
         // Ensure a fully qualified base url for this api
         $baseUrl = self::mergeUrl($declaration->getBasePath(), $service->getBaseUrl());
         // add each api against required base url
         foreach ($declaration->getApis() as $api) {
             $this->addApi($api, $baseUrl);
         }
     }
     $this->debug('finished');
     return $this;
 }