Add example and gql type definitions

master
Etzelia 2020-08-13 15:41:14 -05:00
parent 63f942fb9e
commit 4069709a59
No known key found for this signature in database
GPG Key ID: 3CAEB74806C4ADE5
6 changed files with 79 additions and 4 deletions

View File

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

2
ban.go
View File

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

View File

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

View File

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

View File

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

View File

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