-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
68 lines (56 loc) · 1.51 KB
/
client.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
package resharmonics
import (
"fmt"
"log"
"time"
"github.com/JoseFMP/resharmonics/auth"
)
type client struct {
credentials Credentials
reqTokenChan chan *auth.AuthTask
tokensChan chan string
token string
tokenFetchedOn *time.Time
}
// Client client
type Client interface {
//Auth() error
DoPost(subPath string, params map[string]string) ([]byte, error)
DoGet(subPath string, params map[string]interface{}) ([]byte, error)
}
// Init gives you a Resharmonics client with functionality to do HTTP requests and authenticate
func Init(cred Credentials, preAuthorize bool) (Client, error) {
errValidating := validate(cred.Username, cred.Password)
if errValidating != nil {
return nil, errValidating
}
clientResult := &client{
credentials: cred,
reqTokenChan: make(chan *auth.AuthTask),
tokensChan: make(chan string),
}
go auth.Auth(clientResult.reqTokenChan, cred.Username, cred.Password, clientResult.tokensChan)
if preAuthorize {
clientResult.reqTokenChan <- &auth.AuthTask{}
token := <-clientResult.tokensChan
if token == "" {
return nil, fmt.Errorf("Could not secure initial token")
}
clientResult.token = token
log.Printf("Got initial token.")
}
return clientResult, nil
}
func validate(username string, password string) error {
if username == "" {
return fmt.Errorf("Username is empty")
}
if password == "" {
return fmt.Errorf("Password is empty")
}
return nil
}
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}