66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package apiclient
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/domain"
|
|
)
|
|
|
|
const (
|
|
UserLoginRoute = "api/v1/users/login"
|
|
UserRegisterRoute = "api/v1/users/register"
|
|
)
|
|
|
|
type Users interface {
|
|
Login(username, password string) (domain.LoginResponse, error)
|
|
SignUp(username, password string) (domain.BaseResponse, error)
|
|
}
|
|
|
|
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) {
|
|
endpoint := fmt.Sprintf("%s/%s", a.serverAddress, UserLoginRoute)
|
|
|
|
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) {
|
|
endpoint := fmt.Sprintf("%s/%s", a.serverAddress, UserRegisterRoute)
|
|
|
|
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
|
|
}
|