go-asink/client/net.go

142 lines
3.4 KiB
Go
Raw Normal View History

2013-03-17 23:02:51 -04:00
package main
import (
"asink"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
2013-03-17 23:02:51 -04:00
"io/ioutil"
"net/http"
"strconv"
"time"
)
2013-03-18 07:17:44 -04:00
const MIN_ERROR_WAIT = 100 // 1/10 of a second
2013-03-17 23:02:51 -04:00
const MAX_ERROR_WAIT = 10000 // 10 seconds
func AuthenticatedRequest(method, url, bodyType string, body io.Reader, username, password string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if bodyType != "" {
req.Header.Set("Content-Type", bodyType)
}
req.SetBasicAuth(username, password)
return client.Do(req)
}
func AuthenticatedGet(url string, username, password string) (*http.Response, error) {
return AuthenticatedRequest("GET", url, "", nil, username, password)
}
func AuthenticatedPost(url, bodyType string, body io.Reader, username, password string) (*http.Response, error) {
return AuthenticatedRequest("POST", url, bodyType, body, username, password)
}
2013-03-17 23:02:51 -04:00
func SendEvent(globals AsinkGlobals, event *asink.Event) error {
url := "http://" + globals.server + ":" + strconv.Itoa(int(globals.port)) + "/events/"
//construct json payload
events := asink.EventList{
Events: []*asink.Event{event},
}
b, err := json.Marshal(events)
if err != nil {
return err
}
//actually make the request
resp, err := AuthenticatedPost(url, "application/json", bytes.NewReader(b), globals.username, globals.password)
2013-03-17 23:02:51 -04:00
if err != nil {
return err
}
defer resp.Body.Close()
//check to make sure request succeeded
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var apistatus asink.APIResponse
err = json.Unmarshal(body, &apistatus)
if err != nil {
return err
}
if apistatus.Status != asink.SUCCESS {
return errors.New("API response was not success: " + apistatus.Explanation)
}
return nil
}
func GetEvents(globals AsinkGlobals, events chan *asink.Event) {
url := "http://" + globals.server + ":" + strconv.Itoa(int(globals.port)) + "/events/"
var successiveErrors uint = 0
errorWait := func(err error) {
fmt.Println(err)
var waitMilliseconds time.Duration = MIN_ERROR_WAIT << successiveErrors
if waitMilliseconds > MAX_ERROR_WAIT {
waitMilliseconds = MAX_ERROR_WAIT
}
time.Sleep(waitMilliseconds * time.Millisecond)
successiveErrors++
}
//query DB for latest remote event version number that we've seen locally
2013-03-18 07:17:44 -04:00
latestEvent, err := globals.db.DatabaseLatestRemoteEvent()
2013-03-17 23:02:51 -04:00
if err != nil {
panic(err)
}
for {
//query for events after latest_event
var fullUrl string
if latestEvent != nil {
2013-03-18 07:17:44 -04:00
fullUrl = url + strconv.FormatInt(latestEvent.Id+1, 10)
2013-03-17 23:02:51 -04:00
} else {
fullUrl = url + "0"
}
resp, err := AuthenticatedGet(fullUrl, globals.username, globals.password)
2013-03-17 23:02:51 -04:00
//if error, perform exponential backoff (with maximum timeout)
if err != nil {
if resp != nil {
resp.Body.Close()
}
errorWait(err)
continue
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close() //must be done after the last time resp is used
if err != nil {
errorWait(err)
continue
}
var apistatus asink.APIResponse
err = json.Unmarshal(body, &apistatus)
if err != nil {
errorWait(err)
continue
}
if apistatus.Status != asink.SUCCESS {
errorWait(err)
continue
}
for _, event := range apistatus.Events {
2013-03-18 07:17:44 -04:00
if latestEvent != nil && event.Id != latestEvent.Id+1 {
2013-03-17 23:02:51 -04:00
break
}
events <- event
latestEvent = event
}
successiveErrors = 0
}
}