Add functions for model POST API
Signed-off-by: Etzelia <etzelia@hotmail.com>model_post
parent
2bd753efa7
commit
017a921a4b
|
@ -1,11 +1,17 @@
|
||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
func ResponseGet(endpoint string) ([]byte, error) {
|
func ResponseGet(endpoint string) ([]byte, error) {
|
||||||
resp, err := http.Get(endpoint)
|
resp, err := http.Get(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -36,7 +42,17 @@ func ResponsePost(endpoint string, form url.Values) ([]byte, error) {
|
||||||
return body, err
|
return body, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Status struct {
|
func ResponseStatus(endpoint string, form url.Values) (*Status, error) {
|
||||||
Success bool `json:"success"`
|
resp, err := ResponsePost(endpoint, form)
|
||||||
Message string `json:"message"`
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
status := &Status{}
|
||||||
|
err = json.Unmarshal(resp, &status)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,12 @@ type Alert struct {
|
||||||
Seen bool `json:"seen"`
|
Seen bool `json:"seen"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) Alert(builder *django.Builder) ([]*Alert, error) {
|
func (q *Model) Alert(builder *django.Builder) ([]*Alert, error) {
|
||||||
endpoint := q.endpoint("alert", builder.QueryString())
|
endpoint := q.queryEndpoint("alert", builder.QueryString())
|
||||||
alerts := make([]*Alert, 0)
|
alerts := make([]*Alert, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -29,5 +31,14 @@ func (q *Model) Alert(builder *django.Builder) ([]*Alert, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, alert := range alerts {
|
||||||
|
alert.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return alerts, nil
|
return alerts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Alert) Save() (*internal.Status, error) {
|
||||||
|
endpoint := a.model.endpoint("alert")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(a))
|
||||||
|
}
|
||||||
|
|
|
@ -18,10 +18,12 @@ type Application struct {
|
||||||
Accepted bool `json:"accepted"`
|
Accepted bool `json:"accepted"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) Application(builder *django.Builder) ([]*Application, error) {
|
func (q *Model) Application(builder *django.Builder) ([]*Application, error) {
|
||||||
endpoint := q.endpoint("application", builder.QueryString())
|
endpoint := q.queryEndpoint("application", builder.QueryString())
|
||||||
applications := make([]*Application, 0)
|
applications := make([]*Application, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -34,5 +36,14 @@ func (q *Model) Application(builder *django.Builder) ([]*Application, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, application := range applications {
|
||||||
|
application.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return applications, nil
|
return applications, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Application) Save() (*internal.Status, error) {
|
||||||
|
endpoint := a.model.endpoint("application")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(a))
|
||||||
|
}
|
||||||
|
|
13
model/ip.go
13
model/ip.go
|
@ -11,10 +11,12 @@ type IP struct {
|
||||||
PlayerID int64 `json:"player_id"`
|
PlayerID int64 `json:"player_id"`
|
||||||
LastUsed string `json:"last_used"`
|
LastUsed string `json:"last_used"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) IP(builder *django.Builder) ([]*IP, error) {
|
func (q *Model) IP(builder *django.Builder) ([]*IP, error) {
|
||||||
endpoint := q.endpoint("ip", builder.QueryString())
|
endpoint := q.queryEndpoint("ip", builder.QueryString())
|
||||||
ips := make([]*IP, 0)
|
ips := make([]*IP, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -27,5 +29,14 @@ func (q *Model) IP(builder *django.Builder) ([]*IP, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, ip := range ips {
|
||||||
|
ip.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return ips, nil
|
return ips, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IP) Save() (*internal.Status, error) {
|
||||||
|
endpoint := i.model.endpoint("ip")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(i))
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package model
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go.etztech.xyz/go-mcm/model/django"
|
"go.etztech.xyz/go-mcm/model/django"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,9 +24,23 @@ func (q *Model) NewDjangoBuilder() *django.Builder {
|
||||||
return django.NewBuilder(q.Token)
|
return django.NewBuilder(q.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) endpoint(model, query string) string {
|
func (q *Model) endpoint(model string) string {
|
||||||
|
return fmt.Sprintf("%s/%s", q.URL, model)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Model) queryEndpoint(model, query string) string {
|
||||||
if !strings.HasPrefix(query, "?") {
|
if !strings.HasPrefix(query, "?") {
|
||||||
query = "?" + query
|
query = "?" + query
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s/%s%s", q.URL, model, query)
|
return fmt.Sprintf("%s%s", q.endpoint(model), query)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Values(i interface{}) url.Values {
|
||||||
|
values := url.Values{}
|
||||||
|
iVal := reflect.ValueOf(i).Elem()
|
||||||
|
typ := iVal.Type()
|
||||||
|
for i := 0; i < iVal.NumField(); i++ {
|
||||||
|
values.Set(strings.ToLower(typ.Field(i).Name), fmt.Sprint(iVal.Field(i)))
|
||||||
|
}
|
||||||
|
return values
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,10 +22,12 @@ type Note struct {
|
||||||
StaffID int64 `json:"staff_id"`
|
StaffID int64 `json:"staff_id"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) Note(builder *django.Builder) ([]*Note, error) {
|
func (q *Model) Note(builder *django.Builder) ([]*Note, error) {
|
||||||
endpoint := q.endpoint("note", builder.QueryString())
|
endpoint := q.queryEndpoint("note", builder.QueryString())
|
||||||
notes := make([]*Note, 0)
|
notes := make([]*Note, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -38,5 +40,14 @@ func (q *Model) Note(builder *django.Builder) ([]*Note, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, note := range notes {
|
||||||
|
note.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return notes, nil
|
return notes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Note) Save() (*internal.Status, error) {
|
||||||
|
endpoint := n.model.endpoint("note")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(n))
|
||||||
|
}
|
||||||
|
|
|
@ -15,10 +15,12 @@ type Player struct {
|
||||||
FirstSeen string `json:"first_seen"`
|
FirstSeen string `json:"first_seen"`
|
||||||
LastSeen string `json:"last_seen"`
|
LastSeen string `json:"last_seen"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) Player(builder *django.Builder) ([]*Player, error) {
|
func (q *Model) Player(builder *django.Builder) ([]*Player, error) {
|
||||||
endpoint := q.endpoint("player", builder.QueryString())
|
endpoint := q.queryEndpoint("player", builder.QueryString())
|
||||||
players := make([]*Player, 0)
|
players := make([]*Player, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -31,5 +33,14 @@ func (q *Model) Player(builder *django.Builder) ([]*Player, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, player := range players {
|
||||||
|
player.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return players, nil
|
return players, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Player) Save() (*internal.Status, error) {
|
||||||
|
endpoint := p.model.endpoint("player")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(p))
|
||||||
|
}
|
||||||
|
|
|
@ -35,10 +35,12 @@ type Ticket struct {
|
||||||
Z string `json:"Z"`
|
Z string `json:"Z"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) Ticket(builder *django.Builder) ([]*Ticket, error) {
|
func (q *Model) Ticket(builder *django.Builder) ([]*Ticket, error) {
|
||||||
endpoint := q.endpoint("ticket", builder.QueryString())
|
endpoint := q.queryEndpoint("ticket", builder.QueryString())
|
||||||
tickets := make([]*Ticket, 0)
|
tickets := make([]*Ticket, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -51,5 +53,14 @@ func (q *Model) Ticket(builder *django.Builder) ([]*Ticket, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, ticket := range tickets {
|
||||||
|
ticket.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return tickets, nil
|
return tickets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Ticket) Save() (*internal.Status, error) {
|
||||||
|
endpoint := t.model.endpoint("ticket")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(t))
|
||||||
|
}
|
||||||
|
|
|
@ -14,10 +14,12 @@ type TicketNote struct {
|
||||||
LastUpdate string `json:"last_update"`
|
LastUpdate string `json:"last_update"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
|
func (q *Model) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
|
||||||
endpoint := q.endpoint("ticketnote", builder.QueryString())
|
endpoint := q.queryEndpoint("ticketnote", builder.QueryString())
|
||||||
ticketNotes := make([]*TicketNote, 0)
|
ticketNotes := make([]*TicketNote, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -30,5 +32,14 @@ func (q *Model) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, ticketNote := range ticketNotes {
|
||||||
|
ticketNote.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return ticketNotes, nil
|
return ticketNotes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TicketNote) Save() (*internal.Status, error) {
|
||||||
|
endpoint := t.model.endpoint("ticketnote")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(t))
|
||||||
|
}
|
||||||
|
|
|
@ -35,10 +35,12 @@ type UserSettings struct {
|
||||||
ShowTimestampChat bool `json:"show_timestamp_chat"`
|
ShowTimestampChat bool `json:"show_timestamp_chat"`
|
||||||
LastIP string `json:"last_ip"`
|
LastIP string `json:"last_ip"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|
||||||
|
model *Model `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Model) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
|
func (q *Model) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
|
||||||
endpoint := q.endpoint("usersettings", builder.QueryString())
|
endpoint := q.queryEndpoint("usersettings", builder.QueryString())
|
||||||
userSettingss := make([]*UserSettings, 0)
|
userSettingss := make([]*UserSettings, 0)
|
||||||
|
|
||||||
resp, err := internal.ResponseGet(endpoint)
|
resp, err := internal.ResponseGet(endpoint)
|
||||||
|
@ -51,5 +53,14 @@ func (q *Model) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, userSetting := range userSettingss {
|
||||||
|
userSetting.model = q
|
||||||
|
}
|
||||||
|
|
||||||
return userSettingss, nil
|
return userSettingss, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserSettings) Save() (*internal.Status, error) {
|
||||||
|
endpoint := u.model.endpoint("usersettings")
|
||||||
|
return internal.ResponseStatus(endpoint, Values(u))
|
||||||
|
}
|
||||||
|
|
14
web/alert.go
14
web/alert.go
|
@ -1,14 +1,12 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"go.etztech.xyz/go-mcm/internal"
|
"go.etztech.xyz/go-mcm/internal"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (web *Web) Alert(message string, ping bool) (*internal.Status, error) {
|
func (web *Web) Alert(message string, ping bool) (*internal.Status, error) {
|
||||||
endpoint := web.endpoint("alert")
|
endpoint := web.endpoint("alert")
|
||||||
status := &internal.Status{}
|
|
||||||
|
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Add("api", web.Token)
|
form.Add("api", web.Token)
|
||||||
|
@ -17,15 +15,5 @@ func (web *Web) Alert(message string, ping bool) (*internal.Status, error) {
|
||||||
form.Add("ping", "True")
|
form.Add("ping", "True")
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := internal.ResponsePost(endpoint, form)
|
return internal.ResponseStatus(endpoint, form)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(resp, &status)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return status, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"go.etztech.xyz/go-mcm/internal"
|
"go.etztech.xyz/go-mcm/internal"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (web *Web) Discord(message string, ping bool) (*internal.Status, error) {
|
func (web *Web) Discord(message string, ping bool) (*internal.Status, error) {
|
||||||
endpoint := web.endpoint("discord")
|
endpoint := web.endpoint("discord")
|
||||||
status := &internal.Status{}
|
|
||||||
|
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Add("api", web.Token)
|
form.Add("api", web.Token)
|
||||||
|
@ -17,15 +15,5 @@ func (web *Web) Discord(message string, ping bool) (*internal.Status, error) {
|
||||||
form.Add("ping", "True")
|
form.Add("ping", "True")
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := internal.ResponsePost(endpoint, form)
|
return internal.ResponseStatus(endpoint, form)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(resp, &status)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return status, nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue