newsbot-portal/apiclient/users.go

93 lines
2.2 KiB
Go
Raw Normal View History

2024-05-12 09:21:20 -07:00
package apiclient
import (
"fmt"
"net/http"
"net/url"
"git.jamestombleson.com/jtom38/newsbot-api/domain"
)
const (
2024-05-12 10:46:17 -07:00
UserBaseRoute = "api/v1/users"
2024-05-12 09:21:20 -07:00
)
type Users interface {
Login(username, password string) (domain.LoginResponse, error)
SignUp(username, password string) (domain.BaseResponse, error)
2024-05-12 09:21:20 -07:00
}
type userClient struct {
serverAddress string
client http.Client
}
func newUserService(serverAddress string) userClient {
return userClient{
serverAddress: serverAddress,
client: http.Client{},
}
}
func (a userClient) Login(username, password string) (domain.LoginResponse, error) {
2024-05-12 10:46:17 -07:00
endpoint := fmt.Sprintf("%s/%s/login", a.serverAddress, UserBaseRoute)
2024-05-12 09:21:20 -07:00
param := url.Values{}
param.Set("username", username)
param.Set("password", password)
// Create the struct we are expecting back from the API so we can have it populated
var bind = domain.LoginResponse{}
err := PostUrlForm(a.client, endpoint, param, &bind)
if err != nil {
return domain.LoginResponse{}, err
}
return bind, nil
}
func (a userClient) SignUp(username, password string) (domain.BaseResponse, error) {
2024-05-12 10:46:17 -07:00
endpoint := fmt.Sprintf("%s/%s/register", a.serverAddress, UserBaseRoute)
2024-05-26 09:59:31 -07:00
param := url.Values{}
param.Set("username", username)
param.Set("password", password)
// Create the struct we are expecting back from the API so we can have it populated
var bind = domain.BaseResponse{}
err := PostUrlForm(a.client, endpoint, param, &bind)
if err != nil {
return domain.BaseResponse{}, err
}
return bind, nil
}
2024-05-26 09:59:31 -07:00
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
}