60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package helpers
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"resty.dev/v3"
|
|
)
|
|
|
|
type Login struct {
|
|
Email string
|
|
Password string
|
|
}
|
|
|
|
type LoginResponse struct {
|
|
Token string
|
|
}
|
|
|
|
func GetToken(client *resty.Client) (token string) {
|
|
var login LoginResponse
|
|
_, err := client.R().
|
|
SetBody(Login{
|
|
Email: "thomas.peetz@thpeetz.de",
|
|
Password: "admin",
|
|
}).
|
|
SetHeader("Accept", "application/json").
|
|
SetResult(&login).
|
|
Post("http://127.0.0.1:8550/api/login")
|
|
if err != nil {
|
|
slog.Info("Health", "error", err.Error())
|
|
}
|
|
slog.Info("health", "token", login.Token)
|
|
return login.Token
|
|
}
|
|
|
|
func GetJSON(client *resty.Client, token string, url string, v any) {
|
|
_, err := client.R().
|
|
SetAuthToken(token).
|
|
SetHeader("Accept", "application/json").
|
|
SetResult(v).
|
|
Get(url)
|
|
if err != nil {
|
|
slog.Info(url, "error", err.Error())
|
|
}
|
|
}
|
|
|
|
func PostJSON(client *resty.Client, url string, v any) {
|
|
_, err := client.R().
|
|
SetBody(Login{
|
|
Email: "thomas.peetz@thpeetz.de",
|
|
Password: "admin",
|
|
}).
|
|
SetHeader("Accept", "application/json").
|
|
SetResult(v).
|
|
Post(url)
|
|
if err != nil {
|
|
slog.Info("Health", "error", err.Error())
|
|
}
|
|
slog.Info(url, "token", v)
|
|
}
|