github.com/khulnasoft/codebase@v0.0.0-20231214144635-a707781cbb24/go-bitbucket/configuration.go (about) 1 /* 2 * Bitbucket API 3 * 4 * Code against the Bitbucket API to automate simple tasks, embed Bitbucket data into your own site, build mobile or desktop apps, or even add custom UI add-ons into Bitbucket itself using the Connect framework. 5 * 6 * API version: 2.0 7 * Contact: support@bitbucket.org 8 */ 9 10 // Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT. 11 12 package bitbucket 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 // ContextOAuth2 takes an oauth2.TokenSource as authentication for the request. 33 ContextOAuth2 = contextKey("token") 34 35 // ContextBasicAuth takes BasicAuth as authentication for the request. 36 ContextBasicAuth = contextKey("basic") 37 38 // ContextAccessToken takes a string oauth2 access token as authentication for the request. 39 ContextAccessToken = contextKey("accesstoken") 40 41 // ContextAPIKeys takes a string apikey as authentication for the request 42 ContextAPIKeys = contextKey("apiKeys") 43 44 // ContextHttpSignatureAuth takes HttpSignatureAuth as authentication for the request. 45 ContextHttpSignatureAuth = contextKey("httpsignature") 46 47 // ContextServerIndex uses a server configuration from the index. 48 ContextServerIndex = contextKey("serverIndex") 49 50 // ContextOperationServerIndices uses a server configuration from the index mapping. 51 ContextOperationServerIndices = contextKey("serverOperationIndices") 52 53 // ContextServerVariables overrides a server configuration variables. 54 ContextServerVariables = contextKey("serverVariables") 55 56 // ContextOperationServerVariables overrides a server configuration variables using operation specific values. 57 ContextOperationServerVariables = contextKey("serverOperationVariables") 58 ) 59 60 // BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth 61 type BasicAuth struct { 62 UserName string `json:"userName,omitempty"` 63 Password string `json:"password,omitempty"` 64 } 65 66 // APIKey provides API key based authentication to a request passed via context using ContextAPIKey 67 type APIKey struct { 68 Key string 69 Prefix string 70 } 71 72 // ServerVariable stores the information about a server variable 73 type ServerVariable struct { 74 Description string 75 DefaultValue string 76 EnumValues []string 77 } 78 79 // ServerConfiguration stores the information about a server 80 type ServerConfiguration struct { 81 URL string 82 Description string 83 Variables map[string]ServerVariable 84 } 85 86 // ServerConfigurations stores multiple ServerConfiguration items 87 type ServerConfigurations []ServerConfiguration 88 89 // Configuration stores the configuration of the API client 90 type Configuration struct { 91 Host string `json:"host,omitempty"` 92 Scheme string `json:"scheme,omitempty"` 93 DefaultHeader map[string]string `json:"defaultHeader,omitempty"` 94 UserAgent string `json:"userAgent,omitempty"` 95 Debug bool `json:"debug,omitempty"` 96 Servers ServerConfigurations 97 OperationServers map[string]ServerConfigurations 98 HTTPClient *http.Client 99 } 100 101 // NewConfiguration returns a new Configuration object 102 func NewConfiguration() *Configuration { 103 cfg := &Configuration{ 104 DefaultHeader: make(map[string]string), 105 UserAgent: "OpenAPI-Generator/1.0.0/go", 106 Debug: false, 107 Servers: ServerConfigurations{ 108 { 109 URL: "https://api.bitbucket.org/2.0", 110 Description: "No description provided", 111 }, 112 }, 113 OperationServers: map[string]ServerConfigurations{}, 114 } 115 return cfg 116 } 117 118 // AddDefaultHeader adds a new HTTP header to the default header in the request 119 func (c *Configuration) AddDefaultHeader(key string, value string) { 120 c.DefaultHeader[key] = value 121 } 122 123 // URL formats template on a index using given variables 124 func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) { 125 if index < 0 || len(sc) <= index { 126 return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1) 127 } 128 server := sc[index] 129 url := server.URL 130 131 // go through variables and replace placeholders 132 for name, variable := range server.Variables { 133 if value, ok := variables[name]; ok { 134 found := bool(len(variable.EnumValues) == 0) 135 for _, enumValue := range variable.EnumValues { 136 if value == enumValue { 137 found = true 138 } 139 } 140 if !found { 141 return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) 142 } 143 url = strings.Replace(url, "{"+name+"}", value, -1) 144 } else { 145 url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) 146 } 147 } 148 return url, nil 149 } 150 151 // ServerURL returns URL based on server settings 152 func (c *Configuration) ServerURL(index int, variables map[string]string) (string, error) { 153 return c.Servers.URL(index, variables) 154 } 155 156 func getServerIndex(ctx context.Context) (int, error) { 157 si := ctx.Value(ContextServerIndex) 158 if si != nil { 159 if index, ok := si.(int); ok { 160 return index, nil 161 } 162 return 0, reportError("Invalid type %T should be int", si) 163 } 164 return 0, nil 165 } 166 167 func getServerOperationIndex(ctx context.Context, endpoint string) (int, error) { 168 osi := ctx.Value(ContextOperationServerIndices) 169 if osi != nil { 170 if operationIndices, ok := osi.(map[string]int); !ok { 171 return 0, reportError("Invalid type %T should be map[string]int", osi) 172 } else { 173 index, ok := operationIndices[endpoint] 174 if ok { 175 return index, nil 176 } 177 } 178 } 179 return getServerIndex(ctx) 180 } 181 182 func getServerVariables(ctx context.Context) (map[string]string, error) { 183 sv := ctx.Value(ContextServerVariables) 184 if sv != nil { 185 if variables, ok := sv.(map[string]string); ok { 186 return variables, nil 187 } 188 return nil, reportError("ctx value of ContextServerVariables has invalid type %T should be map[string]string", sv) 189 } 190 return nil, nil 191 } 192 193 func getServerOperationVariables(ctx context.Context, endpoint string) (map[string]string, error) { 194 osv := ctx.Value(ContextOperationServerVariables) 195 if osv != nil { 196 if operationVariables, ok := osv.(map[string]map[string]string); !ok { 197 return nil, reportError("ctx value of ContextOperationServerVariables has invalid type %T should be map[string]map[string]string", osv) 198 } else { 199 variables, ok := operationVariables[endpoint] 200 if ok { 201 return variables, nil 202 } 203 } 204 } 205 return getServerVariables(ctx) 206 } 207 208 // ServerURLWithContext returns a new server URL given an endpoint 209 func (c *Configuration) ServerURLWithContext(ctx context.Context, endpoint string) (string, error) { 210 sc, ok := c.OperationServers[endpoint] 211 if !ok { 212 sc = c.Servers 213 } 214 215 if ctx == nil { 216 return sc.URL(0, nil) 217 } 218 219 index, err := getServerOperationIndex(ctx, endpoint) 220 if err != nil { 221 return "", err 222 } 223 224 variables, err := getServerOperationVariables(ctx, endpoint) 225 if err != nil { 226 return "", err 227 } 228 229 return sc.URL(index, variables) 230 }