Skip to content

Commit

Permalink
[PS][Experimental] Add multiple server support (OpenAPITools#5741)
Browse files Browse the repository at this point in the history
* code comment

* add get host setting

* add multiple server support
  • Loading branch information
wing328 authored and michaelpro1 committed May 7, 2020
1 parent dad651f commit 622dfb4
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public PowerShellExperimentalClientCodegen() {
typeMapping.put("long", "Int64");
typeMapping.put("double", "Double");
typeMapping.put("number", "Decimal");
typeMapping.put("object", "System.Hashtable");
typeMapping.put("object", "System.Collections.Hashtable");
typeMapping.put("file", "System.IO.FileInfo");
typeMapping.put("ByteArray", "System.Byte[]");
typeMapping.put("binary", "System.IO.FileInfo");
Expand Down Expand Up @@ -374,8 +374,9 @@ public void processOpts() {
}

if (StringUtils.isNotBlank(powershellGalleryUrl)) {
// get the last segment of the URL
// e.g. https://www.powershellgallery.com/packages/PSTwitter => PSTwitter
additionalProperties.put("powershellGalleryId", powershellGalleryUrl.replaceFirst(".*/([^/?]+).*", "$1"));
//additionalProperties.put("powershellGalleryId", "something");
}

if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) {
Expand Down Expand Up @@ -569,7 +570,7 @@ public String getTypeDeclaration(Schema p) {
Schema inner = ap.getItems();
return getTypeDeclaration(inner) + "[]";
} else if (ModelUtils.isMapSchema(p)) {
return "Hashtable";
return "System.Collections.Hashtable";
} else if (!languageSpecificPrimitives.contains(getSchemaType(p))) {
return super.getTypeDeclaration(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,106 @@ function Set-{{{apiNamePrefix}}}ConfigurationApiKeyPrefix {
$Script:Configuration["ApiKeyPrefix"][$Id] = $ApiKeyPrefix
}
}

<#
.SYNOPSIS

Get the host setting.

.DESCRIPTION

Get the host setting in the form of array of hashtables.

.OUTPUTS

System.Collections.Hashtable[]
#>
function Get-{{apiNamePrefix}}HostSettings {
return @(
{{#servers}}
@{
"Url" = "{{{url}}}";
"Description" = "{{{description}}}{{^description}}No description provided{{/description}}";
{{#variables}}
{{#-first}}
"Variables" = @{
{{/-first}}
"{{{name}}}" = @{
"Description" = "{{{description}}}{{^description}}No description provided{{/description}}";
"DefaultValue" = "{{{defaultValue}}}";
{{#enumValues}}
{{#-first}}
"EnumValues" = @(
{{/-first}}
"{{{.}}}"{{^-last}},{{/-last}}
{{#-last}}
)
{{/-last}}
{{/enumValues}}
}{{^-last}};{{/-last}}
{{#-last}}
}
{{/-last}}
{{/variables}}
}{{^-last}},{{/-last}}
{{/servers}}
)

}

<#
.SYNOPSIS

Get the URL from the host settings.

.PARAMETER Index
Index of the host settings (array)

.PARAMETER Variables
Names and values of the variables (hashtable)

.DESCRIPTION

Get the URL from the host settings.

.OUTPUTS

String
#>
function Get-{{apiNamePrefix}}UrlFromHostSettings {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline = $true)]
[Int]$Index,
[Hashtable]$Variables = @{}
)

Process {
$Hosts = Get-{{apiNamePrefix}}HostSettings

# check array index out of bound
if ($Index -lt 0 -or $Index -gt $Hosts.Length) {
throw "Invalid index $index when selecting the host. Must be less than $($Hosts.Length)"
}

$Host = $Hosts[$Index];
$Url = $Host["Url"];

# go through variable and assign a value
foreach ($h in $Host["Variables"].GetEnumerator()) {
if ($Variables.containsKey($h.Name)) { # check to see if it's in the variables provided by the user
if ($h.Value["EnumValues"] -Contains $Variables[$h.Name]) {
$Url = $Url.replace("{$($h.Name)}", $Variables[$h.Name])
} else {
throw "The variable '$($h.Name)' in the host URL has invalid value $($Variables[$h.Name]). Must be $($h.Value["EnumValues"] -join ",")"
}
} else {
$Url = $Url.replace("{$($h.Name)}", $h.Value["DefaultValue"])
}
}
return $Url;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
openapi: 3.0.0
servers:
- url: 'http://petstore.swagger.io/v2'
- url: 'http://{server}.swagger.io:{port}/v2'
description: petstore server
variables:
server:
enum:
- 'petstore'
- 'qa-petstore'
- 'dev-petstore'
default: 'petstore'
port:
enum:
- 80
- 8080
default: 80
- url: https://localhost:8080/{version}
description: The local server
variables:
version:
enum:
- 'v1'
- 'v2'
default: 'v2'
info:
description: >-
This is a sample server Petstore server. For this sample, you can use the api key
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/powershell-experimental/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Invoker-Pester

## Documentation for API Endpoints

All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to *http://petstore.swagger.io:80/v2*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PSPetstore.PSPetstore/Api.PSPetApi

All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PSPetstore.PSPetstore/Api.PSStoreApi

All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
Expand Down Expand Up @@ -57,7 +57,7 @@ No authorization required

<a name="Get-PSInventory"></a>
# **Get-PSInventory**
> Hashtable Get-PSInventory<br>
> System.Collections.Hashtable Get-PSInventory<br>
Returns pet inventories by status

Expand All @@ -76,7 +76,7 @@ $Configuration["ApiKey"]["api_key"] = "YOUR_API_KEY"
# Returns pet inventories by status
try {
Hashtable $Result = Get-PSInventory
System.Collections.Hashtable $Result = Get-PSInventory
} catch {
Write-Host ($_.ErrorDetails | ConvertFrom-Json)
Write-Host ($_.Exception.Response.Headers | ConvertTo-Json)
Expand All @@ -88,7 +88,7 @@ This endpoint does not need any parameter.

### Return type

**Hashtable**
**System.Collections.Hashtable**

### Authorization

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PSPetstore.PSPetstore/Api.PSUserApi

All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ No description available.
.OUTPUTS
Hashtable
System.Collections.Hashtable
#>
function Get-PSInventory {
[CmdletBinding()]
Expand Down Expand Up @@ -114,7 +114,7 @@ function Get-PSInventory {
-QueryParameters $LocalVarQueryParameters `
-FormParameters $LocalVarFormParameters `
-CookieParameters $LocalVarCookieParameters `
-ReturnType "Hashtable"
-ReturnType "System.Collections.Hashtable"

return $LocalVarResult["Response"]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Get-PSConfiguration {
$Configuration = $Script:Configuration

if ([string]::IsNullOrEmpty($Configuration["BaseUrl"])) {
$Configuration["BaseUrl"] = "http://petstore.swagger.io/v2";
$Configuration["BaseUrl"] = "http://petstore.swagger.io:80/v2";
}

if (!$Configuration.containsKey("Username")) {
Expand Down Expand Up @@ -215,3 +215,116 @@ function Set-PSConfigurationApiKeyPrefix {
$Script:Configuration["ApiKeyPrefix"][$Id] = $ApiKeyPrefix
}
}

<#
.SYNOPSIS
Get the host setting.
.DESCRIPTION
Get the host setting in the form of array of hashtables.
.OUTPUTS
System.Collections.Hashtable[]
#>
function Get-PSHostSettings {
return @(
@{
"Url" = "http://{server}.swagger.io:{port}/v2";
"Description" = "petstore server";
"Variables" = @{
"server" = @{
"Description" = "No description provided";
"DefaultValue" = "petstore";
"EnumValues" = @(
"petstore",
"qa-petstore",
"dev-petstore"
)
};
"port" = @{
"Description" = "No description provided";
"DefaultValue" = "80";
"EnumValues" = @(
"80",
"8080"
)
}
}
},
@{
"Url" = "https://localhost:8080/{version}";
"Description" = "The local server";
"Variables" = @{
"version" = @{
"Description" = "No description provided";
"DefaultValue" = "v2";
"EnumValues" = @(
"v1",
"v2"
)
}
}
}
)

}

<#
.SYNOPSIS
Get the URL from the host settings.
.PARAMETER Index
Index of the host settings (array)
.PARAMETER Variables
Names and values of the variables (hashtable)
.DESCRIPTION
Get the URL from the host settings.
.OUTPUTS
String
#>
function Get-PSUrlFromHostSettings {

[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline = $true)]
[Int]$Index,
[Hashtable]$Variables = @{}
)

Process {
$Hosts = Get-PSHostSettings

# check array index out of bound
if ($Index -lt 0 -or $Index -gt $Hosts.Length) {
throw "Invalid index $index when selecting the host. Must be less than $($Hosts.Length)"
}

$Host = $Hosts[$Index];
$Url = $Host["Url"];

# go through variable and assign a value
foreach ($h in $Host["Variables"].GetEnumerator()) {
if ($Variables.containsKey($h.Name)) { # check to see if it's in the variables provided by the user
if ($h.Value["EnumValues"] -Contains $Variables[$h.Name]) {
$Url = $Url.replace("{$($h.Name)}", $Variables[$h.Name])
} else {
throw "The variable '$($h.Name)' in the host URL has invalid value $($Variables[$h.Name]). Must be $($h.Value["EnumValues"] -join ",")"
}
} else {
$Url = $Url.replace("{$($h.Name)}", $h.Value["DefaultValue"])
}
}

return $Url;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: OpenAPI Generator Team
#
# Generated on: 3/19/20
# Generated on: 3/29/20
#

@{
Expand Down Expand Up @@ -79,7 +79,8 @@ FunctionsToExport = 'Add-PSPet', 'Remove-Pet', 'Find-PSPetsByStatus', 'Find-PSPe
'Update-PSUser', 'New-PSApiResponse', 'New-PSCategory',
'New-PSInlineObject', 'New-PSInlineObject1', 'New-PSOrder', 'New-PSPet',
'New-PSTag', 'New-PSUser', 'Get-PSConfiguration', 'Set-PSConfiguration',
'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix'
'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix',
'Get-PSHostSettings', 'Get-PSUrlFromHostSettings'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
Loading

0 comments on commit 622dfb4

Please sign in to comment.