github.com/schmorrison/Zoho@v1.1.4/zoho.go (about)

     1  package zoho
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/http"
     7  	"time"
     8  
     9  	"github.com/hashicorp/go-retryablehttp"
    10  )
    11  
    12  // New initializes a Zoho structure
    13  func New() *Zoho {
    14  	retryClient := retryablehttp.NewClient()
    15  	retryClient.Logger = nil
    16  	retryClient.RetryMax = 1
    17  	retryClient.HTTPClient = &http.Client{
    18  		Timeout: time.Second * 10,
    19  		Transport: &http.Transport{
    20  			Dial: (&net.Dialer{
    21  				Timeout: 5 * time.Second,
    22  			}).Dial,
    23  			TLSHandshakeTimeout: 5 * time.Second,
    24  		},
    25  	}
    26  
    27  	z := Zoho{
    28  		client:     retryClient.StandardClient(),
    29  		ZohoTLD:    "com",
    30  		tokensFile: "./.tokens.zoho",
    31  		oauth: OAuth{
    32  			baseURL: "https://accounts.zoho.com/oauth/v2/",
    33  		},
    34  	}
    35  
    36  	return &z
    37  }
    38  
    39  // SetTokenManager can be used to provide a type which implements the TokenManager interface
    40  // which will get/set AccessTokens/RenewTokens using a persistence mechanism
    41  func (z *Zoho) SetTokenManager(tm TokenLoaderSaver) {
    42  	z.tokenManager = tm
    43  }
    44  
    45  // SetTokensFile can be used to set the file location of the token persistence location,
    46  // by default tokens are stored in a file in the current directory called '.tokens.zoho'
    47  func (z *Zoho) SetTokensFile(s string) {
    48  	z.tokensFile = s
    49  }
    50  
    51  // SetZohoTLD can be used to set the TLD extension for API calls for example for Zoho in EU and China.
    52  // by default this is set to "com", other options are "eu" and "ch"
    53  func (z *Zoho) SetZohoTLD(s string) {
    54  	z.ZohoTLD = s
    55  	z.oauth.baseURL = fmt.Sprintf("https://accounts.zoho.%s/oauth/v2/", s)
    56  }
    57  
    58  // CustomHTTPClient can be used to provide a custom HTTP Client that replaces the once instantiated
    59  // when executing New()
    60  //
    61  // A notable use case is AppEngine where a user must use the appengine/urlfetch packages provided http client
    62  // when performing outbound http requests.
    63  func (z *Zoho) CustomHTTPClient(c *http.Client) {
    64  	z.client = c
    65  }
    66  
    67  // SetOrganizationID can be used to add organization id in zoho struct
    68  // which is needed for expense apis
    69  func (z *Zoho) SetOrganizationID(orgID string) {
    70  	z.OrganizationID = orgID
    71  }
    72  
    73  // Zoho is for accessing all APIs. It is used by subpackages to simplify passing authentication
    74  // values between API subpackages.
    75  type Zoho struct {
    76  	oauth OAuth
    77  
    78  	client         *http.Client
    79  	tokenManager   TokenLoaderSaver
    80  	tokensFile     string
    81  	OrganizationID string
    82  
    83  	ZohoTLD string
    84  }
    85  
    86  // OAuth is the OAuth part of the Zoho struct
    87  type OAuth struct {
    88  	scopes       []ScopeString
    89  	clientID     string
    90  	clientSecret string
    91  	redirectURI  string
    92  	token        AccessTokenResponse
    93  	baseURL      string
    94  }