refactor foundry lib structure; add transport classes for db,http,websocket; add action interface instead of multiple function that accepts action classes; add changing foundry status on websocket responses
This commit is contained in:
@@ -42,17 +42,17 @@ var PathToSetupState = map[string]db.StateType{
|
||||
}
|
||||
|
||||
var PathToWorldState = map[string]db.StateType{
|
||||
JoinPath: db.JoinState,
|
||||
PlayersPath: db.PlayersState,
|
||||
JoinPath: db.JoinState,
|
||||
// PlayersPath: db.PlayersState,
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
type FoundryHttpRequest struct {
|
||||
Host string
|
||||
Password string
|
||||
SessionID string
|
||||
}
|
||||
|
||||
func (conf *Config) SetSessionTokenFromHeader(resp http.Header) (bool, error) {
|
||||
func (conf *FoundryHttpRequest) SetSessionTokenFromHeader(resp http.Header) (bool, error) {
|
||||
setCookieHeader := resp.Get("Set-Cookie")
|
||||
if setCookieHeader == "" {
|
||||
return false, nil
|
||||
@@ -73,7 +73,7 @@ func (conf *Config) SetSessionTokenFromHeader(resp http.Header) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (conf *Config) GetRequestHeader() *http.Header {
|
||||
func (conf *FoundryHttpRequest) GetRequestHeader() *http.Header {
|
||||
header := http.Header{}
|
||||
header.Set("Cookie", fmt.Sprintf("session=%s", conf.SessionID))
|
||||
header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
json_model "gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/json"
|
||||
)
|
||||
|
||||
func (conf *Config) GetSessionId() error {
|
||||
func (conf *FoundryHttpRequest) GetSessionId() error {
|
||||
getResp, err := http.Get(fmt.Sprintf("http://%s%s", conf.Host, AuthPath))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -18,7 +20,7 @@ func (conf *Config) GetSessionId() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (conf *Config) GetSessionToken() (bool, error) {
|
||||
func (conf *FoundryHttpRequest) GetSessionToken() (bool, error) {
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s%s", conf.Host, AuthPath), nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -42,21 +44,21 @@ func (conf *Config) GetSessionToken() (bool, error) {
|
||||
return conf.SetSessionTokenFromHeader(resp.Header)
|
||||
}
|
||||
|
||||
func (conf *Config) GetStatus() (*json_model.Status, error) {
|
||||
func (conf *FoundryHttpRequest) GetStatus() (*json_model.Status, error) {
|
||||
getResp, err := http.Get(fmt.Sprintf("http://%s/api/status", conf.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer getResp.Body.Close()
|
||||
|
||||
statusByte := make([]byte, 64)
|
||||
_, err = getResp.Body.Read(statusByte)
|
||||
if err != nil {
|
||||
statusByte := bytes.Buffer{}
|
||||
_, err = io.Copy(&statusByte, getResp.Body)
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var status json_model.Status
|
||||
err = json.Unmarshal(statusByte, &status)
|
||||
err = json.Unmarshal(statusByte.Bytes(), &status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (conf *Config) PostAuthenticationData() (*http.Response, error) {
|
||||
func (conf *FoundryHttpRequest) PostAuthenticationData() (*http.Response, error) {
|
||||
authData := url.Values{}
|
||||
authData.Add("adminPassword", conf.Password)
|
||||
authData.Add("action", "adminAuth")
|
||||
@@ -30,7 +30,7 @@ func (conf *Config) PostAuthenticationData() (*http.Response, error) {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (conf *Config) PostLaunchWorld(world string) (*http.Response, error) {
|
||||
func (conf *FoundryHttpRequest) PostLaunchWorld(world string) (*http.Response, error) {
|
||||
launchData := url.Values{}
|
||||
launchData.Add("world", world)
|
||||
launchData.Add("action", "launchWorld")
|
||||
@@ -53,7 +53,7 @@ func (conf *Config) PostLaunchWorld(world string) (*http.Response, error) {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (conf *Config) PostReturnToSetup() (*http.Response, error) {
|
||||
func (conf *FoundryHttpRequest) PostReturnToSetup() (*http.Response, error) {
|
||||
launchData := url.Values{}
|
||||
launchData.Add("action", "shutdown")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user