Private
Public Access
1
0

Implements pollUpdates

This commit is contained in:
2023-07-19 11:58:13 -06:00
parent 7611bedef7
commit 1fce2c7cb3
5 changed files with 182 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
@@ -159,11 +160,6 @@ func (m *MockHTTPServer) handleNotFound(w http.ResponseWriter, r *http.Request)
http.NotFound(w, r)
}
func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Request) {
// Stub: return 205 (Nothing to report)
w.WriteHeader(http.StatusResetContent)
}
func (m *MockHTTPServer) handleSendMessage(w http.ResponseWriter, r *http.Request) {
// Decode request body as SendMessageRequest
var sendMessageReq SendMessageRequest
@@ -196,6 +192,43 @@ func (m *MockHTTPServer) handleSendMessage(w http.ResponseWriter, r *http.Reques
w.WriteHeader(http.StatusOK)
}
func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Request) {
// TODO: This should block if we don't have updates for that seq yet.
seq := -1
seqString := r.URL.Query().Get("seq")
if len(seqString) > 0 {
var err error
seq, err = strconv.Atoi(seqString)
if err != nil {
log.Error().Err(err).Msg("FetchUpdates: Error parsing seq")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
// Fetch updates (blocking)
updates := m.Server.FetchUpdatesBlocking(seq)
if len(updates) == 0 {
// return 205 (Nothing to report)
w.WriteHeader(http.StatusResetContent)
log.Info().Msg("FetchUpdates: Nothing to report")
} else {
// Encode updates as JSON
jsonData, err := json.Marshal(updates)
if err != nil {
log.Error().Err(err).Msg("Error marshalling updates")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Write JSON to response
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}
}
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.logRequest(r, r.URL.Query().Encode())
m.mux.ServeHTTP(w, r)