add raw response #32

This commit is contained in:
gempir 2019-09-08 17:08:38 +02:00
parent bd52c80662
commit e2176ee338
3 changed files with 46 additions and 0 deletions

View file

@ -131,6 +131,7 @@ func (s *Server) getChannelLogs(c echo.Context) error {
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.ClearChatMessage:
message := *parsedMessage.(*twitch.ClearChatMessage)
@ -149,6 +150,7 @@ func (s *Server) getChannelLogs(c echo.Context) error {
Text: text,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.UserNoticeMessage:
message := *parsedMessage.(*twitch.UserNoticeMessage)
@ -160,6 +162,7 @@ func (s *Server) getChannelLogs(c echo.Context) error {
Text: message.SystemMsg + " " + message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
}
@ -170,6 +173,10 @@ func (s *Server) getChannelLogs(c echo.Context) error {
return writeJSONResponse(c, &logResult)
}
if shouldRespondWithRaw(c) {
return writeRawResponse(c, &logResult)
}
return writeTextResponse(c, &logResult)
}
@ -221,6 +228,7 @@ func (s *Server) getChannelLogsRange(c echo.Context) error {
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.ClearChatMessage:
message := *parsedMessage.(*twitch.ClearChatMessage)
@ -243,6 +251,7 @@ func (s *Server) getChannelLogsRange(c echo.Context) error {
Text: text,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.UserNoticeMessage:
message := *parsedMessage.(*twitch.UserNoticeMessage)
@ -254,6 +263,7 @@ func (s *Server) getChannelLogsRange(c echo.Context) error {
Text: message.SystemMsg + " " + message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
}
@ -264,5 +274,9 @@ func (s *Server) getChannelLogsRange(c echo.Context) error {
return writeJSONResponse(c, &logResult)
}
if shouldRespondWithRaw(c) {
return writeRawResponse(c, &logResult)
}
return writeTextResponse(c, &logResult)
}

View file

@ -119,6 +119,7 @@ type chatMessage struct {
Channel string `json:"channel"`
Timestamp timestamp `json:"timestamp"`
Type twitch.MessageType `json:"type"`
Raw string `json:"raw"`
}
type ErrorResponse struct {
@ -234,6 +235,17 @@ func writeTextResponse(c echo.Context, cLog *chatLog) error {
return nil
}
func writeRawResponse(c echo.Context, cLog *chatLog) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
for _, cMessage := range cLog.Messages {
c.Response().Write([]byte(cMessage.Raw + "\n"))
}
return nil
}
func writeJSONResponse(c echo.Context, logResult *chatLog) error {
_, stream := c.QueryParams()["stream"]
if stream {
@ -272,3 +284,9 @@ func shouldRespondWithJson(c echo.Context) bool {
return c.Request().Header.Get("Content-Type") == "application/json" || c.Request().Header.Get("accept") == "application/json" || c.QueryParam("type") == "json" || ok
}
func shouldRespondWithRaw(c echo.Context) bool {
_, ok := c.QueryParams()["raw"]
return c.QueryParam("type") == "raw" || ok
}

View file

@ -305,6 +305,7 @@ func (s *Server) getUserLogs(c echo.Context) error {
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.ClearChatMessage:
message := *parsedMessage.(*twitch.ClearChatMessage)
@ -323,6 +324,7 @@ func (s *Server) getUserLogs(c echo.Context) error {
Text: text,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.UserNoticeMessage:
message := *parsedMessage.(*twitch.UserNoticeMessage)
@ -334,6 +336,7 @@ func (s *Server) getUserLogs(c echo.Context) error {
Text: message.SystemMsg + " " + message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
}
@ -344,6 +347,10 @@ func (s *Server) getUserLogs(c echo.Context) error {
return writeJSONResponse(c, &logResult)
}
if shouldRespondWithRaw(c) {
return writeRawResponse(c, &logResult)
}
return writeTextResponse(c, &logResult)
}
@ -396,6 +403,7 @@ func (s *Server) getUserLogsRange(c echo.Context) error {
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.ClearChatMessage:
message := *parsedMessage.(*twitch.ClearChatMessage)
@ -418,6 +426,7 @@ func (s *Server) getUserLogsRange(c echo.Context) error {
Text: text,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
case *twitch.UserNoticeMessage:
message := *parsedMessage.(*twitch.UserNoticeMessage)
@ -429,6 +438,7 @@ func (s *Server) getUserLogsRange(c echo.Context) error {
Text: message.SystemMsg + " " + message.Message,
Type: message.Type,
Channel: message.Channel,
Raw: message.Raw,
}
}
@ -439,5 +449,9 @@ func (s *Server) getUserLogsRange(c echo.Context) error {
return writeJSONResponse(c, &logResult)
}
if shouldRespondWithRaw(c) {
return writeRawResponse(c, &logResult)
}
return writeTextResponse(c, &logResult)
}