-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
57 lines (46 loc) · 1.2 KB
/
api.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
package slacker
import (
"encoding/json"
"errors"
"fmt"
"io"
)
// Response is the simplest representation of a Slack API response
type Response struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}
var (
// ErrNotAuthed is returned when an API response returns with "not_authed"
// as it's error attribute.
ErrNotAuthed = errors.New("slacker: Not Authed")
)
// ParseResponse parses an io.Reader (usually the result of an API request),
// and see's if the response actually contains error information. If it does,
// it will return an error, leaving `dest` untouched. Otherwise, it will
// json decode onto the destination passed in.
func ParseResponse(r io.Reader, dest interface{}) error {
d := json.NewDecoder(r)
var rawData json.RawMessage
if err := d.Decode(&rawData); err != nil {
return nil
}
var resp Response
if err := json.Unmarshal(rawData, &resp); err != nil {
return err
}
if !resp.Ok {
return responseError(resp.Error)
}
if err := json.Unmarshal(rawData, dest); err != nil {
return err
}
return nil
}
func responseError(ident string) error {
err, ok := errMap[ident]
if !ok {
return fmt.Errorf("slacker: unknown error returned: %s", ident)
}
return err
}