From d5fffa1d6620de5a2e954afb20b0950e973c978a Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 26 May 2024 09:59:31 -0700 Subject: [PATCH] adding new routes to the api client --- apiclient/users.go | 30 +++++++++++++++++++++++++++++- apiclient/util.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/apiclient/users.go b/apiclient/users.go index e5c3dfe..7510802 100644 --- a/apiclient/users.go +++ b/apiclient/users.go @@ -48,7 +48,7 @@ func (a userClient) Login(username, password string) (domain.LoginResponse, erro func (a userClient) SignUp(username, password string) (domain.BaseResponse, error) { endpoint := fmt.Sprintf("%s/%s/register", a.serverAddress, UserBaseRoute) - + param := url.Values{} param.Set("username", username) param.Set("password", password) @@ -62,3 +62,31 @@ func (a userClient) SignUp(username, password string) (domain.BaseResponse, erro return bind, nil } + +func (a userClient) RefreshJwtToken(username, refreshToken string) (domain.LoginResponse, error) { + endpoint := fmt.Sprintf("%s/%s/refresh/token", a.serverAddress, UserBaseRoute) + body := domain.RefreshTokenRequest{ + Username: username, + RefreshToken: refreshToken, + } + + var bind = domain.LoginResponse{} + err := PostBodyUrl(a.client, endpoint, body, &bind) + if err != nil { + return domain.LoginResponse{}, err + } + + return bind, nil +} + +func (a userClient) refreshSessionToken() (domain.BaseResponse, error) { + endpoint := fmt.Sprintf("%s/%s/refresh/sessionToken", a.serverAddress, UserBaseRoute) + + var bind = domain.BaseResponse{} + err := PostUrl(a.client, endpoint, &bind) + if err != nil { + return domain.BaseResponse{}, err + } + + return bind, nil +} diff --git a/apiclient/util.go b/apiclient/util.go index 6ba91f9..441e89e 100644 --- a/apiclient/util.go +++ b/apiclient/util.go @@ -29,3 +29,40 @@ func PostUrlForm(client http.Client, endpoint string, param url.Values, t any) e return nil } + +func PostUrl(client http.Client, endpoint string, t any) error { + response, err := http.Post(endpoint, ApplicationJson, nil) + if err != nil { + return err + } + + defer response.Body.Close() + decoder := json.NewDecoder(response.Body) + err = decoder.Decode(&t) + if err != nil { + return err + } + + return nil +} + +func PostBodyUrl(client http.Client, endpoint string, body any, t any) error { + jsonBody, err := json.Marshal(body) + if err != nil { + return err + } + + response, err := http.Post(endpoint, ApplicationJson, bytes.NewBuffer(jsonBody)) + if err != nil { + return err + } + + defer response.Body.Close() + decoder := json.NewDecoder(response.Body) + err = decoder.Decode(&t) + if err != nil { + return err + } + + return nil +}