canopeas/imgur/imgur.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

package imgur
import (
"encoding/json"
"fmt"
"net/http"
)
type Response struct {
Images []*Image `json:"data"`
Success bool `json:"success"`
Status int `json:"status"`
}
type Image struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Datetime int `json:"datetime"`
Type string `json:"type"`
Animated bool `json:"animated"`
Width int `json:"width"`
Height int `json:"height"`
Size int `json:"size"`
Views int `json:"views"`
Bandwidth int `json:"bandwidth"`
Deletehash string `json:"deletehash"`
Section string `json:"section"`
Link string `json:"link"`
}
func Get(clientID, albumID string) ([]*Image, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://api.imgur.com/3/album/%s/images", albumID), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Client-ID %s", clientID))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var imgurResp Response
return imgurResp.Images, json.NewDecoder(resp.Body).Decode(&imgurResp)
}