mirror of
https://github.com/Voronsky/pterogo.git
synced 2025-12-12 16:46:34 -05:00
Implemented an Interface for ClientAPI
This commit is contained in:
29
pterogo.go
29
pterogo.go
@@ -9,20 +9,27 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Collection of methods that pertain to the Pterodactyl Client API
|
||||||
|
type ClientAPI interface {
|
||||||
|
ListServers() (map[string]Server, error)
|
||||||
|
ServerDetails() (Server, error)
|
||||||
|
}
|
||||||
|
type PterodactylClient struct{}
|
||||||
|
|
||||||
type PteroResp struct {
|
type PteroResp struct {
|
||||||
Object string `json:"object, omitempty"`
|
Object string `json:"object"`
|
||||||
Data []PteroData `json:"data, omitempty"`
|
Data []PteroData `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PteroData struct {
|
type PteroData struct {
|
||||||
Object string `json:"object, omitempty"`
|
Object string `json:"object"`
|
||||||
Attributes Attributes `json:"attributes, omitempty"`
|
Attributes Attributes `json:"attributes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Attributes struct {
|
type Attributes struct {
|
||||||
Name string `json:"name, omitempty"`
|
Name string `json:"name"`
|
||||||
Identifier string `json:"identifier, omitempty"`
|
Identifier string `json:"identifier"`
|
||||||
Description string `json:"description, omitempty"`
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
@@ -34,7 +41,7 @@ type Server struct {
|
|||||||
// Taken from the Pterodactyl API page. This will return an error if it fails at any point
|
// Taken from the Pterodactyl API page. This will return an error if it fails at any point
|
||||||
// Otherwise, it will return a map of unique servers , based off their identifier
|
// Otherwise, it will return a map of unique servers , based off their identifier
|
||||||
// A Bearer Auth token is required
|
// A Bearer Auth token is required
|
||||||
func listServers(auth_token string, url string) (map[string]Server, error) {
|
func (pc PterodactylClient) ListServers(auth_token string, url string) (map[string]Server, error) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||||
//Build GET Request
|
//Build GET Request
|
||||||
@@ -77,7 +84,7 @@ func listServers(auth_token string, url string) (map[string]Server, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("Request successful", "Resp", resp)
|
logger.Info("Request successful", "Resp", resp.StatusCode)
|
||||||
|
|
||||||
servers := map[string]Server{}
|
servers := map[string]Server{}
|
||||||
r := PteroResp{}
|
r := PteroResp{}
|
||||||
@@ -103,7 +110,7 @@ func listServers(auth_token string, url string) (map[string]Server, error) {
|
|||||||
return servers, nil
|
return servers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServerDetails(identifier string, auth_token string, url string) (*Server, error) {
|
func (pc PterodactylClient) ServerDetails(identifier string, auth_token string, url string) (*Server, error) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||||
//Build GET Request
|
//Build GET Request
|
||||||
@@ -149,7 +156,7 @@ func ServerDetails(identifier string, auth_token string, url string) (*Server, e
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("Request successful", "Resp", resp)
|
logger.Info("Request successful", "Resp", resp.Status)
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user