Skip to content

Instantly share code, notes, and snippets.

@jtreminio
Created November 24, 2021 15:23
Show Gist options
  • Save jtreminio/39e4e0b87e3d8d7c9bfa32b894e27e81 to your computer and use it in GitHub Desktop.
Save jtreminio/39e4e0b87e3d8d7c9bfa32b894e27e81 to your computer and use it in GitHub Desktop.
openapi-generator creates a PHP SDK where the OAuth typehints in the `OpenAPI\Client\Api\ApiAppApi::apiAppCreate()` method are not namespaced
<?php
/**
* ApiAppApi
* PHP version 7.3
*
* @category Class
* @package OpenAPI\Client
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
/**
* My API
*
* v3 API
*
* The version of the OpenAPI document: 3.0.0
* Contact: apisupport@example.com
* Generated by: https://openapi-generator.tech
* OpenAPI Generator version: 5.3.0
*/
/**
* 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\Api;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use OpenAPI\Client\ApiException;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\HeaderSelector;
use OpenAPI\Client\ObjectSerializer;
/**
* ApiAppApi Class Doc Comment
*
* @category Class
* @package OpenAPI\Client
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class ApiAppApi
{
/**
* @var ClientInterface
*/
protected $client;
/**
* @var Configuration
*/
protected $config;
/**
* @var HeaderSelector
*/
protected $headerSelector;
/**
* @var int Host index
*/
protected $hostIndex;
/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $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,
$hostIndex = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $hostIndex;
}
/**
* Set the host index
*
* @param int $hostIndex Host index (required)
*/
public function setHostIndex($hostIndex): void
{
$this->hostIndex = $hostIndex;
}
/**
* Get the host index
*
* @return int Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}
/**
* @return Configuration
*/
public function getConfig()
{
return $this->config;
}
/**
* Operation apiAppCreate
*
* Creates a new API App.
*
* @param string[] $domains The domain names the ApiApp will be associated with. (required)
* @param string $name The name you want to assign to the ApiApp. (required)
* @param string $callback_url The URL at which the ApiApp should receive event callbacks. (optional)
* @param \SplFileObject $custom_logo_file An image file to use as a custom logo in embedded contexts. (Only applies to some API plans) (optional)
* @param OAuth $oauth oauth (optional)
*
* @throws \OpenAPI\Client\ApiException on non-2xx response
* @throws \InvalidArgumentException
* @return mixed
*/
public function apiAppCreate($domains, $name, $callback_url = null, $custom_logo_file = null, $oauth = null)
{
list($response) = $this->apiAppCreateWithHttpInfo($domains, $name, $callback_url, $custom_logo_file, $oauth);
return $response;
}
/**
* Operation apiAppCreateWithHttpInfo
*
* Creates a new API App.
*
* @param string[] $domains The domain names the ApiApp will be associated with. (required)
* @param string $name The name you want to assign to the ApiApp. (required)
* @param string $callback_url The URL at which the ApiApp should receive event callbacks. (optional)
* @param \SplFileObject $custom_logo_file An image file to use as a custom logo in embedded contexts. (Only applies to some API plans) (optional)
* @param OAuth $oauth (optional)
*
* @throws \OpenAPI\Client\ApiException on non-2xx response
* @throws \InvalidArgumentException
* @return array of mixed, HTTP status code, HTTP response headers (array of strings)
*/
public function apiAppCreateWithHttpInfo($domains, $name, $callback_url = null, $custom_logo_file = null, $oauth = null)
{
$request = $this->apiAppCreateRequest($domains, $name, $callback_url, $custom_logo_file, $oauth);
try {
$options = $this->createHttpClientOption();
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
);
} catch (ConnectException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
null,
null
);
}
$statusCode = $response->getStatusCode();
if ($statusCode < 200 || $statusCode > 299) {
throw new ApiException(
sprintf(
'[%d] Error connecting to the API (%s)',
$statusCode,
(string) $request->getUri()
),
$statusCode,
$response->getHeaders(),
(string) $response->getBody()
);
}
switch($statusCode) {
case 200:
if ('mixed' === '\SplFileObject') {
$content = $response->getBody(); //stream goes to serializer
} else {
$content = (string) $response->getBody();
}
return [
ObjectSerializer::deserialize($content, 'mixed', []),
$response->getStatusCode(),
$response->getHeaders()
];
}
$returnType = 'mixed';
if ($returnType === '\SplFileObject') {
$content = $response->getBody(); //stream goes to serializer
} else {
$content = (string) $response->getBody();
}
return [
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = ObjectSerializer::deserialize(
$e->getResponseBody(),
'mixed',
$e->getResponseHeaders()
);
$e->setResponseObject($data);
break;
}
throw $e;
}
}
/**
* Operation apiAppCreateAsync
*
* Creates a new API App.
*
* @param string[] $domains The domain names the ApiApp will be associated with. (required)
* @param string $name The name you want to assign to the ApiApp. (required)
* @param string $callback_url The URL at which the ApiApp should receive event callbacks. (optional)
* @param \SplFileObject $custom_logo_file An image file to use as a custom logo in embedded contexts. (Only applies to some API plans) (optional)
* @param OAuth $oauth (optional)
*
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function apiAppCreateAsync($domains, $name, $callback_url = null, $custom_logo_file = null, $oauth = null)
{
return $this->apiAppCreateAsyncWithHttpInfo($domains, $name, $callback_url, $custom_logo_file, $oauth)
->then(
function ($response) {
return $response[0];
}
);
}
/**
* Operation apiAppCreateAsyncWithHttpInfo
*
* Creates a new API App.
*
* @param string[] $domains The domain names the ApiApp will be associated with. (required)
* @param string $name The name you want to assign to the ApiApp. (required)
* @param string $callback_url The URL at which the ApiApp should receive event callbacks. (optional)
* @param \SplFileObject $custom_logo_file An image file to use as a custom logo in embedded contexts. (Only applies to some API plans) (optional)
* @param OAuth $oauth (optional)
*
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function apiAppCreateAsyncWithHttpInfo($domains, $name, $callback_url = null, $custom_logo_file = null, $oauth = null)
{
$returnType = 'mixed';
$request = $this->apiAppCreateRequest($domains, $name, $callback_url, $custom_logo_file, $oauth);
return $this->client
->sendAsync($request, $this->createHttpClientOption())
->then(
function ($response) use ($returnType) {
if ($returnType === '\SplFileObject') {
$content = $response->getBody(); //stream goes to serializer
} else {
$content = (string) $response->getBody();
}
return [
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
},
function ($exception) {
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
'[%d] Error connecting to the API (%s)',
$statusCode,
$exception->getRequest()->getUri()
),
$statusCode,
$response->getHeaders(),
(string) $response->getBody()
);
}
);
}
/**
* Create request for operation 'apiAppCreate'
*
* @param string[] $domains The domain names the ApiApp will be associated with. (required)
* @param string $name The name you want to assign to the ApiApp. (required)
* @param string $callback_url The URL at which the ApiApp should receive event callbacks. (optional)
* @param \SplFileObject $custom_logo_file An image file to use as a custom logo in embedded contexts. (Only applies to some API plans) (optional)
* @param OAuth $oauth (optional)
*
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Psr7\Request
*/
public function apiAppCreateRequest($domains, $name, $callback_url = null, $custom_logo_file = null, $oauth = null)
{
// verify the required parameter 'domains' is set
if ($domains === null || (is_array($domains) && count($domains) === 0)) {
throw new \InvalidArgumentException(
'Missing the required parameter $domains when calling apiAppCreate'
);
}
if (count($domains) > 2) {
throw new \InvalidArgumentException('invalid value for "$domains" when calling ApiAppApi.apiAppCreate, number of items must be less than or equal to 2.');
}
if (count($domains) < 1) {
throw new \InvalidArgumentException('invalid value for "$domains" when calling ApiAppApi.apiAppCreate, number of items must be greater than or equal to 1.');
}
// verify the required parameter 'name' is set
if ($name === null || (is_array($name) && count($name) === 0)) {
throw new \InvalidArgumentException(
'Missing the required parameter $name when calling apiAppCreate'
);
}
$resourcePath = '/api_app';
$formParams = [];
$queryParams = [];
$headerParams = [];
$httpBody = '';
$multipart = false;
// form params
if ($callback_url !== null) {
$formParams['callback_url'] = ObjectSerializer::toFormValue($callback_url);
}
// form params
if ($custom_logo_file !== null) {
$multipart = true;
$formParams['custom_logo_file'] = [];
$paramFiles = is_array($custom_logo_file) ? $custom_logo_file : [$custom_logo_file];
foreach ($paramFiles as $paramFile) {
$formParams['custom_logo_file'][] = \GuzzleHttp\Psr7\Utils::tryFopen(
ObjectSerializer::toFormValue($paramFile),
'rb'
);
}
}
// form params
if ($domains !== null) {
$formParams['domains'] = ObjectSerializer::toFormValue($domains);
}
// form params
if ($name !== null) {
$formParams['name'] = ObjectSerializer::toFormValue($name);
}
// form params
if ($oauth !== null) {
$formParams['oauth'] = ObjectSerializer::toFormValue($oauth);
}
if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
['multipart/form-data']
);
}
// for model (json/xml)
if (count($formParams) > 0) {
if ($multipart) {
$multipartContents = [];
foreach ($formParams as $formParamName => $formParamValue) {
$formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
foreach ($formParamValueItems as $formParamValueItem) {
$multipartContents[] = [
'name' => $formParamName,
'contents' => $formParamValueItem
];
}
}
// for HTTP post (form)
$httpBody = new MultipartStream($multipartContents);
} elseif ($headers['Content-Type'] === 'application/json') {
$httpBody = \GuzzleHttp\json_encode($formParams);
} else {
// for HTTP post (form)
$httpBody = \GuzzleHttp\Psr7\Query::build($formParams);
}
}
$defaultHeaders = [];
if ($this->config->getUserAgent()) {
$defaultHeaders['User-Agent'] = $this->config->getUserAgent();
}
$headers = array_merge(
$defaultHeaders,
$headerParams,
$headers
);
$query = \GuzzleHttp\Psr7\Query::build($queryParams);
return new Request(
'POST',
$this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''),
$headers,
$httpBody
);
}
/**
* Create http client option
*
* @throws \RuntimeException on file opening failure
* @return array of http client options
*/
protected function createHttpClientOption()
{
$options = [];
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
}
}
return $options;
}
}
openapi: 3.0.3
info:
title: 'My API'
description: 'v3 API'
termsOfService: 'https://www.example.com/terms'
contact:
email: apisupport@example.com
license:
name: MIT
url: 'https://opensource.org/licenses/MIT'
version: 3.0.0
servers:
-
url: 'https://api.example.com/v3'
paths:
/api_app:
post:
tags:
- 'Api App'
summary: 'Creates a new API App.'
description: 'Creates a new API App.'
operationId: apiAppCreate
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/ApiAppCreateRequest'
responses:
'200':
description: 'successful operation'
content:
application/json:
schema: {}
components:
schemas:
ApiAppCreateRequest:
required:
- name
- domains
properties:
callback_url:
description: 'The URL at which the ApiApp should receive event callbacks.'
type: string
custom_logo_file:
description: 'An image file to use as a custom logo in embedded contexts. (Only applies to some API plans)'
type: string
format: binary
domains:
description: 'The domain names the ApiApp will be associated with.'
type: array
items:
type: string
maxItems: 2
minItems: 1
example: ["example.com"]
name:
description: 'The name you want to assign to the ApiApp.'
type: string
example: 'My Production App'
oauth:
allOf:
- $ref: '#/components/schemas/OAuth'
-
properties:
callback_url:
example: 'https://example.com/oauth'
scopes:
example:
- basic_account_info
- request_signature
type: object
OAuth:
title: OAuth
type: object
properties:
callback_url:
nullable: true
description: "The callback URL to be used for OAuth flows.\n(Required if oauth[scopes] is provided)"
type: string
scopes:
description: "A list of OAuth scopes to be granted to the app.\n(Required if oauth[callback_url] is provided)."
type: array
items:
type: string
tags:
-
name: 'Api App'
description: 'Contains information about an API App'
@jtreminio
Copy link
Author

Specifically this bit:

    /**
     * Operation apiAppCreate
     *
     * Creates a new API App.
     *
     * @param  string[] $domains The domain names the ApiApp will be associated with. (required)
     * @param  string $name The name you want to assign to the ApiApp. (required)
     * @param  string $callback_url The URL at which the ApiApp should receive event callbacks. (optional)
     * @param  \SplFileObject $custom_logo_file An image file to use as a custom logo in embedded contexts. (Only applies to some API plans) (optional)
     * @param  OAuth $oauth oauth (optional)
     *
     * @throws \OpenAPI\Client\ApiException on non-2xx response
     * @throws \InvalidArgumentException
     * @return mixed
     */
    public function apiAppCreate($domains, $name, $callback_url = null, $custom_logo_file = null, $oauth = null)

The OAuth $oauth oauth (optional) should be namespaced. The class itself is correctly generated and is namespaced, but this typehint doesn't use namespacing when referencing it.

Here's command I use:

openapi-generator generate \
  -i "openapi.yaml" \
  --global-property "skipFormModel=false" \
  -g php \
  -o openapi-sdks/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment