Initial Commit

Signed-off-by: Etzelia <etzelia@hotmail.com>
master
Etzelia 2020-08-13 15:14:25 -05:00
commit 63f942fb9e
No known key found for this signature in database
GPG Key ID: 3CAEB74806C4ADE5
14 changed files with 271 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
# GoLand
.idea/

7
LICENSE 100644
View File

@ -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.

13
Makefile 100644
View File

@ -0,0 +1,13 @@
GO ?= go
.PHONY: fmt
fmt:
$(GO) fmt ./...
.PHONY: test
test:
$(GO) test --race ./...
.PHONY: vet
vet:
$(GO) vet ./...

7
README.md 100644
View File

@ -0,0 +1,7 @@
# go-serverapi
Go SDK for [ServerAPI](https://git.etztech.xyz/Minecraft/ServerAPI).
## License
[MIT](LICENSE)

30
ban.go 100644
View File

@ -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)
}

66
client.go 100644
View File

@ -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)
}

5
go.mod 100644
View File

@ -0,0 +1,5 @@
module go.etztech.xyz/go-serverapi
go 1.15
require go.jolheiser.com/gql v0.0.1

2
go.sum 100644
View File

@ -0,0 +1,2 @@
go.jolheiser.com/gql v0.0.1 h1:y3LGHcJUZI9otTCcMn8TVdF3aEzNX0FW6m0YUamlLto=
go.jolheiser.com/gql v0.0.1/go.mod h1:74eYqVRIxsOFxtVl0RYGKNyYQgJYQaxOCgar7LP71Hw=

21
graphql.go 100644
View File

@ -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
}

17
ping.go 100644
View File

@ -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)
}

14
player.go 100644
View File

@ -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)
}

16
plugin.go 100644
View File

@ -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)
}

14
tps.go 100644
View File

@ -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)
}

57
world.go 100644
View File

@ -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)
}