-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
92 lines (75 loc) · 1.79 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package ebct
import (
"encoding/base64"
"fmt"
)
type Client struct {
Token string
Sandbox bool
BasicAuth string
Package string
ExpiryDate string
IdCorreios string
DsContract string
DsCartaoPostagem string
DsNamespace string
FlSoap bool
}
func NewClientToken(user, pass, contract, cartaopostagem, tpPost string, sandbox bool) (*Client, error) {
c := &Client{
BasicAuth: fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(user+":"+pass))),
Package: contract,
Sandbox: sandbox,
IdCorreios: user,
DsContract: contract,
DsCartaoPostagem: cartaopostagem,
}
tmpLoginToken := LoginToken{
Numero: contract,
}
model := new(TokenResponse)
var err error
switch tpPost {
case "AUTH":
err = c.Post(tokenAuth, nil, nil, model)
case "CONTRATO":
err = c.Post(tokenAuthContrato, tmpLoginToken, nil, model)
case "CARTAOPOSTAGEM":
tmpLoginToken.Numero = cartaopostagem
err = c.Post(tokenAuthCartaoPostagem, tmpLoginToken, nil, model)
case "LOGISTICAREVERSA":
err = c.Post(tokenAuth, nil, nil, model)
}
if err == nil {
if *model.Token != "" {
c.Token = *model.Token
c.ExpiryDate = *model.ExpiraEm
}
}
return c, err
}
func NewClient(user, pass, token string, sandbox bool) *Client {
return &Client{
BasicAuth: fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(user+":"+pass))),
Token: token,
Sandbox: sandbox,
}
}
func (c *Client) GetToken() string {
if c.Token != "" {
return fmt.Sprintf("Bearer %s", c.Token)
}
return ""
}
func (c *Client) GetEndpoint() string {
if c.FlSoap {
if c.Sandbox {
return SandBoxUrlSoap
}
return ProductionUrlSoap
}
if c.Sandbox {
return SandBoxUrl
}
return ProductionUrl
}