-
Notifications
You must be signed in to change notification settings - Fork 0
/
chevereto.go
68 lines (56 loc) · 1.41 KB
/
chevereto.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type CheveretoResponse struct {
OriginUrl string
SiteUrl string
DeleteUrl string
}
func call_chevereto_api(tg_url string) (CheveretoResponse, error) {
client := &http.Client{}
form_data := url.Values{
"source": []string{tg_url},
"format": []string{string("json")},
}
req, err := http.NewRequest("POST", CHEVERETO_HOST_API_URL, strings.NewReader(form_data.Encode()))
if err != nil {
return CheveretoResponse{}, err
}
req.Header.Add("X-API-Key", CHEVERETO_API_KEY)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
return CheveretoResponse{}, err
}
defer func() {
err = resp.Body.Close()
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return CheveretoResponse{}, err
}
var (
data map[string]interface{}
)
err = json.Unmarshal([]byte(body), &data)
if err != nil {
return CheveretoResponse{}, err
}
status_code := data["status_code"].(float64)
if status_code != 200 {
error_mgs := data["error"].(map[string]interface{})
return CheveretoResponse{}, errors.New(error_mgs["message"].(string))
}
image_info := data["image"].(map[string]interface{})
return CheveretoResponse{
OriginUrl: image_info["url"].(string),
DeleteUrl: image_info["delete_url"].(string),
SiteUrl: image_info["url_short"].(string),
}, nil
}