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 +}