You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
osem_notify/core/OsemClient.go

36 lines
710 B
Go

package core
import (
"errors"
"net/http"
"github.com/dghubble/sling"
)
type OsemError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type OsemClient struct {
sling *sling.Sling
}
func NewOsemClient(client *http.Client) *OsemClient {
return &OsemClient{
sling: sling.New().Client(client).Base("https://api.opensensemap.org/"),
}
}
func (client *OsemClient) GetBox(boxId string) (*Box, error) {
box := &Box{}
fail := &OsemError{}
client.sling.New().Path("boxes/").Path(boxId).Receive(box, fail)
if fail.Message != "" {
return box, errors.New("could not fetch box: " + fail.Message)
}
return box, nil
}
var Osem = NewOsemClient(&http.Client{}) // default client