From e69128b822f9dee7a76fd12bc5d7fd94c003c9de Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 16 Apr 2023 19:09:33 +0200 Subject: [PATCH] Refactor common code for SN API --- sn.go | 69 ++++++++++++++++++++++++----------------------------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/sn.go b/sn.go index 7d801a1..6498dcc 100644 --- a/sn.go +++ b/sn.go @@ -25,6 +25,31 @@ type DupesResponse struct { } `json:"data"` } +func makeGraphQLRequest(body GraphQLPayload) *http.Response { + bodyJSON, err := json.Marshal(body) + if err != nil { + log.Fatal("Error during json.Marshal:", err) + } + + url := "https://stacker.news/api/graphql" + req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyJSON)) + if err != nil { + panic(err) + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Cookie", fmt.Sprintf("__Host-next-auth.csrf-token=%s", SnApiToken)) + + client := http.DefaultClient + resp, err := client.Do(req) + if err != nil { + panic(err) + } + + log.Printf("POST %s %d\n", url, resp.StatusCode) + + return resp +} + func filterByRelevanceForSN(stories *[]Story) *[]Story { // TODO: filter by relevance @@ -46,29 +71,11 @@ func fetchDupes(url string) *[]Dupe { "url": url, }, } - - bodyJSON, err := json.Marshal(body) - if err != nil { - log.Fatal("Error during json.Marshal:", err) - } - - apiUrl := "https://stacker.news/api/graphql" - req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(bodyJSON)) - if err != nil { - panic(err) - } - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Cookie", fmt.Sprintf("__Host-next-auth.csrf-token=%s", SnApiToken)) - - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - panic(err) - } + resp := makeGraphQLRequest(body) defer resp.Body.Close() var dupesResp DupesResponse - err = json.NewDecoder(resp.Body).Decode(&dupesResp) + err := json.NewDecoder(resp.Body).Decode(&dupesResp) if err != nil { log.Fatal("Error decoding dupes JSON:", err) } @@ -95,26 +102,6 @@ func postToSN(story *Story) { "title": story.Title, }, } - - bodyJSON, err := json.Marshal(body) - if err != nil { - log.Fatal("Error during json.Marshal:", err) - } - - apiUrl := "https://stacker.news/api/graphql" - req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(bodyJSON)) - if err != nil { - panic(err) - } - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Cookie", fmt.Sprintf("__Host-next-auth.csrf-token=%s", SnApiToken)) - - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - panic(err) - } + resp := makeGraphQLRequest(body) defer resp.Body.Close() - - log.Printf("POST %s %d\n", apiUrl, resp.StatusCode) }