add MediaLofi in kontor-gin (#97)
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
Co-authored-by: Thomas Peetz <thomas.peetz@cimt-ag.de> Reviewed-on: #97
This commit was merged in pull request #97.
This commit is contained in:
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"kontor-api-gin/internal/config"
|
||||
"kontor-api-gin/internal/models"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -21,7 +22,7 @@ type LoginResponse struct {
|
||||
|
||||
type ExportData struct {
|
||||
Sport models.SportDTOList `json:"sport"`
|
||||
Player []models.PlayerDTO `json:"player"`
|
||||
Player models.PlayerDTOList `json:"player"`
|
||||
Team []models.TeamDTO `json:"team"`
|
||||
Position []models.PositionDTO `json:"field_position"`
|
||||
Rooster []models.RoosterDTO `json:"rooster"`
|
||||
@@ -64,6 +65,8 @@ func (e *ExportData) GetMap() map[string]any {
|
||||
switch path {
|
||||
case "tysc/sports":
|
||||
dataMap["sports"] = value
|
||||
case "tysc/players":
|
||||
dataMap["players"] = value
|
||||
}
|
||||
}
|
||||
return dataMap
|
||||
@@ -110,14 +113,33 @@ var (
|
||||
|
||||
var Registry map[string]any
|
||||
|
||||
func GetData(server, apiVersion string) (data *ExportData, err error) {
|
||||
func GetToken(client *resty.Client, server config.Server) (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 GetData(server config.Server, apiVersion string) (data *ExportData, err error) {
|
||||
var login LoginResponse
|
||||
|
||||
client := resty.New()
|
||||
PostJSON(client, fmt.Sprintf("%s/%s/%s", server, "api", "login"), &login)
|
||||
client.SetLoggerWarnLevel(true)
|
||||
PostJSON(client, server, "api/login", &login)
|
||||
|
||||
for path, value := range Registry {
|
||||
GetJSON(client, login.Token, fmt.Sprintf("%s/%s/%s", server, apiVersion, path), value)
|
||||
GetJSON(client, login.Token, server, path, value)
|
||||
}
|
||||
export := ExportData{
|
||||
Sport: Sports,
|
||||
@@ -176,7 +198,11 @@ func WriteJson(data *ExportData, fileName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetJSON(client *resty.Client, token string, url string, v any) {
|
||||
func GetJSON(client *resty.Client, token string, server config.Server, path string, v any) {
|
||||
url := fmt.Sprintf("%s/%s/%s", server.Url, config.Config.ApiVersion, path)
|
||||
if config.Verbose > 2 {
|
||||
slog.Info("GetJSON", "url", url)
|
||||
}
|
||||
_, err := client.R().
|
||||
SetAuthToken(token).
|
||||
SetHeader("Accept", "application/json").
|
||||
@@ -185,21 +211,30 @@ func GetJSON(client *resty.Client, token string, url string, v any) {
|
||||
if err != nil {
|
||||
slog.Info(url, "error", err.Error())
|
||||
}
|
||||
if config.Verbose > 2 {
|
||||
slog.Info("GetJSON", "value", v)
|
||||
}
|
||||
}
|
||||
|
||||
func PostJSON(client *resty.Client, url string, v any) {
|
||||
func PostJSON(client *resty.Client, server config.Server, path string, v any) {
|
||||
url := fmt.Sprintf("%s/%s", server.Url, path)
|
||||
if config.Verbose > 2 {
|
||||
slog.Info("PostJSON", "url", url)
|
||||
}
|
||||
_, err := client.R().
|
||||
SetBody(Login{
|
||||
Email: "thomas.peetz@thpeetz.de",
|
||||
Password: "admin",
|
||||
Email: server.Username,
|
||||
Password: server.Password,
|
||||
}).
|
||||
SetHeader("Accept", "application/json").
|
||||
SetResult(v).
|
||||
Post(url)
|
||||
if err != nil {
|
||||
slog.Info("Health", "error", err.Error())
|
||||
slog.Info("PostJSON", "error", err.Error())
|
||||
}
|
||||
if config.Verbose > 2 {
|
||||
slog.Info(url, "token", v)
|
||||
}
|
||||
slog.Info(url, "token", v)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user