From 4069709a59b2c90c4c3fcc68b0a1ae933d5a0b12 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 13 Aug 2020 15:41:14 -0500 Subject: [PATCH] Add example and gql type definitions --- README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ban.go | 2 +- graphql.go | 17 ++++++++++++++++ player.go | 2 +- plugin.go | 2 +- world.go | 2 +- 6 files changed, 79 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c6262c5..81d2e60 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,64 @@ Go SDK for [ServerAPI](https://git.etztech.xyz/Minecraft/ServerAPI). +## Example + +```go +package main + +import ( + "context" + "fmt" + "net/http" + + "go.etztech.xyz/go-serverapi" +) + +func main() { + // ServerAPI REST client + client := serverapi.NewClient("https://api.server.com/", &serverapi.ClientOptions{ + HTTP: http.DefaultClient, + Password: "", + }) + + // Use the REST API + players, err := client.Players() + if err != nil { + panic(err) + } + + for _, p := range players { + fmt.Println(p.Name) + } + + fmt.Println() + fmt.Println() + + // GraphQL Client + g := client.GraphQL() + r := client.GraphQLRequest(` +{ + players { + name + } +} +`) + + // Use the GraphQL API + data := struct { + // Uses serverapi.Players for the magic naming of JSON + Players serverapi.Players + }{} + if err := g.Run(context.Background(), r, &data); err != nil { + panic(err) + } + + for _, p := range data.Players { + fmt.Println(p.Name) + } +} +``` + ## License [MIT](LICENSE) \ No newline at end of file diff --git a/ban.go b/ban.go index b909cc6..68ae822 100644 --- a/ban.go +++ b/ban.go @@ -25,6 +25,6 @@ func (b *Ban) ExpirationTime() time.Time { } // Bans gets a list of Ban from a ServerAPI instance -func (c *Client) Bans() (bans []*Ban, err error) { +func (c *Client) Bans() (bans Ban, err error) { return bans, c.json(fmt.Sprintf("%s/bans", c.Endpoint), &bans) } diff --git a/graphql.go b/graphql.go index f810250..73aec67 100644 --- a/graphql.go +++ b/graphql.go @@ -6,6 +6,23 @@ import ( "go.jolheiser.com/gql" ) +/** +This block of type definitions is primarily for +easily creating composite structs for GraphQL queries +*/ + +// Bans is a list of Ban +type Bans []*Ban + +// Players is a list of Player +type Players []*Player + +// Plugins is a list of Plugin +type Plugins []*Plugin + +// Worlds is a list of World +type Worlds []*World + // 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{ diff --git a/player.go b/player.go index f5a6538..907ab6c 100644 --- a/player.go +++ b/player.go @@ -9,6 +9,6 @@ type Player struct { } // Players returns a list of Player from a ServerAPI instance -func (c *Client) Players() (players []*Player, err error) { +func (c *Client) Players() (players Players, err error) { return players, c.json(fmt.Sprintf("%s/players", c.Endpoint), &players) } diff --git a/plugin.go b/plugin.go index 7ac4f6f..2ec6539 100644 --- a/plugin.go +++ b/plugin.go @@ -11,6 +11,6 @@ type Plugin struct { } // Plugins returns a list of Plugin from a ServerAPI instance -func (c *Client) Plugins() (plugins []*Plugin, err error) { +func (c *Client) Plugins() (plugins Plugins, err error) { return plugins, c.json(fmt.Sprintf("%s/plugins", c.Endpoint), &plugins) } diff --git a/world.go b/world.go index 791deae..62e7bcd 100644 --- a/world.go +++ b/world.go @@ -52,6 +52,6 @@ func (c *Client) World(name string) (world *World, err error) { } // Worlds returns a list of World from a ServerAPI instance -func (c *Client) Worlds() (worlds []*World, err error) { +func (c *Client) Worlds() (worlds Worlds, err error) { return worlds, c.json(fmt.Sprintf("%s/worlds", c.Endpoint), &worlds) }