diff options
author | Martin Polden <mpolden@mpolden.no> | 2017-07-10 13:08:40 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2017-07-10 13:08:40 +0200 |
commit | 5d76fe53d37d903d8ea821aab56272febd73660f (patch) | |
tree | bf559a5482e4c36a517762ff9517924a9abb7808 | |
parent | 26066426c3efeca5b650c3254c2f21dbdafcdc40 (diff) |
Remove pretty parameter
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | api/api.go | 21 | ||||
-rw-r--r-- | api/api_test.go | 22 |
3 files changed, 9 insertions, 45 deletions
@@ -45,13 +45,12 @@ Help Options: ## API usage -The route `/api/v1/busstops` returns a list of all known bus stops. All routes -support the parameter `?pretty` to return pretty-printed JSON. +The route `/api/v1/busstops` returns a list of all known bus stops. Example: ``` -$ curl 'https://atbapi.tar.io/api/v1/busstops?pretty' +$ curl 'https://atbapi.tar.io/api/v1/busstops' | jq . { "stops": [ ... @@ -75,7 +74,7 @@ stop, identified by a node ID. Example: ``` -$ curl 'https://atbapi.tar.io/api/v1/busstops/16011376?pretty' +$ curl 'https://atbapi.tar.io/api/v1/busstops/16011376' | jq . { "stopId": 100633, "nodeId": 16011376, @@ -90,7 +89,7 @@ $ curl 'https://atbapi.tar.io/api/v1/busstops/16011376?pretty' As [GeoJSON](http://geojson.org/): ``` -$ curl 'https://atbapi.tar.io/api/v1/busstops/16011376?pretty&geojson' +$ curl 'https://atbapi.tar.io/api/v1/busstops/16011376?geojson' | jq . { "type": "Feature", "geometry": { @@ -121,7 +120,7 @@ stop, identified by a node ID. Example: ``` -$ curl 'https://atbapi.tar.io/api/v1/departures/16011376?pretty' +$ curl 'https://atbapi.tar.io/api/v1/departures/16011376' | jq . { "isGoingTowardsCentrum": true, "departures": [ @@ -9,8 +9,6 @@ import ( "strconv" "time" - "context" - "github.com/gorilla/mux" "github.com/mpolden/atbapi/atb" cache "github.com/pmylund/go-cache" @@ -29,13 +27,6 @@ type expiration struct { stops time.Duration } -func marshal(data interface{}, indent bool) ([]byte, error) { - if indent { - return json.MarshalIndent(data, "", " ") - } - return json.Marshal(data) -} - func urlPrefix(r *http.Request) string { host := r.Host if host == "" { @@ -273,28 +264,24 @@ func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if e.err != nil { log.Print(e.err) } - jsonBlob, err := marshal(e, true) + out, err := json.Marshal(e) if err != nil { // Should never happen panic(err) } w.WriteHeader(e.Status) - w.Write(jsonBlob) + w.Write(out) } else { - indent := r.Context().Value("indent").(bool) - jsonBlob, err := marshal(data, indent) + out, err := json.Marshal(data) if err != nil { panic(err) } - w.Write(jsonBlob) + w.Write(out) } } func requestFilter(next http.Handler, cors bool) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, indent := r.URL.Query()["pretty"] - ctx := context.Background() - r = r.WithContext(context.WithValue(ctx, "indent", indent)) w.Header().Set("Content-Type", "application/json") if cors { w.Header().Set("Access-Control-Allow-Methods", "GET") diff --git a/api/api_test.go b/api/api_test.go index c6eb85d..e8a875d 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -10,28 +10,6 @@ import ( "github.com/mpolden/atbapi/atb" ) -func TestMarshal(t *testing.T) { - data := struct{ Foo string }{"bar"} - b, err := marshal(data, true) - if err != nil { - t.Fatal(err) - } - actual := string(b) - expected := "{\n \"Foo\": \"bar\"\n}" - if actual != expected { - t.Fatalf("Expected '%s', got '%s'", expected, actual) - } - b, err = marshal(data, false) - if err != nil { - t.Fatal(err) - } - actual = string(b) - expected = "{\"Foo\":\"bar\"}" - if actual != expected { - t.Fatalf("Expected '%s', got '%s'", expected, actual) - } -} - func newTestServer(path string, body string) *httptest.Server { handler := func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/soap+xml; charset=utf-8") |