You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.1 KiB

2 years ago
  1. /*
  2. Seaweedfs Master Server API
  3. The Seaweedfs Master Server API allows you to store blobs
  4. API version: 3.43.0
  5. */
  6. // Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
  7. package openapi
  8. import (
  9. "context"
  10. "fmt"
  11. "net/http"
  12. "strings"
  13. )
  14. // contextKeys are used to identify the type of value in the context.
  15. // Since these are string, it is possible to get a short description of the
  16. // context key for logging and debugging using key.String().
  17. type contextKey string
  18. func (c contextKey) String() string {
  19. return "auth " + string(c)
  20. }
  21. var (
  22. // ContextServerIndex uses a server configuration from the index.
  23. ContextServerIndex = contextKey("serverIndex")
  24. // ContextOperationServerIndices uses a server configuration from the index mapping.
  25. ContextOperationServerIndices = contextKey("serverOperationIndices")
  26. // ContextServerVariables overrides a server configuration variables.
  27. ContextServerVariables = contextKey("serverVariables")
  28. // ContextOperationServerVariables overrides a server configuration variables using operation specific values.
  29. ContextOperationServerVariables = contextKey("serverOperationVariables")
  30. )
  31. // BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
  32. type BasicAuth struct {
  33. UserName string `json:"userName,omitempty"`
  34. Password string `json:"password,omitempty"`
  35. }
  36. // APIKey provides API key based authentication to a request passed via context using ContextAPIKey
  37. type APIKey struct {
  38. Key string
  39. Prefix string
  40. }
  41. // ServerVariable stores the information about a server variable
  42. type ServerVariable struct {
  43. Description string
  44. DefaultValue string
  45. EnumValues []string
  46. }
  47. // ServerConfiguration stores the information about a server
  48. type ServerConfiguration struct {
  49. URL string
  50. Description string
  51. Variables map[string]ServerVariable
  52. }
  53. // ServerConfigurations stores multiple ServerConfiguration items
  54. type ServerConfigurations []ServerConfiguration
  55. // Configuration stores the configuration of the API client
  56. type Configuration struct {
  57. Host string `json:"host,omitempty"`
  58. Scheme string `json:"scheme,omitempty"`
  59. DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
  60. UserAgent string `json:"userAgent,omitempty"`
  61. Debug bool `json:"debug,omitempty"`
  62. Servers ServerConfigurations
  63. OperationServers map[string]ServerConfigurations
  64. HTTPClient *http.Client
  65. }
  66. // NewConfiguration returns a new Configuration object
  67. func NewConfiguration() *Configuration {
  68. cfg := &Configuration{
  69. DefaultHeader: make(map[string]string),
  70. UserAgent: "OpenAPI-Generator/1.0.0/go",
  71. Debug: false,
  72. Servers: ServerConfigurations{
  73. {
  74. URL: "https://127.0.0.1:9333",
  75. Description: "No description provided",
  76. },
  77. },
  78. OperationServers: map[string]ServerConfigurations{},
  79. }
  80. return cfg
  81. }
  82. // AddDefaultHeader adds a new HTTP header to the default header in the request
  83. func (c *Configuration) AddDefaultHeader(key string, value string) {
  84. c.DefaultHeader[key] = value
  85. }
  86. // URL formats template on a index using given variables
  87. func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
  88. if index < 0 || len(sc) <= index {
  89. return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
  90. }
  91. server := sc[index]
  92. url := server.URL
  93. // go through variables and replace placeholders
  94. for name, variable := range server.Variables {
  95. if value, ok := variables[name]; ok {
  96. found := bool(len(variable.EnumValues) == 0)
  97. for _, enumValue := range variable.EnumValues {
  98. if value == enumValue {
  99. found = true
  100. }
  101. }
  102. if !found {
  103. return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
  104. }
  105. url = strings.Replace(url, "{"+name+"}", value, -1)
  106. } else {
  107. url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
  108. }
  109. }
  110. return url, nil
  111. }
  112. // ServerURL returns URL based on server settings
  113. func (c *Configuration) ServerURL(index int, variables map[string]string) (string, error) {
  114. return c.Servers.URL(index, variables)
  115. }
  116. func getServerIndex(ctx context.Context) (int, error) {
  117. si := ctx.Value(ContextServerIndex)
  118. if si != nil {
  119. if index, ok := si.(int); ok {
  120. return index, nil
  121. }
  122. return 0, reportError("Invalid type %T should be int", si)
  123. }
  124. return 0, nil
  125. }
  126. func getServerOperationIndex(ctx context.Context, endpoint string) (int, error) {
  127. osi := ctx.Value(ContextOperationServerIndices)
  128. if osi != nil {
  129. if operationIndices, ok := osi.(map[string]int); !ok {
  130. return 0, reportError("Invalid type %T should be map[string]int", osi)
  131. } else {
  132. index, ok := operationIndices[endpoint]
  133. if ok {
  134. return index, nil
  135. }
  136. }
  137. }
  138. return getServerIndex(ctx)
  139. }
  140. func getServerVariables(ctx context.Context) (map[string]string, error) {
  141. sv := ctx.Value(ContextServerVariables)
  142. if sv != nil {
  143. if variables, ok := sv.(map[string]string); ok {
  144. return variables, nil
  145. }
  146. return nil, reportError("ctx value of ContextServerVariables has invalid type %T should be map[string]string", sv)
  147. }
  148. return nil, nil
  149. }
  150. func getServerOperationVariables(ctx context.Context, endpoint string) (map[string]string, error) {
  151. osv := ctx.Value(ContextOperationServerVariables)
  152. if osv != nil {
  153. if operationVariables, ok := osv.(map[string]map[string]string); !ok {
  154. return nil, reportError("ctx value of ContextOperationServerVariables has invalid type %T should be map[string]map[string]string", osv)
  155. } else {
  156. variables, ok := operationVariables[endpoint]
  157. if ok {
  158. return variables, nil
  159. }
  160. }
  161. }
  162. return getServerVariables(ctx)
  163. }
  164. // ServerURLWithContext returns a new server URL given an endpoint
  165. func (c *Configuration) ServerURLWithContext(ctx context.Context, endpoint string) (string, error) {
  166. sc, ok := c.OperationServers[endpoint]
  167. if !ok {
  168. sc = c.Servers
  169. }
  170. if ctx == nil {
  171. return sc.URL(0, nil)
  172. }
  173. index, err := getServerOperationIndex(ctx, endpoint)
  174. if err != nil {
  175. return "", err
  176. }
  177. variables, err := getServerOperationVariables(ctx, endpoint)
  178. if err != nil {
  179. return "", err
  180. }
  181. return sc.URL(index, variables)
  182. }