Initial Release (#1)

model_post
Etzelia 2019-10-02 21:03:23 +02:00 committed by Gitea
parent 88e679415e
commit 24de43986f
21 changed files with 675 additions and 1 deletions

2
.gitignore vendored 100644
View File

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

View File

@ -1,3 +1,51 @@
# go-mcm # go-mcm
Go library for MCM Go library for MCM
## Model Example
```go
package main
import (
"fmt"
"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")
players, err := query.Player(builder)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%#v\n", players[0])
}
```
Response (formatted)
```go
&model.Player{
ID: 663,
AuthUserID: 0,
UUID: "de10775b-fcb1-4665-bce4-5924b2e30188",
Username: "MrsEtzelia",
ApplicationID: 1,
FirstSeen: "2016-12-18",
LastSeen: "2017-11-23",
Link: "https://www.24carrotcraft.com/whitelist/dashboard/player/663",
}
&model.Player{
ID: 724,
AuthUserID: 1,
UUID: "bf0446a8-9695-4c41-aa4c-7ff45bfd1171",
Username: "Etzelia",
ApplicationID: 838,
FirstSeen: "2016-12-02",
LastSeen: "2019-10-02",
Link: "https://www.24carrotcraft.com/whitelist/dashboard/player/724",
}
```

3
go.mod 100644
View File

@ -0,0 +1,3 @@
module go.etztech.xyz/go-mcm
go 1.13

32
model/alert.go 100644
View File

@ -0,0 +1,32 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type Alert struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Message string `json:"message"`
Seen bool `json:"seen"`
Date string `json:"date"`
Link string `json:"link"`
}
func (q *Query) Alert(builder *django.Builder) ([]*Alert, error) {
endpoint := q.endpoint("alert", builder.QueryString())
alerts := make([]*Alert, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &alerts)
if err != nil {
return nil, err
}
return alerts, nil
}

View File

@ -0,0 +1,37 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type Application struct {
ID int64 `json:"id"`
Username string `json:"username"`
Age int64 `json:"age"`
PlayerType string `json:"player_type"`
EverBanned bool `json:"ever_banned"`
EverBannedExplanation string `json:"ever_banned_explanation"`
Reference string `json:"reference"`
ReadRules string `json:"read_rules"`
Accepted bool `json:"accepted"`
Date string `json:"date"`
Link string `json:"link"`
}
func (q *Query) Application(builder *django.Builder) ([]*Application, error) {
endpoint := q.endpoint("application", builder.QueryString())
applications := make([]*Application, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &applications)
if err != nil {
return nil, err
}
return applications, nil
}

View File

@ -0,0 +1,10 @@
package django
const (
AlertID = ID
AlertUserID = "user_id"
AlertMessage = "message"
AlertSeen = "seen"
AlertDate = "date"
AlertLink = Link
)

View File

@ -0,0 +1,15 @@
package django
const (
ApplicationID = ID
ApplicationUsername = "username"
ApplicationAge = "age"
ApplicationPlayerType = "player_type"
ApplicationEverBanned = "ever_banned"
ApplicationEverBannedExplanation = "ever_banned_explanation"
ApplicationReference = "reference"
ApplicationReadRules = "read_rules"
ApplicationAccepted = "accepted"
ApplicationDate = "date"
ApplicationLink = Link
)

View File

