canopeas/imgur/imgur.go

56 lines
1.2 KiB
Go

package imgur
import (
"encoding/json"
"fmt"
"net/http"
)
// TODO Make a client for this in a separate module
var Images []*Image
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 Init(clientID string) error {
req, err := http.NewRequest(http.MethodGet, "https://api.imgur.com/3/album/TaJVIdQ/images", nil)
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Client-ID %s", clientID))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
var imgurResp Response
if err := json.NewDecoder(resp.Body).Decode(&imgurResp); err != nil {
return err
}
Images = imgurResp.Images
return resp.Body.Close()
}