Добавить сессии в Valkey и вход через Basic с Bearer-токеном
This commit is contained in:
@@ -20,7 +20,7 @@ func newClient(baseURL string) *client {
|
||||
return &client{base: baseURL, http: &http.Client{Timeout: 10 * time.Second}}
|
||||
}
|
||||
|
||||
func (c *client) post(path, actor string, body, out any) error {
|
||||
func (c *client) post(path, token string, body, out any) error {
|
||||
raw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -30,12 +30,28 @@ func (c *client) post(path, actor string, body, out any) error {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
return c.do(req, actor, http.StatusCreated, out)
|
||||
return c.do(req, token, http.StatusCreated, out)
|
||||
}
|
||||
|
||||
// login opens a session with Basic credentials and returns its bearer token.
|
||||
func (c *client) login(username, password string) (string, error) {
|
||||
req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.SetBasicAuth(username, password)
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := c.do(req, "", http.StatusCreated, &session); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return session.Token, nil
|
||||
}
|
||||
|
||||
// get requests the largest page the contract allows, continuing from cursor
|
||||
// when it is set.
|
||||
func (c *client) get(path, actor, cursor string, out any) error {
|
||||
func (c *client) get(path, token, cursor string, out any) error {
|
||||
query := url.Values{"limit": {"100"}}
|
||||
if cursor != "" {
|
||||
query.Set("cursor", cursor)
|
||||
@@ -44,12 +60,12 @@ func (c *client) get(path, actor, cursor string, out any) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.do(req, actor, http.StatusOK, out)
|
||||
return c.do(req, token, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (c *client) do(req *http.Request, actor string, want int, out any) error {
|
||||
if actor != "" {
|
||||
req.Header.Set("X-User-Id", actor)
|
||||
func (c *client) do(req *http.Request, token string, want int, out any) error {
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user