forked from keybase/client
/
chat_api_decoder.go
98 lines (83 loc) · 2.01 KB
/
chat_api_decoder.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2016 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"encoding/json"
"fmt"
"io"
"golang.org/x/net/context"
)
type ErrInvalidMethod struct {
name string
version int
}
func (e ErrInvalidMethod) Error() string {
return fmt.Sprintf("invalid v%d method %q", e.version, e.name)
}
type ErrInvalidVersion struct {
version int
}
func (e ErrInvalidVersion) Error() string {
return fmt.Sprintf("invalid version %d", e.version)
}
type ErrInvalidJSON struct {
message string
}
func (e ErrInvalidJSON) Error() string {
return fmt.Sprintf("invalid JSON: %s", e.message)
}
type ChatAPIDecoder struct {
handler ChatAPIHandler
}
func NewChatAPIDecoder(h ChatAPIHandler) *ChatAPIDecoder {
return &ChatAPIDecoder{
handler: h,
}
}
func (d *ChatAPIDecoder) Decode(ctx context.Context, r io.Reader, w io.Writer) error {
dec := json.NewDecoder(r)
for {
var c Call
if err := dec.Decode(&c); err == io.EOF {
break
} else if err != nil {
if err == io.ErrUnexpectedEOF {
return ErrInvalidJSON{message: "expected more JSON in input"}
}
return err
}
switch c.Params.Version {
case 0, 1:
if err := d.handleV1(ctx, c, w); err != nil {
return err
}
default:
return ErrInvalidVersion{version: c.Params.Version}
}
}
return nil
}
func (d *ChatAPIDecoder) handleV1(ctx context.Context, c Call, w io.Writer) error {
switch c.Method {
case methodList:
return d.handler.ListV1(ctx, c, w)
case methodRead:
return d.handler.ReadV1(ctx, c, w)
case methodSend:
return d.handler.SendV1(ctx, c, w)
case methodEdit:
return d.handler.EditV1(ctx, c, w)
case methodDelete:
return d.handler.DeleteV1(ctx, c, w)
case methodAttach:
return d.handler.AttachV1(ctx, c, w)
case methodDownload:
return d.handler.DownloadV1(ctx, c, w)
case methodSetStatus:
return d.handler.SetStatusV1(ctx, c, w)
case methodMark:
return d.handler.MarkV1(ctx, c, w)
default:
return ErrInvalidMethod{name: c.Method, version: 1}
}
}