adding new routes to the api client

This commit is contained in:
James Tombleson 2024-05-26 09:59:31 -07:00
parent 5a950af7a1
commit d5fffa1d66
2 changed files with 66 additions and 1 deletions

View File

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

View File

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