wip text/csv
parent
847cd83fd5
commit
9bfbcf2d70
23
main.go
23
main.go
|
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
|
@ -98,7 +97,7 @@ func newHandlerGetAPIV1Messages(cfg Config) http.HandlerFunc {
|
|||
return
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]any{"messages": messages})
|
||||
encodeJSONResponse(w, map[string]any{"messages": messages})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -120,7 +119,7 @@ func newHandlerGetAPIV1Threads(cfg Config) http.HandlerFunc {
|
|||
return
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]any{"threads": threads})
|
||||
encodeJSONResponse(w, map[string]any{"threads": threads})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -138,7 +137,7 @@ func newHandlerGetAPIV1ThreadsThread(cfg Config) http.HandlerFunc {
|
|||
return
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]any{"thread": map[string]any{"messages": messages}})
|
||||
encodeJSONResponse(w, map[string]any{"thread": map[string]any{"messages": messages}})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -169,7 +168,7 @@ func handlerPostAPIV1EventsSlackInitialize(w http.ResponseWriter, r *http.Reques
|
|||
return
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]any{"challenge": challenge.Challenge})
|
||||
encodeJSONResponse(w, map[string]any{"challenge": challenge.Challenge})
|
||||
}
|
||||
|
||||
func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc {
|
||||
|
|
@ -245,14 +244,24 @@ func parseSince(s string) (time.Time, error) {
|
|||
return time.Time{}, fmt.Errorf("failed to parse since=%q", s)
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v interface{}) error {
|
||||
func encodeResponse(w http.ResponseWriter, r *http.Request, v interface{}) error {
|
||||
if strings.Contains(r.Header.Get("Accept"), "text/csv") {
|
||||
return encodeCSVResponse(w, v)
|
||||
}
|
||||
return encodeJSONResponse(w, v)
|
||||
}
|
||||
|
||||
func encodeJSONResponse(w http.ResponseWriter, v interface{}) error {
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func writeCSV(w http.ResponseWriter, fields []string, values [][]string) error {
|
||||
func encodeCSVResponse(w http.ResponseWriter, v interface{}) error {
|
||||
return errors.New("not impl")
|
||||
/*
|
||||
enc := csv.NewWriter(w)
|
||||
if err := enc.Write(fields); err != nil {
|
||||
return err
|
||||
}
|
||||
return enc.WriteAll(values)
|
||||
*/
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue