-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgur.go
50 lines (41 loc) · 1.02 KB
/
imgur.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package comics
import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
"os"
"strings"
scan "github.com/mattn/go-scan"
)
const endpoint = "https://api.imgur.com/3/image"
func ImgurUpload(img []byte, clientID string) (string, error) {
encoded := base64.StdEncoding.EncodeToString(img)
params := url.Values{"image": {encoded}}
req, err := http.NewRequest("POST", endpoint, strings.NewReader(params.Encode()))
if err != nil {
fmt.Fprintln(os.Stderr, "post:", err.Error())
os.Exit(1)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "Client-ID "+clientID)
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
if res.StatusCode != 200 {
var message string
err = scan.ScanJSON(res.Body, "data/error", &message)
if err != nil {
message = res.Status
}
return "", fmt.Errorf("%s", message)
}
defer res.Body.Close()
var link string
err = scan.ScanJSON(res.Body, "data/link", &link)
if err != nil {
return "", err
}
return link, nil
}