Add example and gql type definitions
parent
63f942fb9e
commit
4069709a59
58
README.md
58
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)
|
2
ban.go
2
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)
|
||||
}
|
||||
|
|
17
graphql.go
17
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{
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
2
world.go
2
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue