-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
102 lines (87 loc) · 2.71 KB
/
context.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
99
100
101
102
// Kiebitz - Privacy-Friendly Appointment Scheduling
// Copyright (C) 2021-2021 The Kiebitz Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package jsonrpc
import (
"fmt"
"github.com/kiebitz-oss/services"
"regexp"
"strconv"
"strings"
)
var idRegexp = regexp.MustCompile(`^n:(-?\d{1,32})$`)
var idNRegexp = regexp.MustCompile(`^(n+):(-?\d{1,32})$`)
type Context struct {
Request *Request
}
func convertID(id interface{}) interface{} {
if strValue, ok := id.(string); ok {
if matches := idRegexp.FindStringSubmatch(strValue); matches != nil {
// we convert this value back to a number
if n, err := strconv.ParseInt(matches[1], 10, 64); err != nil {
// this should not happen, if it does we log the error and
// return the string value (could only be an overflow)
services.Log.Error(err)
return id
} else {
return n
}
}
if matches := idNRegexp.FindStringSubmatch(strValue); matches != nil && len(matches[1])%2 == 0 {
return fmt.Sprintf("%s:%s", strings.Repeat("n", len(matches[1])/2), matches[2])
}
}
// we do not convert anything
return id
}
func (c *Context) Result(data interface{}) services.Response {
return &Response{
ID: convertID(c.Request.ID),
Result: data,
JSONRPC: "2.0",
}
}
func (c *Context) Error(code int, message string, data interface{}) services.Response {
return &Response{
Error: &Error{
Code: code,
Message: message,
Data: data,
},
JSONRPC: "2.0",
ID: convertID(c.Request.ID),
}
}
func (c *Context) Params() map[string]interface{} {
return c.Request.Params
}
func (c *Context) NotFound() services.Response {
return c.Error(404, "not found", nil)
}
func (c *Context) Acknowledge() services.Response {
return c.Result("ok")
}
func (c *Context) Nil() services.Response {
return c.Result(nil)
}
func (c *Context) MethodNotFound() services.Response {
return c.Error(-32601, "method not found", nil)
}
func (c *Context) InvalidParams(err error) services.Response {
return c.Error(-32602, "invalid params", err)
}
func (c *Context) InternalError() services.Response {
return c.Error(-32603, "internal error", nil)
}