commit 63f942fb9e052da6c8103c07fd5a690222a0f643 Author: Etzelia Date: Thu Aug 13 15:14:25 2020 -0500 Initial Commit Signed-off-by: Etzelia diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a453954 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# GoLand +.idea/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..82b4eb1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Etzelia + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..27f3cc1 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +GO ?= go + +.PHONY: fmt +fmt: + $(GO) fmt ./... + +.PHONY: test +test: + $(GO) test --race ./... + +.PHONY: vet +vet: + $(GO) vet ./... \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c6262c5 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# go-serverapi + +Go SDK for [ServerAPI](https://git.etztech.xyz/Minecraft/ServerAPI). + +## License + +[MIT](LICENSE) \ No newline at end of file diff --git a/ban.go b/ban.go new file mode 100644 index 0000000..b909cc6 --- /dev/null +++ b/ban.go @@ -0,0 +1,30 @@ +package serverapi + +import ( + "fmt" + "time" +) + +// Ban is a Minecraft ban +type Ban struct { + Target string `json:"target"` + Source string `jso:"source"` + Reason string `json:"reason"` + Created int64 `json:"created"` + Expiration int64 `json:"expiration"` +} + +// CreatedTime is Created converted to a time.Time +func (b *Ban) CreatedTime() time.Time { + return time.Unix(b.Created/1000, 0) +} + +// ExpirationTime is Expiration converted to a time.Time +func (b *Ban) ExpirationTime() time.Time { + return time.Unix(b.Expiration/1000, 0) +} + +// Bans gets a list of Ban from a ServerAPI instance +func (c *Client) Bans() (bans []*Ban, err error) { + return bans, c.json(fmt.Sprintf("%s/bans", c.Endpoint), &bans) +} diff --git a/client.go b/client.go new file mode 100644 index 0000000..28bbd13 --- /dev/null +++ b/client.go @@ -0,0 +1,66 @@ +package serverapi + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" +) + +// Client is a client aimed at a specific ServerAPI endpoint +type Client struct { + Endpoint string + Options *ClientOptions +} + +// ClientOptions are options that can be set for a Client +type ClientOptions struct { + HTTP *http.Client + Password string +} + +var ( + // DefaultOptions are the default set of ClientOptions for a Client + DefaultOptions = &ClientOptions{ + HTTP: http.DefaultClient, + } + + defaultHeader = http.Header{ + "Content-Type": []string{"application/json; charset=utf-8"}, + "Accept": []string{"application/json; charset=utf-8"}, + } +) + +// NewClient returns a new Client for making ServerAPI requests +func NewClient(endpoint string, options *ClientOptions) *Client { + if options == nil { + options = DefaultOptions + } + endpoint = strings.TrimSuffix(endpoint, "/") + return &Client{ + Endpoint: endpoint, + Options: options, + } +} + +func (c *Client) json(endpoint string, obj interface{}) error { + req, err := http.NewRequest(http.MethodGet, endpoint, nil) + if err != nil { + return fmt.Errorf("new JSON request: %v", err) + } + req.Header = defaultHeader + req.Header.Add("X-ServerAPI-Password", c.Options.Password) + + res, err := c.Options.HTTP.Do(req) + if err != nil { + return fmt.Errorf("sending request: %v", err) + } + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return fmt.Errorf("reading body: %v", err) + } + + return json.Unmarshal(body, obj) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a5d726b --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go.etztech.xyz/go-serverapi + +go 1.15 + +require go.jolheiser.com/gql v0.0.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2c64d18 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +go.jolheiser.com/gql v0.0.1 h1:y3LGHcJUZI9otTCcMn8TVdF3aEzNX0FW6m0YUamlLto= +go.jolheiser.com/gql v0.0.1/go.mod h1:74eYqVRIxsOFxtVl0RYGKNyYQgJYQaxOCgar7LP71Hw= diff --git a/graphql.go b/graphql.go new file mode 100644 index 0000000..f810250 --- /dev/null +++ b/graphql.go @@ -0,0 +1,21 @@ +package serverapi + +import ( + "fmt" + + "go.jolheiser.com/gql" +) + +// GraphQL returns a gql.Client for a ServerAPI instance +func (c *Client) GraphQL() *gql.Client { + return gql.NewClient(fmt.Sprintf("%s/graphql", c.Endpoint), &gql.ClientOptions{ + HTTP: c.Options.HTTP, + }) +} + +// GraphQLRequest returns a pre-filled gql.Request with the password set +func (c *Client) GraphQLRequest(query string) *gql.Request { + req := gql.NewRequest(query) + req.Header.Add("X-ServerAPI-Password", c.Options.Password) + return req +} diff --git a/ping.go b/ping.go new file mode 100644 index 0000000..620528f --- /dev/null +++ b/ping.go @@ -0,0 +1,17 @@ +package serverapi + +import "fmt" + +// Ping is a Minecraft ping query +type Ping struct { + CurrentPlayers int `json:"current_players"` + MaxPlayers int `json:"max_players"` + Type string `json:"type"` + Version string `json:"version"` + MOTD string `json:"motd"` +} + +// Ping returns a Ping +func (c *Client) Ping() (ping *Ping, err error) { + return ping, c.json(fmt.Sprintf("%s/ping", c.Endpoint), &ping) +} diff --git a/player.go b/player.go new file mode 100644 index 0000000..f5a6538 --- /dev/null +++ b/player.go @@ -0,0 +1,14 @@ +package serverapi + +import "fmt" + +// Player is a Minecraft player +type Player struct { + Name string `json:"name"` + UUID string `json:"uuid"` +} + +// Players returns a list of Player from a ServerAPI instance +func (c *Client) Players() (players []*Player, err error) { + return players, c.json(fmt.Sprintf("%s/players", c.Endpoint), &players) +} diff --git a/plugin.go b/plugin.go new file mode 100644 index 0000000..7ac4f6f --- /dev/null +++ b/plugin.go @@ -0,0 +1,16 @@ +package serverapi + +import "fmt" + +// Plugin is a Minecraft plugin +type Plugin struct { + Name string `json:"name"` + Version string `json:"version"` + Website string `json:"website"` + Authors []string `json:"authors"` +} + +// Plugins returns a list of Plugin from a ServerAPI instance +func (c *Client) Plugins() (plugins []*Plugin, err error) { + return plugins, c.json(fmt.Sprintf("%s/plugins", c.Endpoint), &plugins) +} diff --git a/tps.go b/tps.go new file mode 100644 index 0000000..e65329f --- /dev/null +++ b/tps.go @@ -0,0 +1,14 @@ +package serverapi + +import "fmt" + +// TPS is a Minecraft server's relative performance +type TPS struct { + Average float64 `json:"average"` + History []float64 `json:"history"` +} + +// TPS returns TPS from a ServerAPI instance +func (c *Client) TPS() (tps *TPS, err error) { + return tps, c.json(fmt.Sprintf("%s/tps", c.Endpoint), &tps) +} diff --git a/world.go b/world.go new file mode 100644 index 0000000..791deae --- /dev/null +++ b/world.go @@ -0,0 +1,57 @@ +package serverapi + +import "fmt" + +// Weather described the weather of a Minecraft World +type Weather int + +const ( + // Clear weather + WeatherClear Weather = iota + // Raining/Snowing + WeatherStorm + // Thundering + WeatherThunder +) + +// String is human-friendly Weather +func (w Weather) String() string { + switch w { + case WeatherClear: + return "clear" + case WeatherStorm: + return "storm" + case WeatherThunder: + return "thunder" + default: + return "unknown" + } +} + +// World is a Minecraft world +type World struct { + Name string `json:"name"` + Weather Weather `json:"weather"` + Time int64 `json:"time"` + FullTime int64 `json:"full_time"` +} + +// TimeHours returns Time in hours +func (w *World) TimeHours() int64 { + return w.Time / 1000 +} + +// FullTimeHours returns FullTime in hours +func (w *World) FullTimeHours() int64 { + return w.FullTime / 1000 +} + +// World returns a single World from a ServerAPI instance +func (c *Client) World(name string) (world *World, err error) { + return world, c.json(fmt.Sprintf("%s/worlds/%s", c.Endpoint, name), &world) +} + +// Worlds returns a list of World from a ServerAPI instance +func (c *Client) Worlds() (worlds []*World, err error) { + return worlds, c.json(fmt.Sprintf("%s/worlds", c.Endpoint), &worlds) +}