github.com/moov-io/imagecashletter@v0.10.1/client/configuration.go (about) 1 /* 2 * ImageCashLetter API 3 * 4 * Moov Image Cash Letter (ICL) implements an HTTP API for creating, parsing, and validating ImageCashLetter files. 5 * 6 * API version: v1 7 * Generated by: OpenAPI Generator (https://openapi-generator.tech) 8 */ 9 10 package openapi 11 12 import ( 13 "fmt" 14 "net/http" 15 "strings" 16 ) 17 18 // contextKeys are used to identify the type of value in the context. 19 // Since these are string, it is possible to get a short description of the 20 // context key for logging and debugging using key.String(). 21 22 type contextKey string 23 24 func (c contextKey) String() string { 25 return "auth " + string(c) 26 } 27 28 var ( 29 // ContextOAuth2 takes an oauth2.TokenSource as authentication for the request. 30 ContextOAuth2 = contextKey("token") 31 32 // ContextBasicAuth takes BasicAuth as authentication for the request. 33 ContextBasicAuth = contextKey("basic") 34 35 // ContextAccessToken takes a string oauth2 access token as authentication for the request. 36 ContextAccessToken = contextKey("accesstoken") 37 38 // ContextAPIKey takes an APIKey as authentication for the request 39 ContextAPIKey = contextKey("apikey") 40 ) 41 42 // BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth 43 type BasicAuth struct { 44 UserName string `json:"userName,omitempty"` 45 Password string `json:"password,omitempty"` 46 } 47 48 // APIKey provides API key based authentication to a request passed via context using ContextAPIKey 49 type APIKey struct { 50 Key string 51 Prefix string 52 } 53 54 // ServerVariable stores the information about a server variable 55 type ServerVariable struct { 56 Description string 57 DefaultValue string 58 EnumValues []string 59 } 60 61 // ServerConfiguration stores the information about a server 62 type ServerConfiguration struct { 63 Url string 64 Description string 65 Variables map[string]ServerVariable 66 } 67 68 // Configuration stores the configuration of the API client 69 type Configuration struct { 70 BasePath string `json:"basePath,omitempty"` 71 Host string `json:"host,omitempty"` 72 Scheme string `json:"scheme,omitempty"` 73 DefaultHeader map[string]string `json:"defaultHeader,omitempty"` 74 UserAgent string `json:"userAgent,omitempty"` 75 Debug bool `json:"debug,omitempty"` 76 Servers []ServerConfiguration 77 HTTPClient *http.Client 78 } 79 80 // NewConfiguration returns a new Configuration object 81 func NewConfiguration() *Configuration { 82 cfg := &Configuration{ 83 BasePath: "http://localhost:8083", 84 DefaultHeader: make(map[string]string), 85 UserAgent: "OpenAPI-Generator/1.0.0/go", 86 Debug: false, 87 Servers: []ServerConfiguration{ 88 { 89 Url: "http://localhost:8083", 90 Description: "Local development", 91 }, 92 }, 93 } 94 return cfg 95 } 96 97 // AddDefaultHeader adds a new HTTP header to the default header in the request 98 func (c *Configuration) AddDefaultHeader(key string, value string) { 99 c.DefaultHeader[key] = value 100 } 101 102 // ServerUrl returns URL based on server settings 103 func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) { 104 if index < 0 || len(c.Servers) <= index { 105 return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers)-1) 106 } 107 server := c.Servers[index] 108 url := server.Url 109 110 // go through variables and replace placeholders 111 for name, variable := range server.Variables { 112 if value, ok := variables[name]; ok { 113 found := bool(len(variable.EnumValues) == 0) 114 for _, enumValue := range variable.EnumValues { 115 if value == enumValue { 116 found = true 117 } 118 } 119 if !found { 120 return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues) 121 } 122 url = strings.Replace(url, "{"+name+"}", value, -1) 123 } else { 124 url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1) 125 } 126 } 127 return url, nil 128 }