From 3967367eed0097f49c4c5961824487178fa3305c Mon Sep 17 00:00:00 2001 From: Alessio Date: Mon, 18 Mar 2024 21:15:27 -0700 Subject: [PATCH] Fix more lint errors --- pkg/scraper/api_request_utils.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/scraper/api_request_utils.go b/pkg/scraper/api_request_utils.go index 721236c..a0a3b64 100644 --- a/pkg/scraper/api_request_utils.go +++ b/pkg/scraper/api_request_utils.go @@ -2,10 +2,12 @@ package scraper import ( "encoding/json" + "errors" "fmt" "io" "net/http" "net/http/cookiejar" + "net/http/httputil" "net/url" "strings" "time" @@ -144,6 +146,11 @@ func (api *API) update_csrf_token() { } } +func is_timeout(err error) bool { + var urlErr *url.Error + return errors.As(err, &urlErr) && urlErr.Timeout() +} + func (api *API) do_http_POST(remote_url string, body string, result interface{}) error { req, err := http.NewRequest("POST", remote_url, strings.NewReader(body)) if err != nil { @@ -161,7 +168,7 @@ func (api *API) do_http_POST(remote_url string, body string, result interface{}) log.Debug(" " + body) resp, err := api.Client.Do(req) - if urlErr, ok := err.(*url.Error); ok && urlErr.Timeout() { + if is_timeout(err) { return fmt.Errorf("POST %q:\n %w", remote_url, ErrRequestTimeout) } else if err != nil { return fmt.Errorf("Error executing HTTP POST request:\n %w", err) @@ -212,7 +219,7 @@ func (api *API) do_http(remote_url string, cursor string, result interface{}) er } resp, err := api.Client.Do(req) - if urlErr, ok := err.(*url.Error); ok && urlErr.Timeout() { + if is_timeout(err) { return fmt.Errorf("GET %q:\n %w", remote_url, ErrRequestTimeout) } else if err != nil { return fmt.Errorf("Error executing HTTP request:\n %w", err) @@ -390,7 +397,7 @@ func (api *API) DownloadMedia(remote_url string) ([]byte, error) { // req.Header.Set("Referer", "https://twitter.com/") // DM embedded images require this header resp, err := api.Client.Do(req) - if urlErr, ok := err.(*url.Error); ok && urlErr.Timeout() { + if is_timeout(err) { return []byte{}, fmt.Errorf("GET %q:\n %w", remote_url, ErrRequestTimeout) } else if err != nil { return []byte{}, fmt.Errorf("Error executing HTTP request:\n %w", err)