github.com/Finnhub-Stock-API/finnhub-go@v1.2.1/configuration.go (about)

     1  /*
     2   * Finnhub API
     3   *
     4   * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
     5   *
     6   * API version: 1.0.0
     7   * Generated by: OpenAPI Generator (https://openapi-generator.tech)
     8   */
     9  
    10  package finnhub
    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  
    43  // BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
    44  type BasicAuth struct {
    45  	UserName string `json:"userName,omitempty"`
    46  	Password string `json:"password,omitempty"`
    47  }
    48  
    49  // APIKey provides API key based authentication to a request passed via context using ContextAPIKey
    50  type APIKey struct {
    51  	Key    string
    52  	Prefix string
    53  }
    54  
    55  
    56  // ServerVariable stores the information about a server variable
    57  type ServerVariable struct {
    58  	Description  string
    59  	DefaultValue string
    60  	EnumValues   []string
    61  }
    62  
    63  // ServerConfiguration stores the information about a server
    64  type ServerConfiguration struct {
    65  	Url string
    66  	Description string
    67  	Variables map[string]ServerVariable
    68  }
    69  
    70  // Configuration stores the configuration of the API client
    71  type Configuration struct {
    72  	BasePath      string            `json:"basePath,omitempty"`
    73  	Host          string            `json:"host,omitempty"`
    74  	Scheme        string            `json:"scheme,omitempty"`
    75  	DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
    76  	UserAgent     string            `json:"userAgent,omitempty"`
    77  	Debug         bool              `json:"debug,omitempty"`
    78  	Servers       []ServerConfiguration
    79  	HTTPClient    *http.Client
    80  }
    81  
    82  // NewConfiguration returns a new Configuration object
    83  func NewConfiguration() *Configuration {
    84  	cfg := &Configuration{
    85  		BasePath:      "https://finnhub.io/api/v1",
    86  		DefaultHeader: make(map[string]string),
    87  		UserAgent:     "OpenAPI-Generator/1.2.1/go",
    88  		Debug:         false,
    89  		Servers:       []ServerConfiguration{
    90  			{
    91  				Url: "https://finnhub.io/api/v1",
    92  				Description: "No description provided",
    93  			},
    94  		},
    95  	}
    96  	return cfg
    97  }
    98  
    99  // AddDefaultHeader adds a new HTTP header to the default header in the request
   100  func (c *Configuration) AddDefaultHeader(key string, value string) {
   101  	c.DefaultHeader[key] = value
   102  }
   103  
   104  // ServerUrl returns URL based on server settings
   105  func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) {
   106  	if index < 0 || len(c.Servers) <= index {
   107  		return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1)
   108  	}
   109  	server := c.Servers[index]
   110  	url := server.Url
   111  
   112  	// go through variables and replace placeholders
   113  	for name, variable := range server.Variables {
   114  		if value, ok := variables[name]; ok {
   115  			found := bool(len(variable.EnumValues) == 0)
   116  			for _, enumValue := range variable.EnumValues {
   117  				if value == enumValue {
   118  					found = true
   119  				}
   120  			}
   121  			if !found {
   122  				return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
   123  			}
   124  			url = strings.Replace(url, "{"+name+"}", value, -1)
   125  		} else {
   126  			url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
   127  		}
   128  	}
   129  	return url, nil
   130  }