mirror of
https://github.com/Voronsky/pterogo.git
synced 2025-12-12 08:46:33 -05:00
Initial call and unit test
This commit is contained in:
90
pterogo.go
Normal file
90
pterogo.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package pterogo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type PteroResp struct {
|
||||
Object string `json:"object, omitempty"`
|
||||
Data []PteroData `json:"data, omitempty"`
|
||||
}
|
||||
|
||||
type PteroData struct {
|
||||
Object string `json:"object, omitempty"`
|
||||
Attributes Attributes `json:"attributes, omitempty"`
|
||||
}
|
||||
|
||||
type Attributes struct {
|
||||
Name string `json:"name, omitempty"`
|
||||
Identifier string `json:"identifier, omitempty"`
|
||||
Description string `json:"description, omitempty"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Name string
|
||||
Description string
|
||||
}
|
||||
|
||||
// Grabs the list of servers from Pterodactyl
|
||||
// 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
|
||||
// A Bearer Auth token is required
|
||||
func listServers(auth_token string) (map[string]Server, error) {
|
||||
client := &http.Client{}
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
//Build GET Request
|
||||
req, err := http.NewRequest("GET", "https://panel.12egc.com/api/client", nil)
|
||||
if err != nil {
|
||||
slog.Error("Failed to make a new request", "Error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Add Pterodactyl Headers
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", "Bearer "+auth_token)
|
||||
|
||||
//Issue GET request
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
slog.Error("An error occurred trying to issue the request", "Error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 300 {
|
||||
// Create custom error for this
|
||||
err := errors.New("request failed")
|
||||
slog.Error("Non-200 status code was returned", "StatusCode", resp.StatusCode)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Info("Request successful", "Resp", resp)
|
||||
|
||||
servers := map[string]Server{}
|
||||
r := PteroResp{}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
slog.Error("Failed to read body", "Error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Decode the JSON body into the appropriate interface
|
||||
json.Unmarshal(body, &r)
|
||||
slog.Info("listServers()", "pteroResp", r)
|
||||
for i := 0; i < len(r.Data); i++ {
|
||||
attrs := r.Data[i]
|
||||
servers[attrs.Attributes.Identifier] = Server{attrs.Attributes.Name, attrs.Attributes.Description}
|
||||
//slog.Info("Server identifier", "Server=", attrs.Attributes.Identifier)
|
||||
logger.Info("Server identifer", "Server=", attrs.Attributes.Identifier)
|
||||
}
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
25
pterogo_test.go
Normal file
25
pterogo_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package pterogo
|
||||
|
||||
import (
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func TestListServers(t *testing.T) {
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
//s, err := listServers()
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatalf(`No env file found`)
|
||||
}
|
||||
bearer_auth_token := os.Getenv("PTERO_API_KEY")
|
||||
s, err := listServers(bearer_auth_token)
|
||||
if err != nil {
|
||||
log.Fatalf(`ListServers() = %q, %v, want nil, error`, s, err)
|
||||
}
|
||||
logger.Info("Servers queried", "Servers", s)
|
||||
}
|
||||
Reference in New Issue
Block a user