upd(rbac): groundwork for users APIs

This commit is contained in:
Tit Petric
2018-07-17 13:09:18 +02:00
parent b84d61ff18
commit 27e69eb7b8
4 changed files with 73 additions and 2 deletions
+13 -2
View File
@@ -19,6 +19,8 @@ type (
}
)
func (c *Client) Users() *Users { return &Users{c} }
func New() (*Client, error) {
if err := config.validate(); err != nil {
return nil, err
@@ -50,10 +52,18 @@ func New() (*Client, error) {
}
func (c *Client) Get(url string) (*http.Response, error) {
return c.Request("GET", url)
return c.Request("GET", url, nil)
}
func (c *Client) Request(method string, url string) (*http.Response, error) {
func (c *Client) Post(url string, body interface{}) (*http.Response, error) {
return c.Request("POST", url, body)
}
func (c *Client) Delete(url string) (*http.Response, error) {
return c.Request("DELETE", url, nil)
}
func (c *Client) Request(method string, url string, body interface{}) (*http.Response, error) {
link := strings.TrimRight(c.config.baseURL, "/") + "/" + strings.TrimLeft(url, "/")
if c.isDebug {
@@ -65,6 +75,7 @@ func (c *Client) Request(method string, url string) (*http.Response, error) {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(c.config.auth)))
req.Header.Add("X-TENANT-ID", c.config.tenant)
+7
View File
@@ -0,0 +1,7 @@
package types
type (
User struct {
username string
}
)
+53
View File
@@ -0,0 +1,53 @@
package rbac
import (
"encoding/json"
"github.com/crusttech/crust/rbac/types"
)
type (
Users struct {
*Client
}
UsersInterface interface {
Create(username, password string) error
Get(username string) (*types.User, error)
Delete(username string) error
}
)
func (u *Users) Create(username, password string) error {
body := struct {
Password string `json:"password"`
}{password}
resp, err := u.Client.Post("/users/"+username, body)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (u *Users) Get(username string) (*types.User, error) {
resp, err := u.Client.Get("/users/" + username)
if err != nil {
return nil, err
}
user := &types.User{}
defer resp.Body.Close()
return user, json.NewDecoder(resp.Body).Decode(user)
}
func (u *Users) Delete(username string) error {
resp, err := u.Client.Delete("/users/" + username)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
var _ UsersInterface = &Users{}