Passed
Push — master ( a266ab...6dd3ac )
by Viktor
01:47
created

fun.GetImage   A

Complexity

Conditions 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nop 1
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
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
introduced by
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
introduced by
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
		return "", err
30
	}
31
32
	if result.Success {
33
		return fmt.Sprintf("https://botimages.realpha.ru/%v", result.ImageURL), nil
34
	}
35
	return "", errors.New("wrong data")
36
}
37
38
func GetImage(category string) (*bytes.Buffer, error) {
0 ignored issues
show
introduced by
exported function GetImage should have comment or be unexported
Loading history...
39
	resp, err := http.Get(fmt.Sprintf("https://botimages.realpha.ru/?category=%v", category))
40
	if err != nil {
41
		fmt.Printf("Getting image url error: %v", err)
42
		return nil, errors.New("getting image url error")
43
	}
44
45
	buf := new(bytes.Buffer)
46
47
	_, err = io.Copy(buf, resp.Body) //png.Encode(buf, resp.Body)
48
	if err != nil {
49
		fmt.Printf("Map image: %v", err.Error())
50
	}
51
	return buf, err
52
}
53