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