From 9df63af33ca25561e519ce2451668b828131177e Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Wed, 20 Oct 2021 21:09:38 +0200 Subject: [PATCH] eksterd: make preview also available as a post request --- pkg/client/requests.go | 6 +++--- pkg/server/microsub.go | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/client/requests.go b/pkg/client/requests.go index 6ce9fa5..d664771 100644 --- a/pkg/client/requests.go +++ b/pkg/client/requests.go @@ -88,7 +88,7 @@ func (c *Client) microsubPostRequest(action string, args map[string]string) (*ht if res.StatusCode != 200 { msg, _ := ioutil.ReadAll(res.Body) - return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, string(msg)) + return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, strings.TrimSpace(string(msg))) } return res, err @@ -116,7 +116,7 @@ func (c *Client) microsubPostFormRequest(action string, args map[string]string, if res.StatusCode != 200 { msg, _ := ioutil.ReadAll(res.Body) - return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, string(msg)) + return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, strings.TrimSpace(string(msg))) } return res, err @@ -180,7 +180,7 @@ func (c *Client) TimelineGet(before, after, channel string) (microsub.Timeline, func (c *Client) PreviewURL(url string) (microsub.Timeline, error) { args := make(map[string]string) args["url"] = url - res, err := c.microsubGetRequest("preview", args) + res, err := c.microsubPostRequest("preview", args) if err != nil { return microsub.Timeline{}, err } diff --git a/pkg/server/microsub.go b/pkg/server/microsub.go index d3b749d..227ff76 100644 --- a/pkg/server/microsub.go +++ b/pkg/server/microsub.go @@ -125,7 +125,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, "internal server error", 500) } } else { - http.Error(w, fmt.Sprintf("unknown action %s\n", action), 400) + http.Error(w, fmt.Sprintf("unknown action %s", action), 400) return } return @@ -182,6 +182,13 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } respondJSON(w, []string{}) + } else if action == "preview" { + timeline, err := h.backend.PreviewURL(values.Get("url")) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + respondJSON(w, timeline) } else if action == "search" { query := values.Get("query") channel := values.Get("channel")