-
Notifications
You must be signed in to change notification settings - Fork 1
/
call.go
166 lines (149 loc) · 3.39 KB
/
call.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package apirouter
import (
"net/http"
"strings"
"github.com/KarpelesLab/pobj"
"github.com/KarpelesLab/webutil"
)
func (c *Context) Call() (any, error) {
p := c.path
if len(p) >= 1 && p[0] == '@' {
return c.CallSpecial()
}
if p == "_websocket" {
return c.prepareWebsocket()
}
r := pobj.Root()
m := ""
method := false
corsReq := c.verb == "OPTIONS"
var obj any
if pos := strings.LastIndexByte(p, ':'); pos != -1 {
m = p[pos+1:]
p = p[:pos]
method = true
}
ps := strings.Split(p, "/")
for _, s := range ps {
// detect what is "s"
if len(s) == 0 {
// return error?
continue
}
if s[0] >= 'A' && s[0] <= 'Z' {
// starts with A-Z: this is likely a class name
v := r.Child(s)
if v != nil {
r = v
obj = nil
continue
}
//return nil, ErrNotFound
}
// this is an attempt to load as ID
if obj != nil {
// you can't have Object/id/id_again
return nil, ErrNotFound
}
// make sure we have a GET action
if r.Action == nil {
return nil, ErrNotFound
}
get := r.Action.Fetch
if get == nil {
return nil, ErrNotFound
}
if corsReq {
// ignore loading the actual ID if in cors req
obj = true
continue
}
var res any
var err error
if get.IsStringArg(0) {
res, err = get.CallArg(c, s)
} else {
res, err = get.CallArg(c, struct{ Id string }{Id: s})
}
if err != nil {
return nil, err
}
c.objects[r.String()] = res
obj = res
}
// ok we need to return a class
if method {
if corsReq {
c.flags["raw"] = true
return nil, &optionsResponder{[]string{"GET", "POST", "HEAD", "OPTIONS"}}
}
// ok we need to call a static method
meth := r.Static(m)
if meth == nil {
return nil, ErrNotFound
}
switch c.verb {
case "HEAD", "GET", "POST":
return meth.CallArg(c, c.params)
default:
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
}
}
if obj != nil {
if corsReq {
c.flags["raw"] = true
return nil, &optionsResponder{[]string{"GET", "HEAD", "OPTIONS", "PATCH", "DELETE"}}
}
switch c.verb {
case "HEAD", "GET": // Fetch (default)
return obj, nil
case "PATCH": // Update
if res, ok := obj.(Updatable); ok {
err := res.ApiUpdate(c)
if err != nil {
return nil, err
}
return obj, nil
}
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
case "DELETE": // Delete
if res, ok := obj.(Deletable); ok {
err := res.ApiDelete(c)
if err != nil {
return nil, err
}
return obj, nil
}
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
default:
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
}
}
// need to call list
if r.Action == nil {
return nil, ErrNotFound
}
if corsReq {
c.flags["raw"] = true
return nil, &optionsResponder{[]string{"GET", "HEAD", "OPTIONS", "POST", "DELETE"}}
}
switch c.verb {
case "HEAD", "GET": // List
if list := r.Action.List; list != nil {
return list.CallArg(c, c.params)
}
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
case "POST": // Create
if create := r.Action.Create; create != nil {
return create.CallArg(c, c.params)
}
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
case "DELETE": // Clear
if clear := r.Action.Clear; clear != nil {
return clear.CallArg(c, c.params)
}
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
default:
return nil, webutil.HttpError(http.StatusMethodNotAllowed)
}
}