-
Notifications
You must be signed in to change notification settings - Fork 27
/
client.go
51 lines (39 loc) · 1.09 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
package sdks
import (
"net/http"
"net/url"
"github.com/0chain/errors"
"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/sys"
"github.com/0chain/gosdk/zboxcore/client"
)
// Client a client instance of restful api
type Client struct {
ClientID string
ClientPublicKey string
BaseURL string
}
// create client instance
func NewClient(clientID, clientPublicKey, baseURL string) (Client, error) {
u, err := url.Parse(baseURL)
c := Client{
ClientID: clientID,
ClientPublicKey: clientPublicKey,
}
if err != nil {
return c, errors.Throw(constants.ErrInvalidParameter, "baseURL")
}
c.BaseURL = u.String()
return c, nil
}
func (c *Client) SignRequest(req *http.Request, allocation string) error {
req.Header.Set("X-App-Client-ID", c.ClientID)
req.Header.Set("X-App-Client-Key", c.ClientPublicKey)
sign, err := sys.Sign(encryption.Hash(allocation), client.GetClient().SignatureScheme, client.GetClientSysKeys())
if err != nil {
return err
}
req.Header.Set("X-App-Client-Signature", sign)
return nil
}