github.com/Files-com/files-sdk-go/v3@v3.1.81/config.go (about) 1 package files_sdk 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "strings" 8 9 "github.com/Files-com/files-sdk-go/v3/lib" 10 libLog "github.com/Files-com/files-sdk-go/v3/lib/logpath" 11 "github.com/hashicorp/go-retryablehttp" 12 ) 13 14 var VERSION = "3.1.81" 15 16 const ( 17 UserAgent = "Files.com Go SDK" 18 DefaultSite = "app" 19 APIPath = "/api/rest/v1" 20 ) 21 22 var GlobalConfig Config 23 24 func init() { 25 GlobalConfig = Config{}.Init() 26 } 27 28 type Config struct { 29 APIKey string `header:"X-FilesAPI-Key" json:"api_key"` 30 SessionId string `header:"X-FilesAPI-Auth" json:"session_id"` 31 Subdomain string `json:"subdomain"` 32 EndpointOverride string `json:"endpoint_override"` 33 *retryablehttp.Client 34 AdditionalHeaders map[string]string `json:"additional_headers"` 35 lib.Logger 36 Debug bool `json:"debug"` 37 UserAgent string `json:"user_agents"` 38 Environment `json:"environment"` 39 FeatureFlags map[string]bool `json:"feature_flags"` 40 } 41 42 func (c Config) Init() Config { 43 if c.Logger == nil { 44 c.Logger = lib.NullLogger{} 45 } 46 if c.Client == nil { 47 c.Client = lib.DefaultRetryableHttp(c) 48 } 49 50 if c.FeatureFlags == nil { 51 c.FeatureFlags = FeatureFlags() 52 } 53 54 c.UserAgent = fmt.Sprintf("%v %v", UserAgent, strings.TrimSpace(VERSION)) 55 56 return c 57 } 58 59 func (c Config) Endpoint() string { 60 if c.EndpointOverride != "" && !strings.HasPrefix(c.EndpointOverride, "https://") && !strings.HasPrefix(c.EndpointOverride, "http://") { 61 c.EndpointOverride = "https://" + c.EndpointOverride 62 } 63 64 return lib.DefaultString( 65 c.EndpointOverride, 66 strings.Replace(c.Environment.Endpoint(), "{{SUBDOMAIN}}", lib.DefaultString(c.Subdomain, DefaultSite), 1), 67 ) 68 } 69 70 func (c Config) Do(req *http.Request) (*http.Response, error) { 71 return c.Client.StandardClient().Do(req) 72 } 73 74 func (c Config) SetCustomClient(client *http.Client) Config { 75 c.Client = lib.DefaultRetryableHttp(c, client) 76 return c 77 } 78 79 func (c Config) InDebug() bool { 80 return c.Debug || (os.Getenv("FILES_SDK_DEBUG") != "") 81 } 82 83 func (c Config) LogPath(path string, args map[string]interface{}) { 84 c.Logger.Printf(libLog.New(path, args)) 85 } 86 87 func (c Config) RootPath() string { 88 return c.Endpoint() + APIPath 89 } 90 91 func (c Config) GetAPIKey() string { 92 return lib.DefaultString(c.APIKey, os.Getenv("FILES_API_KEY")) 93 } 94 95 func (c Config) SetHeaders(headers *http.Header) { 96 headers.Set("User-Agent", c.UserAgent) 97 if c.GetAPIKey() != "" { 98 headers.Set("X-FilesAPI-Key", c.GetAPIKey()) 99 } else if c.SessionId != "" { 100 headers.Set("X-FilesAPI-Auth", c.SessionId) 101 } 102 103 for key, value := range c.AdditionalHeaders { 104 headers.Set(key, value) 105 } 106 } 107 108 func (c Config) FeatureFlag(flag string) bool { 109 value, ok := c.FeatureFlags[flag] 110 if !ok { 111 panic(fmt.Sprintf("feature flag `%v` is not a valid flag", flag)) 112 } 113 return value 114 } 115 116 func FeatureFlags() map[string]bool { 117 return map[string]bool{ 118 "incremental-updates": false, 119 } 120 }