Skip to content

Commit

Permalink
minor move thing around
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Sep 15, 2021
1 parent 0d7db5f commit 1f48fe2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
39 changes: 20 additions & 19 deletions internal/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import (
"time"
)

func Fetch(url, ua string, timeout time.Duration) (string, error) {
request, err := makeRequest(url, ua)
if err != nil {
return "", err
}
response, err := getResponse(request, timeout)
if err != nil {
return "", err
}
body, err := handleResponse(response)
if err != nil {
return "", err
}
return body, nil
}

func makeRequest(url, ua string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
Expand All @@ -29,25 +45,10 @@ func handleResponse(resp *http.Response) (string, error) {
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("invalid http status code %d", resp.StatusCode)
}
if b, err := ioutil.ReadAll(resp.Body); err == nil {
return string(b), nil
}
defer resp.Body.Close()
return "", fmt.Errorf("no body read")
}

func Fetch(url, ua string, timeout time.Duration) (string, error) {
request, err := makeRequest(url, ua)
if err != nil {
return "", err
}
response, err := getResponse(request, timeout)
if err != nil {
return "", err
}
body, err := handleResponse(response)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
return "", fmt.Errorf("no body read")
}
return body, nil
defer resp.Body.Close()
return string(body), nil
}
22 changes: 11 additions & 11 deletions internal/twitter/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ import (
"jaytaylor.com/html2text"
)

func externalName(external string) string {
if strings.Contains(external, "pic.twitter.com") {
return "image: " + external + "\n"
} else if strings.Contains(external, "t.co") {
return "external: " + external + "\n"
} else if external != "" {
panic("unknown not external URL passed: " + external)
}
return ""
}

func Format(t *Embedded) (formatted string) {
msg, links := paragraph(t.Html)
formatted += fmt.Sprintf("URL: %s\n", t.Url)
Expand All @@ -33,6 +22,17 @@ func Format(t *Embedded) (formatted string) {
return
}

func externalName(external string) string {
if strings.Contains(external, "pic.twitter.com") {
return "image: " + external + "\n"
} else if strings.Contains(external, "t.co") {
return "external: " + external + "\n"
} else if external != "" {
panic("unknown not external URL passed: " + external)
}
return ""
}

func fitInScreen(s string) string {
s, _ = text.WrapLeftPadded(s, 100, 3)
return s
Expand Down

0 comments on commit 1f48fe2

Please sign in to comment.