From 8286336c3207532feab79bd598f72f29fabe8d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Sun, 22 Oct 2023 22:59:16 +0200 Subject: [PATCH] Add GetUser to UserClient --- server/userclient.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/server/userclient.go b/server/userclient.go index 6b79a0b..20c77b3 100644 --- a/server/userclient.go +++ b/server/userclient.go @@ -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 +}