diff --git a/pastebin/pastebin.go b/pastebin/pastebin.go index 8d13263..f9dadd2 100644 --- a/pastebin/pastebin.go +++ b/pastebin/pastebin.go @@ -4,6 +4,7 @@ package pastebin import ( "errors" + "fmt" "io/ioutil" "net/http" "net/url" @@ -11,6 +12,7 @@ import ( ) const ( + baseURL = "https://pastebin.com/" pastebinDevKey = "d06a9df64b29123b8eeda23f53d6535d" ) @@ -36,24 +38,24 @@ func (p Pastebin) Put(text, title string) (id string, err error) { data.Set("api_paste_private", "0") // Create a public paste. data.Set("api_paste_expire_date", "N") // The paste should never expire. - resp, err := http.PostForm("http://pastebin.com/api/api_post.php", data) + resp, err := http.PostForm(baseURL+"api/api_post.php", data) if err != nil { return "", err } - if resp.StatusCode != 200 { - return "", ErrPutFailed - } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } + if resp.StatusCode != 200 { + return "", fmt.Errorf("%w: %s", ErrPutFailed, string(respBody)) + } return p.StripURL(string(respBody)), nil } // Get returns the text inside the paste identified by ID. func (p Pastebin) Get(id string) (text string, err error) { - resp, err := http.Get("http://pastebin.com/raw.php?i=" + id) + resp, err := http.Get(baseURL + "raw.php?i=" + id) if err != nil { return "", err } @@ -70,10 +72,10 @@ func (p Pastebin) Get(id string) (text string, err error) { // StripURL returns the paste ID from a pastebin URL. func (p Pastebin) StripURL(url string) string { - return strings.Replace(url, "http://pastebin.com/", "", -1) + return strings.Replace(url, baseURL, "", -1) } // WrapID returns the pastebin URL from a paste ID. func (p Pastebin) WrapID(id string) string { - return "http://pastebin.com/" + id + return baseURL + id }