github.com/Files-com/files-sdk-go/v2@v2.1.2/config.go (about)

     1  package files_sdk
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  	"strings"
     9  
    10  	libLog "github.com/Files-com/files-sdk-go/v2/lib/logpath"
    11  	"github.com/hashicorp/go-retryablehttp"
    12  )
    13  
    14  var VERSION = "2.1.2"
    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  type HttpClient interface {
    25  	Do(*http.Request) (*http.Response, error)
    26  	Get(string) (*http.Response, error)
    27  }
    28  
    29  type Logger interface {
    30  	Printf(string, ...interface{})
    31  }
    32  
    33  type Config struct {
    34  	APIKey            string `header:"X-FilesAPI-Key"`
    35  	SessionId         string `header:"X-FilesAPI-Auth"`
    36  	Endpoint          string
    37  	Subdomain         string
    38  	standardClient    HttpClient
    39  	rawClient         *retryablehttp.Client
    40  	AdditionalHeaders map[string]string
    41  	logger            Logger
    42  	Debug             bool
    43  	UserAgent         string
    44  	Environment
    45  	FeatureFlags map[string]bool
    46  }
    47  
    48  func (c *Config) SetHttpClient(client *http.Client) {
    49  	c.GetRawClient().HTTPClient = client
    50  }
    51  
    52  func (c *Config) GetHttpClient() HttpClient {
    53  	if c.standardClient == nil {
    54  		c.standardClient = c.GetRawClient().StandardClient()
    55  	}
    56  	return c.standardClient
    57  }
    58  
    59  func (c *Config) GetRawClient() *retryablehttp.Client {
    60  	if c.rawClient == nil {
    61  		c.rawClient = retryablehttp.NewClient()
    62  		c.rawClient.ErrorHandler = retryablehttp.PassthroughErrorHandler
    63  		c.rawClient.Logger = c.Logger()
    64  		c.rawClient.RetryMax = 3
    65  	}
    66  
    67  	return c.rawClient
    68  }
    69  
    70  type NullLogger struct{}
    71  
    72  func (n NullLogger) Printf(_ string, _ ...interface{}) {
    73  }
    74  
    75  func (c *Config) InDebug() bool {
    76  	return c.Debug || (os.Getenv("FILES_SDK_DEBUG") != "")
    77  }
    78  
    79  func (c *Config) Logger() retryablehttp.Logger {
    80  	if c.logger != nil {
    81  		return c.logger
    82  	}
    83  	if c.InDebug() {
    84  		c.SetLogger(log.New(os.Stderr, "", log.LstdFlags))
    85  	} else {
    86  		c.SetLogger(NullLogger{})
    87  	}
    88  	return c.logger
    89  }
    90  
    91  func (c *Config) LogPath(path string, args map[string]interface{}) {
    92  	c.Logger().Printf(libLog.New(path, args))
    93  }
    94  
    95  func (c *Config) SetLogger(l Logger) {
    96  	c.logger = l
    97  }
    98  
    99  func (c *Config) RootPath() string {
   100  	if c.Subdomain == "" {
   101  		c.Subdomain = DefaultSite
   102  	}
   103  	if c.Endpoint == "" {
   104  		c.Endpoint = strings.Replace(c.Environment.Endpoint(), "{SUBDOMAIN}", c.Subdomain, 1)
   105  	}
   106  	return c.Endpoint + APIPath
   107  }
   108  
   109  func (c *Config) GetAPIKey() string {
   110  	if c.APIKey != "" {
   111  		return c.APIKey
   112  	}
   113  	if os.Getenv("FILES_API_KEY") != "" {
   114  		return os.Getenv("FILES_API_KEY")
   115  	}
   116  	return ""
   117  }
   118  
   119  func (c *Config) SetHeaders(headers *http.Header) {
   120  	if c.UserAgent == "" {
   121  		c.UserAgent = fmt.Sprintf("%v %v", UserAgent, strings.TrimSpace(VERSION))
   122  	}
   123  	headers.Set("User-Agent", c.UserAgent)
   124  	if c.GetAPIKey() != "" {
   125  		headers.Set("X-FilesAPI-Key", c.GetAPIKey())
   126  	} else if c.SessionId != "" {
   127  		headers.Set("X-FilesAPI-Auth", c.SessionId)
   128  	}
   129  
   130  	for key, value := range c.AdditionalHeaders {
   131  		headers.Set(key, value)
   132  	}
   133  }
   134  
   135  func (c *Config) FeatureFlag(flag string) bool {
   136  	var flags map[string]bool
   137  	if c.FeatureFlags == nil {
   138  		flags = FeatureFlags()
   139  	} else {
   140  		flags = c.FeatureFlags
   141  	}
   142  	value, ok := flags[flag]
   143  	if !ok {
   144  		panic(fmt.Sprintf("feature flag `%v` is not a valid flag", flag))
   145  	}
   146  	return value
   147  }
   148  
   149  func FeatureFlags() map[string]bool {
   150  	return map[string]bool{
   151  		"incremental-updates": false,
   152  	}
   153  }