Skip to content

Commit

Permalink
Add ClientBuilder Supporting File
Browse files Browse the repository at this point in the history
  • Loading branch information
ybelenko committed Feb 14, 2024
1 parent 742c2c4 commit 8dbb8de
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile(".php-cs-fixer.dist.php", "", ".php-cs-fixer.dist.php"));
supportingFiles.add(new SupportingFile(".phplint.mustache", "", ".phplint.yml"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("ClientBuilder.mustache", toSrcPath(invokerPackage, srcBasePath), "ClientBuilder.php"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
declare(strict_types=1);

/**
* ClientBuilder
*
* PHP version 8.1
*
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/

{{>partial_header}}
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

namespace {{invokerPackage}};

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use {{invokerPackage}}\Configuration;
use {{invokerPackage}}\HeaderSelector;
{{#apiInfo}}
{{#apis}}
use {{apiPackage}}\{{classFilename}};
{{/apis}}
{{/apiInfo}}

/**
* ClientBuilder Class Doc Comment
*
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
{{#apiInfo}}
{{#apis}}
* @property-read {{classFilename}} ${{classVarName}}
{{/apis}}
{{/apiInfo}}
*/
class ClientBuilder
{
/**
* @var ClientInterface
*/
protected ClientInterface $client;
/**
* @var Configuration
*/
protected Configuration $config;
/**
* @var HeaderSelector
*/
protected HeaderSelector $headerSelector;
/**
* @var int Host index
*/
protected int $hostIndex;
/**
* @param ClientInterface|null $client
* @param Configuration|null $config
* @param HeaderSelector|null $selector
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $hostIndex;
}

/**
* Initiates new client builder with OAuth access token.
* @link https://docs.salla.dev/docs/merchant/2fa0d7aca2def-authorization
*
* @param string $accessToken
* @throws \InvalidArgumentException
* @return ClientBuilder
*/
public static function createWithAccessToken(
string $accessToken
): static {
if (empty($accessToken)) {
throw new \InvalidArgumentException('Access token cannot be empty');
}

$config = new Configuration();
$config->setAccessToken($accessToken);

return new static(null, $config);
}

/**
* {{=<% %>=}}{@inheritDoc}<%={{ }}=%>
*/
public function __get(string $name)
{
switch ($name) {
{{#apiInfo}}
{{#apis}}
case '{{classVarName}}':
return new {{classFilename}}($this->client, $this->config, $this->headerSelector, $this->hostIndex);
{{/apis}}
{{/apiInfo}}
default:
throw new \InvalidArgumentException(\sprintf('Unknown property "%s"', $name));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ src/Api/HeaderApi.php
src/Api/PathApi.php
src/Api/QueryApi.php
src/ApiException.php
src/ClientBuilder.php
src/Configuration.php
src/HeaderSelector.php
src/Model/Bird.php
Expand Down
140 changes: 140 additions & 0 deletions samples/client/echo_api/php-nextgen/src/ClientBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
declare(strict_types=1);

/**
* ClientBuilder
*
* PHP version 8.1
*
* @package OpenAPI\Client
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/

/**
* Echo Server API
*
* Echo Server API
*
* The version of the OpenAPI document: 0.1.0
* Contact: team@openapitools.org
* @generated Generated by: https://openapi-generator.tech
* OpenAPI Generator version: 7.3.0-SNAPSHOT
*/

/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

namespace OpenAPI\Client;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\HeaderSelector;
use OpenAPI\Client\Api\AuthApi;
use OpenAPI\Client\Api\BodyApi;
use OpenAPI\Client\Api\FormApi;
use OpenAPI\Client\Api\HeaderApi;
use OpenAPI\Client\Api\PathApi;
use OpenAPI\Client\Api\QueryApi;

/**
* ClientBuilder Class Doc Comment
*
* @package OpenAPI\Client
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
* @property-read AuthApi $auth
* @property-read BodyApi $body
* @property-read FormApi $form
* @property-read HeaderApi $header
* @property-read PathApi $path
* @property-read QueryApi $query
*/
class ClientBuilder
{
/**
* @var ClientInterface
*/
protected ClientInterface $client;

/**
* @var Configuration
*/
protected Configuration $config;

/**
* @var HeaderSelector
*/
protected HeaderSelector $headerSelector;

/**
* @var int Host index
*/
protected int $hostIndex;

/**
* @param ClientInterface|null $client
* @param Configuration|null $config
* @param HeaderSelector|null $selector
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null,
int $hostIndex = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $hostIndex;
}

/**
* Initiates new client builder with OAuth access token.
* @link https://docs.salla.dev/docs/merchant/2fa0d7aca2def-authorization
*
* @param string $accessToken
* @throws \InvalidArgumentException
* @return ClientBuilder
*/
public static function createWithAccessToken(
string $accessToken
): static {
if (empty($accessToken)) {
throw new \InvalidArgumentException('Access token cannot be empty');
}

$config = new Configuration();
$config->setAccessToken($accessToken);

return new static(null, $config);
}

/**
* {@inheritDoc}
*/
public function __get(string $name)
{
switch ($name) {
case 'auth':
return new AuthApi($this->client, $this->config, $this->headerSelector, $this->hostIndex);
case 'body':
return new BodyApi($this->client, $this->config, $this->headerSelector, $this->hostIndex);
case 'form':
return new FormApi($this->client, $this->config, $this->headerSelector, $this->hostIndex);
case 'header':
return new HeaderApi($this->client, $this->config, $this->headerSelector, $this->hostIndex);
case 'path':
return new PathApi($this->client, $this->config, $this->headerSelector, $this->hostIndex);
case 'query':
return new QueryApi($this->client, $this->config, $this->headerSelector, $this->hostIndex);
default:
throw new \InvalidArgumentException(\sprintf('Unknown property "%s"', $name));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ src/Api/PetApi.php
src/Api/StoreApi.php
src/Api/UserApi.php
src/ApiException.php
src/ClientBuilder.php
src/Configuration.php
src/HeaderSelector.php
src/Model/AdditionalPropertiesClass.php
Expand Down
Loading

0 comments on commit 8dbb8de

Please sign in to comment.