/
files.go
66 lines (52 loc) · 1.23 KB
/
files.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
package icqbotapi
import (
"context"
"net/http"
"net/url"
"github.com/mailru/easyjson"
)
// FileID represents file identifier.
type FileID string
func (r FileID) validate() error {
if r == "" {
return errValidation
}
return nil
}
func (r FileID) contributeToQuery(q url.Values) {
q.Set("fileId", string(r))
}
//easyjson:json
// FileInfoResponse represents info about uploaded file.
type FileInfoResponse struct {
StatusResponse
Type string `json:"type"`
Size uint64 `json:"size"`
Filename string `json:"filename"`
URL string `json:"url"`
}
//nolint:dupl
// GetFileInfo provides the file information function.
func (b *Bot) GetFileInfo(ctx context.Context, r FileID) (*FileInfoResponse, error) { //nolint:dupl
if err := r.validate(); err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, b.apiBaseURL+"/files/getInfo", nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
r.contributeToQuery(q)
req.URL.RawQuery = q.Encode()
httpResp, err := b.doRequest(ctx, req)
if err != nil {
return nil, err
}
defer httpResp.Body.Close()
resp := &FileInfoResponse{}
err = easyjson.UnmarshalFromReader(httpResp.Body, resp)
if err != nil {
return nil, err
}
return resp, nil
}