wip text/csv
parent
847cd83fd5
commit
9bfbcf2d70
33
main.go
33
main.go
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/csv"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -98,7 +97,7 @@ func newHandlerGetAPIV1Messages(cfg Config) http.HandlerFunc {
|
||||||
return
|
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
|
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
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
writeJSON(w, map[string]any{"challenge": challenge.Challenge})
|
encodeJSONResponse(w, map[string]any{"challenge": challenge.Challenge})
|
||||||
}
|
}
|
||||||
|
|
||||||
func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc {
|
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)
|
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)
|
return json.NewEncoder(w).Encode(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeCSV(w http.ResponseWriter, fields []string, values [][]string) error {
|
func encodeCSVResponse(w http.ResponseWriter, v interface{}) error {
|
||||||
enc := csv.NewWriter(w)
|
return errors.New("not impl")
|
||||||
if err := enc.Write(fields); err != nil {
|
/*
|
||||||
return err
|
enc := csv.NewWriter(w)
|
||||||
}
|
if err := enc.Write(fields); err != nil {
|
||||||
return enc.WriteAll(values)
|
return err
|
||||||
|
}
|
||||||
|
return enc.WriteAll(values)
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue