58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package berkutschi
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/monaco-io/request"
|
|
"github.com/rs/zerolog/log"
|
|
"time"
|
|
)
|
|
|
|
var pollURL = "https://live.berkutschi.com/events/"
|
|
|
|
func Poll(event int) (PollData, error) {
|
|
ctx, _ := context.WithTimeout(context.Background(), 15*time.Second)
|
|
c := request.Client{
|
|
Context: ctx,
|
|
URL: fmt.Sprintf("%s%d.json", pollURL, event),
|
|
Method: "GET",
|
|
}
|
|
data := new(PollData)
|
|
resp := c.Send().Scan(data)
|
|
if !resp.OK() {
|
|
return PollData{}, resp.Error()
|
|
}
|
|
return *data, nil
|
|
}
|
|
|
|
func (b *Berkutschi) registerClient() error {
|
|
var body = struct {
|
|
Channel string `json:"channel"`
|
|
ID string `json:"id"`
|
|
SupportedConnectionTypes []string `json:"supportedConnectionTypes"`
|
|
Version string `json:"version"`
|
|
}{
|
|
Channel: "/meta/handshake",
|
|
Version: "1.0",
|
|
ID: "1",
|
|
SupportedConnectionTypes: []string{"websocket"},
|
|
}
|
|
var result []BerkutschiClientRegisterResponse
|
|
|
|
c := request.Client{
|
|
URL: "https://live.berkutschi.com/faye",
|
|
Method: "POST",
|
|
JSON: body,
|
|
}
|
|
resp := c.Send().Scan(&result)
|
|
if !resp.OK() {
|
|
// handle error
|
|
log.Error().Err(fmt.Errorf("%v", resp.Error())).Fields(struct{ Event int }{Event: b.event}).Send()
|
|
return resp.Error()
|
|
}
|
|
|
|
b.log.Debug().Msgf("%+v", result)
|
|
b.clientID = result[0].ClientID
|
|
//TODO check length of result
|
|
return nil
|
|
}
|