Add GetUser to UserClient

This commit is contained in:
Torjus Håkestad 2023-10-22 22:59:16 +02:00
parent abbc102060
commit 8286336c32

View File

@ -7,6 +7,8 @@ import (
"fmt"
"net/http"
"time"
"git.t-juice.club/microfilm/users"
)
type UserClient struct {
@ -53,3 +55,36 @@ func (c *UserClient) VerifyUserPassword(username, password string) error {
return nil
}
func (c *UserClient) GetUser(identifier string) (users.User, error) {
var u users.User
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
url := fmt.Sprintf("%s/%s", c.BaseURL, identifier)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return u, err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return u, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return u, fmt.Errorf("authentication failed")
}
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&u); err != nil {
return u, err
}
return u, nil
}