Private
Public Access
1
0

messages: Implements /messages API

This commit is contained in:
2023-06-19 18:31:05 -07:00
parent cdf3d922f7
commit 2d415a1170
6 changed files with 183 additions and 6 deletions

View File

@@ -82,6 +82,35 @@ func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Requ
w.Write(jsonData)
}
func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request) {
guid := r.URL.Query().Get("guid")
if len(guid) == 0 {
log.Println("handleMessage: Got empty guid parameter")
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
return
}
conversation, err := m.Server.ConversationForGUID(guid)
if err != nil {
log.Printf("handleMessage: Error getting conversation (%s): %s", guid, err)
http.Error(w, "conversation not found", http.StatusBadRequest)
return
}
messages := m.Server.MessagesForConversation(*conversation)
jsonData, err := json.Marshal(messages)
if err != nil {
log.Printf("Error marshalling messages: %s", err)
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) handleAuthenticate(w http.ResponseWriter, r *http.Request) {
// Decode request body as AuthenticationRequest
var authReq AuthenticationRequest
@@ -130,6 +159,7 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
this.mux.Handle("/conversations", http.HandlerFunc(this.handleConversations))
this.mux.Handle("/status", http.HandlerFunc(this.handleStatus))
this.mux.Handle("/authenticate", http.HandlerFunc(this.handleAuthenticate))
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
return &this
}