@ -0,0 +1,163 @@
package django
import (
"fmt"
"strings"
)
const (
ID = "id"
Link = "link"
)
type Builder struct {
Params []string
}
func NewBuilder(token string) *Builder {
return &Builder{[]string{fmt.Sprintf("api=%s", token)}}
}
func (db *Builder) QueryString() string {
return "?" + strings.Join(db.Params, "&")
}
func (db *Builder) Exact(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__exact=%s", field, value))
return db
}
func (db *Builder) IExact(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iexact=%s", field, value))
return db
}
func (db *Builder) Contains(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__contains=%s", field, value))
return db
}
func (db *Builder) IContains(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__icontains=%s", field, value))
return db
}
func (db *Builder) In(field string, values ...string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__in=[%s]", field, strings.Join(values, ",")))
return db
}
func (db *Builder) Gt(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__gt=%s", field, value))
return db
}
func (db *Builder) Gte(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__gte=%s", field, value))
return db
}
func (db *Builder) Lt(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__lt=%s", field, value))
return db
}
func (db *Builder) Lte(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__lte=%s", field, value))
return db
}
func (db *Builder) Startswith(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__startswith=%s", field, value))
return db
}
func (db *Builder) IStartswith(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__istartswith=%s", field, value))
return db
}
func (db *Builder) Endswith(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__endswith=%s", field, value))
return db
}
func (db *Builder) IEndswith(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iendswith=%s", field, value))
return db
}
func (db *Builder) Range(field, from, to string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__range=(%s,%s)", field, from, to))
return db
}
func (db *Builder) Date(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__date=%s", field, value))
return db
}
func (db *Builder) Year(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__year=%s", field, value))
return db
}
func (db *Builder) Month(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__month=%s", field, value))
return db
}
func (db *Builder) Day(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__day=%s", field, value))
return db
}
func (db *Builder) Week(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__week=%s", field, value))
return db
}
func (db *Builder) WeekDay(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__week_day=%s", field, value))
return db
}
func (db *Builder) Quarter(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__quarter=%s", field, value))
return db
}
func (db *Builder) Time(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__time=%s", field, value))
return db
}
func (db *Builder) Hour(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__hour=%s", field, value))
return db
}
func (db *Builder) Minute(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__minute=%s", field, value))
return db
}
func (db *Builder) Second(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__second=%s", field, value))
return db
}
func (db *Builder) IsNull(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__isnull=%s", field, value))
return db
}
func (db *Builder) Regex(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__regex=r'%s'", field, value))
return db
}
func (db *Builder) IRegex(field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iregex=r'%s'", field, value))
return db
}

View File

@ -0,0 +1,8 @@
package django
const (
IPID = ID
IPPlayerID = "player_id"
IPLastUsed = "last_used"
IPLink = Link
)

View File

@ -0,0 +1,11 @@
package django
const (
NoteID = ID
NotePlayerID = "player_id"
NoteMessage = "message"
NoteImportance = "importance"
NoteStaffID = "staff_id"
NoteDate = "date"
NoteLink = Link
)

View File

@ -0,0 +1,12 @@
package django
const (
PlayerID = ID
PlayerAuthUserID = "auth_user_id"
PlayerUUID = "uuid"
PlayerUsername = "username"
PlayerApplicationID = "application_id"
PlayerFirstSeen = "first_seen"
PlayerLastSeen = "last_seen"
PlayerLink = Link
)

View File

@ -0,0 +1,16 @@
package django
const (
TicketID = ID
TicketPlayerID = "player_id"
TicketMessage = "message"
TicketPriority = "priority"
TicketStaffID = "staff_id"
TicketResolved = "resolved"
TicketWorld = "world"
TicketX = "x"
TicketY = "Y"
TicketZ = "Z"
TicketDate = "date"
TicketLink = Link
)

View File

@ -0,0 +1,11 @@
package django
const (
TicketNoteID = ID
TicketNoteAuthorID = "author_id"
TicketNoteTicketID = "ticket_id"
TicketNoteMessage = "message"
TicketNoteLastUpdate = "last_update"
TicketNoteDate = "date"
TicketNoteLink = Link
)

View File

@ -0,0 +1,13 @@
package django
const (
UserSettingsID = ID
UserSettingsAuthUserID = "auth_user_id"
UserSettingsDefaultResults = "default_results"
UserSettingsDefaultTheme = "default_theme"
UserSettingsDefaultTimezone = "default_timezone"
UserSettingsSearchPlayerIP = "search_player_ip"
UserSettingsShowTimestampChat = "show_timestamp_chat"
UserSettingsLastIP = "last_ip"
UserSettingsLink = Link
)

30
model/ip.go 100644
View File

@ -0,0 +1,30 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type IP struct {
ID int64 `json:"id"`
PlayerID int64 `json:"player_id"`
LastUsed string `json:"last_used"`
Link string `json:"link"`
}
func (q *Query) IP(builder *django.Builder) ([]*IP, error) {
endpoint := q.endpoint("ip", builder.QueryString())
ips := make([]*IP, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &ips)
if err != nil {
return nil, err
}
return ips, nil
}

47
model/model.go 100644
View File

@ -0,0 +1,47 @@
package model
import (
"fmt"
"go.etztech.xyz/go-mcm/model/django"
"io/ioutil"
"net/http"
"strings"
)
type Query struct {
Token string
URL string
}
func NewQuery(token, baseURL string) *Query {
return &Query{
Token: token,
URL: fmt.Sprintf("%s/model", strings.TrimRight(baseURL, "/")),
}
}
func (q *Query) NewDjangoBuilder() *django.Builder {
return django.NewBuilder(q.Token)
}
func (q *Query) 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
}

