Newer
Older
"io"
"net/http"
"net/url"
"code.vereign.com/gaiax/tsa/golib/errors"
func New(addr string, httpClient *http.Client) *Client {
// Evaluate calls the policy service to execute the given policy.
func (c *Client) Evaluate(ctx context.Context, policy string, data []byte) ([]byte, error) {
uri := c.addr + "/policy/" + policy + "/evaluation"
policyURL, err := url.ParseRequestURI(uri)
if err != nil {
return nil, errors.New(errors.BadRequest, "invalid policy evaluation URL", err)
}
req, err := http.NewRequest("POST", policyURL.String(), bytes.NewReader(data))
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response on policy evaluation: %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)