model_post
Etzelia 2019-10-03 22:22:22 +02:00 committed by Gitea
parent bfe77e9da6
commit 3ce698c397
17 changed files with 266 additions and 41 deletions

View File

@ -8,15 +8,17 @@ package main
import (
"fmt"
"go.etztech.xyz/go-mcm"
"go.etztech.xyz/go-mcm/model"
"go.etztech.xyz/go-mcm/model/django"
)
func main() {
query := model.NewQuery("<token>", "https://24carrotcraft.com/whitelist/api")
builder := query.NewDjangoBuilder().Contains(django.PlayerUsername, "Etzelia")
client := mcm.NewMCM("<token>", "https://24carrotcraft.com/whitelist/api")
model := client.NewModel()
builder := model.NewDjangoBuilder().Contains(django.PlayerUsername, "Etzelia")
players, err := query.Player(builder)
players, err := model.Player(builder)
if err != nil {
fmt.Println(err)
}

42
internal/common.go 100644
View File

@ -0,0 +1,42 @@
package internal
import (
"io/ioutil"
"net/http"
"net/url"
)
func ResponseGet(endpoint string) ([]byte, error) {
resp, err := http.Get(endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
func ResponsePost(endpoint string, form url.Values) ([]byte, error) {
resp, err := http.PostForm(endpoint, form)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
type Status struct {
Success bool `json:"success"`
Message string `json:"message"`
}

34
mcm.go 100644
View File

@ -0,0 +1,34 @@
package mcm
import (
"fmt"
"go.etztech.xyz/go-mcm/model"
"go.etztech.xyz/go-mcm/web"
"strings"
)
type MCM struct {
Token string
URL string
}
func NewMCM(token, baseURL string) *MCM {
return &MCM{
Token: token,
URL: strings.TrimRight(baseURL, "/"),
}
}
func (mcm *MCM) NewModel() *model.Model {
return &model.Model{
Token: mcm.Token,
URL: fmt.Sprintf("%s/model", strings.TrimRight(mcm.URL, "/")),
}
}
func (mcm *MCM) NewWeb() *web.Web {
return &web.Web{
Token: mcm.Token,
URL: fmt.Sprintf("%s/web", strings.TrimRight(mcm.URL, "/")),
}
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -14,11 +15,11 @@ type Alert struct {
Link string `json:"link"`
}
func (q *Query) Alert(builder *django.Builder) ([]*Alert, error) {
func (q *Model) Alert(builder *django.Builder) ([]*Alert, error) {
endpoint := q.endpoint("alert", builder.QueryString())
alerts := make([]*Alert, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -19,11 +20,11 @@ type Application struct {
Link string `json:"link"`
}
func (q *Query) Application(builder *django.Builder) ([]*Application, error) {
func (q *Model) Application(builder *django.Builder) ([]*Application, error) {
endpoint := q.endpoint("application", builder.QueryString())
applications := make([]*Application, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -12,11 +13,11 @@ type IP struct {
Link string `json:"link"`
}
func (q *Query) IP(builder *django.Builder) ([]*IP, error) {
func (q *Model) IP(builder *django.Builder) ([]*IP, error) {
endpoint := q.endpoint("ip", builder.QueryString())
ips := make([]*IP, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

View File

@ -3,45 +3,28 @@ package model
import (
"fmt"
"go.etztech.xyz/go-mcm/model/django"
"io/ioutil"
"net/http"
"strings"
)
type Query struct {
type Model struct {
Token string
URL string
}
func NewQuery(token, baseURL string) *Query {
return &Query{
func NewModel(token, baseURL string) *Model {
return &Model{
Token: token,
URL: fmt.Sprintf("%s/model", strings.TrimRight(baseURL, "/")),
}
}
func (q *Query) NewDjangoBuilder() *django.Builder {
func (q *Model) NewDjangoBuilder() *django.Builder {
return django.NewBuilder(q.Token)
}
func (q *Query) endpoint(model, query string) string {
func (q *Model) endpoint(model, query string) string {
if !strings.HasPrefix(query, "?") {
query = "?" + query
}
return fmt.Sprintf("%s/%s%s", q.URL, model, query)
}
func response(endpoint string) ([]byte, error) {
resp, err := http.Get(endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -23,11 +24,11 @@ type Note struct {
Link string `json:"link"`
}
func (q *Query) Note(builder *django.Builder) ([]*Note, error) {
func (q *Model) Note(builder *django.Builder) ([]*Note, error) {
endpoint := q.endpoint("note", builder.QueryString())
notes := make([]*Note, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -16,11 +17,11 @@ type Player struct {
Link string `json:"link"`
}
func (q *Query) Player(builder *django.Builder) ([]*Player, error) {
func (q *Model) Player(builder *django.Builder) ([]*Player, error) {
endpoint := q.endpoint("player", builder.QueryString())
players := make([]*Player, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -36,11 +37,11 @@ type Ticket struct {
Link string `json:"link"`
}
func (q *Query) Ticket(builder *django.Builder) ([]*Ticket, error) {
func (q *Model) Ticket(builder *django.Builder) ([]*Ticket, error) {
endpoint := q.endpoint("ticket", builder.QueryString())
tickets := make([]*Ticket, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -15,11 +16,11 @@ type TicketNote struct {
Link string `json:"link"`
}
func (q *Query) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
func (q *Model) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
endpoint := q.endpoint("ticketnote", builder.QueryString())
ticketNotes := make([]*TicketNote, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"go.etztech.xyz/go-mcm/model/django"
)
@ -36,11 +37,11 @@ type UserSettings struct {
Link string `json:"link"`
}
func (q *Query) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
func (q *Model) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
endpoint := q.endpoint("usersettings", builder.QueryString())
userSettingss := make([]*UserSettings, 0)
resp, err := response(endpoint)
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}

31
web/alert.go 100644
View File

@ -0,0 +1,31 @@
package web
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"net/url"
)
func (web *Web) Alert(message string, ping bool) (*internal.Status, error) {
endpoint := web.endpoint("alert")
status := &internal.Status{}
form := url.Values{}
form.Add("api", web.Token)
form.Add("message", message)
if ping {
form.Add("ping", "True")
}
resp, err := internal.ResponsePost(endpoint, form)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &status)
if err != nil {
return nil, err
}
return status, nil
}

31
web/discord.go 100644
View File

@ -0,0 +1,31 @@
package web
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"net/url"
)
func (web *Web) Discord(message string, ping bool) (*internal.Status, error) {
endpoint := web.endpoint("discord")
status := &internal.Status{}
form := url.Values{}
form.Add("api", web.Token)
form.Add("message", message)
if ping {
form.Add("ping", "True")
}
resp, err := internal.ResponsePost(endpoint, form)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &status)
if err != nil {
return nil, err
}
return status, nil
}

38
web/log.go 100644
View File

@ -0,0 +1,38 @@
package web
import (
"encoding/json"
"fmt"
"go.etztech.xyz/go-mcm/internal"
)
type Log struct {
Chats Chats `json:"chats"`
}
type Chats struct {
Global []Entry `json:"global"`
Staff []Entry `json:"staff"`
}
type Entry struct {
Date string `json:"date"`
Text string `json:"text"`
}
func (web *Web) Log() (*Log, error) {
endpoint := fmt.Sprintf("%s?api=%s", web.endpoint("log"), web.Token)
log := &Log{}
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &log)
if err != nil {
return nil, err
}
return log, nil
}

34
web/online.go 100644
View File

@ -0,0 +1,34 @@
package web
import (
"encoding/json"
"fmt"
"go.etztech.xyz/go-mcm/internal"
)
type Online struct {
Query Query `json:"query"`
}
type Query struct {
Max string `json:"max"`
Online string `json:"online"`
Players []string `json:"players"`
}
func (web *Web) Online() (*Online, error) {
endpoint := fmt.Sprintf("%s?api=%s", web.endpoint("online"), web.Token)
online := &Online{}
resp, err := internal.ResponseGet(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &online)
if err != nil {
return nil, err
}
return online, nil
}

22
web/web.go 100644
View File

@ -0,0 +1,22 @@
package web
import (
"fmt"
"strings"
)
type Web struct {
Token string
URL string
}
func NewWeb(token, baseURL string) *Web {
return &Web{
Token: token,
URL: fmt.Sprintf("%s/web", strings.TrimRight(baseURL, "/")),
}
}
func (web *Web) endpoint(endpoint string) string {
return fmt.Sprintf("%s/%s/", web.URL, endpoint)
}