make client interface

This commit is contained in:
Mike Mason
2023-07-18 13:36:41 +00:00
parent 05762f5d75
commit f828546cfe
10 changed files with 72 additions and 46 deletions

View File

@@ -12,21 +12,37 @@ import (
"time"
"github.com/labstack/echo/v4"
"go.infratographer.com/x/gidx"
"go.uber.org/zap"
)
const (
defaultPermissionsURL = "https://permissions-api.hollow-a.sv15.metalkube.net"
DefaultHTTPClientTimeout = 5 * time.Second
defaultHTTPClientTimeout = 5 * time.Second
)
// DefaultHTTPClient is the default HTTP client for the Permissions Client.
var DefaultHTTPClient = &http.Client{
Timeout: DefaultHTTPClientTimeout,
Timeout: defaultHTTPClientTimeout,
}
// Client is the permissions client.
type Client struct {
// Client defines the Permissions API client interface.
type Client interface {
AssignRole(ctx context.Context, roleID gidx.PrefixedID, memberID gidx.PrefixedID) error
CreateRole(ctx context.Context, resourceID gidx.PrefixedID, actions []string) (gidx.PrefixedID, error)
DeleteResourceRelationship(ctx context.Context, resourceID gidx.PrefixedID, relation string, relatedResourceID gidx.PrefixedID) error
DeleteRole(ctx context.Context, roleID gidx.PrefixedID) error
FindResourceRoleByActions(ctx context.Context, resourceID gidx.PrefixedID, actions []string) (ResourceRole, error)
ListResourceRelationships(ctx context.Context, resourceID gidx.PrefixedID, relatedResourceType string) ([]ResourceRelationship, error)
ListResourceRoles(ctx context.Context, resourceID gidx.PrefixedID) (ResourceRoles, error)
ListRoleAssignments(ctx context.Context, roleID gidx.PrefixedID) ([]gidx.PrefixedID, error)
RoleHasAssignment(ctx context.Context, roleID gidx.PrefixedID, memberID gidx.PrefixedID) (bool, error)
UnassignRole(ctx context.Context, roleID gidx.PrefixedID, memberID gidx.PrefixedID) error
}
// client is the permissions client.
type client struct {
logger *zap.SugaredLogger
httpClient *http.Client
@@ -39,7 +55,7 @@ type Client struct {
// Do executes the provided request.
// If the out value is provided, the response will attempt to be json decoded.
func (c *Client) Do(req *http.Request, out any) (*http.Response, error) {
func (c *client) Do(req *http.Request, out any) (*http.Response, error) {
if c.token != "" {
req.Header.Set(echo.HeaderAuthorization, "Bearer "+c.token)
}
@@ -63,7 +79,7 @@ func (c *Client) Do(req *http.Request, out any) (*http.Response, error) {
}
// DoRequest creates a new request from the provided parameters and executes the request.
func (c *Client) DoRequest(ctx context.Context, method, path string, body io.Reader, out any) (*http.Response, error) {
func (c *client) DoRequest(ctx context.Context, method, path string, body io.Reader, out any) (*http.Response, error) {
path = strings.TrimPrefix(path, c.baseURL.Path)
pathURL, err := url.Parse(path)
@@ -109,8 +125,8 @@ func encodeJSON(v any) (*bytes.Buffer, error) {
}
// NewClient creats a new permissions client.
func NewClient(token string, options ...Option) (*Client, error) {
client := &Client{
func NewClient(token string, options ...Option) (Client, error) {
client := &client{
logger: zap.NewNop().Sugar(),
httpClient: DefaultHTTPClient,
token: token,