configuration.go raw

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