github.com/0chain/gosdk@v1.17.11/sdks/client.go (about)

     1  package sdks
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  
     7  	"github.com/0chain/errors"
     8  	"github.com/0chain/gosdk/constants"
     9  	"github.com/0chain/gosdk/core/encryption"
    10  	"github.com/0chain/gosdk/core/sys"
    11  	"github.com/0chain/gosdk/zboxcore/client"
    12  )
    13  
    14  // Client a client instance of restful api
    15  type Client struct {
    16  	ClientID        string
    17  	ClientPublicKey string
    18  	BaseURL         string
    19  }
    20  
    21  // create client instance
    22  func NewClient(clientID, clientPublicKey, baseURL string) (Client, error) {
    23  	u, err := url.Parse(baseURL)
    24  
    25  	c := Client{
    26  		ClientID:        clientID,
    27  		ClientPublicKey: clientPublicKey,
    28  	}
    29  
    30  	if err != nil {
    31  		return c, errors.Throw(constants.ErrInvalidParameter, "baseURL")
    32  	}
    33  
    34  	c.BaseURL = u.String()
    35  
    36  	return c, nil
    37  }
    38  
    39  func (c *Client) SignRequest(req *http.Request, allocation string) error {
    40  
    41  	req.Header.Set("X-App-Client-ID", c.ClientID)
    42  	req.Header.Set("X-App-Client-Key", c.ClientPublicKey)
    43  
    44  	sign, err := sys.Sign(encryption.Hash(allocation), client.GetClient().SignatureScheme, client.GetClientSysKeys())
    45  	if err != nil {
    46  		return err
    47  	}
    48  	req.Header.Set("X-App-Client-Signature", sign)
    49  
    50  	return nil
    51  }