Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.7k
[Go] Add multiple servers support to Go(-experimental) client#4635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
0c919fef18203b21046fcc8fd34d4f40b89File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,7 +2,9 @@ | ||
| package {{packageName}} | ||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
| // contextKeys are used to identify the type of value in the context. | ||
| @@ -41,6 +43,20 @@ type APIKey struct { | ||
| Prefix string | ||
| } | ||
| // ServerVariable stores the information about a server variable | ||
| type ServerVariable struct { | ||
| Description string | ||
| DefaultValue string | ||
| EnumValues []string | ||
| } | ||
| // ServerConfiguration stores the information about a server | ||
| type ServerConfiguration struct { | ||
| Url string | ||
| Description string | ||
| Variables map[string]ServerVariable | ||
| } | ||
| // Configuration stores the configuration of the API client | ||
| type Configuration struct { | ||
| BasePath string `json:"basePath,omitempty"` | ||
| @@ -49,6 +65,7 @@ type Configuration struct { | ||
| DefaultHeader map[string]string `json:"defaultHeader,omitempty"` | ||
| UserAgent string `json:"userAgent,omitempty"` | ||
| Debug bool `json:"debug,omitempty"` | ||
| Servers []ServerConfiguration | ||
| HTTPClient *http.Client | ||
| } | ||
| @@ -59,6 +76,38 @@ func NewConfiguration() *Configuration { | ||
| DefaultHeader: make(map[string]string), | ||
| UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/go{{/httpUserAgent}}", | ||
| Debug: false, | ||
| {{#servers}} | ||
| {{#-first}} | ||
| Servers: []ServerConfiguration{{ | ||
| {{/-first}} | ||
| Url: "{{{url}}}", | ||
| Description: "{{{description}}}{{^description}}No description provided{{/description}}", | ||
| {{#variables}} | ||
| {{#-first}} | ||
| Variables: map[string]ServerVariable{ | ||
| {{/-first}} | ||
| "{{{name}}}": ServerVariable{ | ||
| Description: "{{{description}}}{{^description}}No description provided{{/description}}", | ||
| DefaultValue: "{{{defaultValue}}}", | ||
| {{#enumValues}} | ||
| {{#-first}} | ||
| EnumValues: []string{ | ||
| {{/-first}} | ||
| "{{{.}}}", | ||
| {{#-last}} | ||
| }, | ||
| {{/-last}} | ||
| {{/enumValues}} | ||
| }, | ||
| {{#-last}} | ||
| }, | ||
| {{/-last}} | ||
| {{/variables}} | ||
| }, | ||
| {{#-last}} | ||
| }, | ||
| {{/-last}} | ||
| {{/servers}} | ||
| } | ||
| return cfg | ||
| } | ||
| @@ -67,3 +116,31 @@ func NewConfiguration() *Configuration { | ||
| func (c *Configuration) AddDefaultHeader(key string, value string) { | ||
| c.DefaultHeader[key] = value | ||
| } | ||
| // ServerUrl returns URL based on server settings | ||
| func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { | ||
| if index < 0 || len(c.Servers) <= index { | ||
| return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1) | ||
| } | ||
| server := c.Servers[index] | ||
jirikuncar marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| url := server.Url | ||
| // go through variables and replace placeholders | ||
| for name, variable := range server.Variables { | ||
| if value, ok := variables[name]; ok { | ||
| found := bool(len(variable.EnumValues) == 0) | ||
| for _, enumValue := range variable.EnumValues { | ||
| if value == enumValue { | ||
| found = true | ||
| } | ||
| } | ||
| if !found { | ||
| return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) | ||
| } | ||
| url = strings.Replace(url, "{"+name+"}", value, -1) | ||
| } else { | ||
| url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) | ||
| } | ||
| } | ||
| return url, nil | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.