forked from scoro-api/v3-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_factory.go
105 lines (88 loc) · 2.57 KB
/
client_factory.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
95
96
97
98
99
100
101
102
103
104
105
package scoro
import (
"context"
"github.com/joho/godotenv"
"golang.org/x/oauth2"
"log"
"os"
)
const envSiteURL = "SITE_URL"
const envClientID = "CLIENT_ID"
const envClientSecret = "CLIENT_SECRET"
const envScope = "SCOPE"
const envRedirectUrl = "REDIRECT_URL"
const envLang = "API_LANG"
type ApiClientActions interface {
HandleAuthorization(oauth2.Config) string
SaveTokens(*oauth2.Token)
FetchTokens() *oauth2.Token
}
type ApiConfig struct {
oauthConfig oauth2.Config
siteUrl string
}
// GetClient return http.APIClient using the provided oauth2.Config
func GetClient(config ApiConfig, client interface{ ApiClientActions }) APIClient {
ctx := context.Background()
tok := client.FetchTokens()
if len(tok.AccessToken) == 0 {
connect(config.oauthConfig, client)
tok = client.FetchTokens()
}
tokenSource := config.oauthConfig.TokenSource(ctx, tok)
newToken, err := tokenSource.Token()
if err != nil {
log.Fatalln(err)
}
if newToken.AccessToken != tok.AccessToken {
client.SaveTokens(newToken)
tok = newToken
}
return APIClient{config: config, httpClient: config.oauthConfig.Client(ctx, tok), customHeaders: make(map[string]string)}
}
// GetAPIClientConfigFromEnvFile returns oauth2.Config from .env file
func GetAPIClientConfigFromEnvFile() ApiConfig {
siteURL := getEnvVariable(envSiteURL)
clientID := getEnvVariable(envClientID)
clientSecret := getEnvVariable(envClientSecret)
scope := getEnvVariable(envScope)
redirectURL := getEnvVariable(envRedirectUrl)
language := getEnvVariable(envLang)
if len(clientID) == 0 || len(clientSecret) == 0 || len(language) == 0 || len(siteURL) == 0 || len(scope) == 0 || len(redirectURL) == 0 {
log.Fatalf("Error loading values from .env file")
}
oauthConf := oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{scope},
Endpoint: oauth2.Endpoint{
TokenURL: siteURL + "/api/" + apiVersion + "/token",
AuthURL: siteURL + "/apiAuth",
},
RedirectURL: redirectURL,
}
return ApiConfig{oauthConf, siteURL}
}
func getEnvVariable(key string) string {
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
err = godotenv.Load(path + "/.env")
if err != nil {
log.Fatalf("Error loading .env file")
}
return os.Getenv(key)
}
func connect(config oauth2.Config, client ApiClientActions) {
ctx := context.Background()
authorizationCode := client.HandleAuthorization(config)
if len(authorizationCode) == 0 {
log.Fatalf("Error getting authorization code")
}
tok, err := config.Exchange(ctx, authorizationCode)
if err != nil {
log.Fatal(err)
}
client.SaveTokens(tok)
}