Passed
Push — master ( 73b11f...9f2199 )
by Viktor
01:41
created

api/fun/images.go (3 issues)

Severity
1
package fun
2
3
import (
4
	"bytes"
5
	"encoding/json"
6
	"errors"
7
	"fmt"
8
	"io"
9
	"net/http"
10
)
11
12
type ImageResponse struct {
0 ignored issues
show
exported type ImageResponse should have comment or be unexported
Loading history...
13
	Error    string `json:"error"`
14
	Success  bool   `json:"success"`
15
	ImageURL string `json:"image"`
16
}
17
18
func GetImageURL(category string) (string, error) {
0 ignored issues
show
exported function GetImageURL should have comment or be unexported
Loading history...
19
	resp, err := http.Get(fmt.Sprintf("https://botimages.realpha.ru/?category=%v", category))
20
	if err != nil && resp.StatusCode == http.StatusOK {
21
		fmt.Printf("Getting image url error: %v", err)
22
		return "", errors.New("getting image url error")
23
	}
24
25
	var result ImageResponse
26
27
	err = json.NewDecoder(resp.Body).Decode(&result)
28
	if err != nil {
29
		fmt.Printf(err.Error())
30
		return "", err
31
	}
32
33
	if result.Success {
34
		return fmt.Sprintf("https://botimages.realpha.ru/%v", result.ImageURL), nil
35
	}
36
	return "", errors.New("wrong data")
37
}
38
39
func GetImage(category string) (*bytes.Buffer, error) {
0 ignored issues
show
exported function GetImage should have comment or be unexported
Loading history...
40
	resp, err := http.Get(fmt.Sprintf("https://botimages.realpha.ru/?category=%v", category))
41
	if err != nil {
42
		fmt.Printf("Getting image url error: %v", err)
43
		return nil, errors.New("getting image url error")
44
	}
45
46
	buf := new(bytes.Buffer)
47
48
	_, err = io.Copy(buf, resp.Body) //png.Encode(buf, resp.Body)
49
	if err != nil {
50
		fmt.Printf("Map image: %v", err.Error())
51
	}
52
	return buf, err
53
}
54