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