41
model/note.go 100644
View File

@ -0,0 +1,41 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type Importance string
const (
ImportanceLow Importance = "L"
ImportanceMedium Importance = "M"
ImportanceHigh Importance = "H"
)
type Note struct {
ID int64 `json:"id"`
PlayerID int64 `json:"player_id"`
Message string `json:"message"`
Importance Importance `json:"importance"`
StaffID int64 `json:"staff_id"`
Date string `json:"date"`
Link string `json:"link"`
}
func (q *Query) Note(builder *django.Builder) ([]*Note, error) {
endpoint := q.endpoint("note", builder.QueryString())
notes := make([]*Note, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &notes)
if err != nil {
return nil, err
}
return notes, nil
}

34
model/player.go 100644
View File

@ -0,0 +1,34 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type Player struct {
ID int64 `json:"id"`
AuthUserID int64 `json:"auth_user_id"`
UUID string `json:"uuid"`
Username string `json:"username"`
ApplicationID int64 `json:"application_id"`
FirstSeen string `json:"first_seen"`
LastSeen string `json:"last_seen"`
Link string `json:"link"`
}
func (q *Query) Player(builder *django.Builder) ([]*Player, error) {
endpoint := q.endpoint("player", builder.QueryString())
players := make([]*Player, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &players)
if err != nil {
return nil, err
}
return players, nil
}

54
model/ticket.go 100644
View File

@ -0,0 +1,54 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type Priority string
const (
PriorityLow Priority = "L"
PriorityMedium Priority = "M"
PriorityHigh Priority = "H"
)
type World string
const (
WorldOverworld World = "O"
WorldNether World = "N"
WorldTheEnd World = "E"
)
type Ticket struct {
ID int64 `json:"id"`
PlayerID int64 `json:"player_id"`
Message string `json:"message"`
Priority Priority `json:"priority"`
StaffID int64 `json:"staff_id"`
Resolved bool `json:"resolved"`
World World `json:"world"`
X string `json:"x"`
Y string `json:"Y"`
Z string `json:"Z"`
Date string `json:"date"`
Link string `json:"link"`
}
func (q *Query) Ticket(builder *django.Builder) ([]*Ticket, error) {
endpoint := q.endpoint("ticket", builder.QueryString())
tickets := make([]*Ticket, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &tickets)
if err != nil {
return nil, err
}
return tickets, nil
}

View File

@ -0,0 +1,33 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type TicketNote struct {
ID int64 `json:"id"`
AuthorID int64 `json:"author_id"`
TicketID int64 `json:"ticket_id"`
Message string `json:"message"`
LastUpdate string `json:"last_update"`
Date string `json:"date"`
Link string `json:"link"`
}
func (q *Query) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
endpoint := q.endpoint("ticketnote", builder.QueryString())
ticketNotes := make([]*TicketNote, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &ticketNotes)
if err != nil {
return nil, err
}
return ticketNotes, nil
}

View File

@ -0,0 +1,54 @@
package model
import (
"encoding/json"
"go.etztech.xyz/go-mcm/model/django"
)
type Results int64
const (
Results10 Results = 10
Results25 Results = 25
Results50 Results = 50
Results100 Results = 100
ResultsAll Results = -1
)
type Theme string
const (
ThemesDefault Theme = "DE"
ThemesDark Theme = "DA"
ThemesSolar Theme = "SO"
ThemesSlate Theme = "SL"
)
type UserSettings struct {
ID int64 `json:"id"`
AuthUserID int64 `json:"auth_user_id"`
DefaultResults Results `json:"default_results"`
DefaultTheme Theme `json:"default_theme"`
DefaultTimezone string `json:"default_timezone"`
SearchPlayerIP bool `json:"search_player_ip"`
ShowTimestampChat bool `json:"show_timestamp_chat"`
LastIP string `json:"last_ip"`
Link string `json:"link"`
}
func (q *Query) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
endpoint := q.endpoint("usersettings", builder.QueryString())
userSettingss := make([]*UserSettings, 0)
resp, err := response(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &userSettingss)
if err != nil {
return nil, err
}
return userSettingss, nil
}