This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
uncrawlable_handlers.go
92 lines (85 loc) · 2.27 KB
/
uncrawlable_handlers.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
package main
import (
"encoding/json"
"github.com/datatogether/api/apiutil"
"github.com/datatogether/core"
"net/http"
)
func UncrawlableHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
GetUncrawlableHandler(w, r)
case "PUT":
SaveUncrawlableHandler(w, r)
case "DELETE":
DeleteUncrawlableHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func UncrawlablesHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
ListUncrawlablesHandler(w, r)
case "PUT", "POST":
SaveUncrawlableHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func GetUncrawlableHandler(w http.ResponseWriter, r *http.Request) {
res := &core.Uncrawlable{}
args := &UncrawlablesGetParams{
Id: r.URL.Path[len("/uncrawlables/"):],
Url: r.FormValue("url"),
}
err := new(Uncrawlables).Get(args, res)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WriteResponse(w, res)
}
func ListUncrawlablesHandler(w http.ResponseWriter, r *http.Request) {
p := apiutil.PageFromRequest(r)
res := make([]*core.Uncrawlable, p.Size)
args := &UncrawlablesListParams{
Limit: p.Limit(),
Offset: p.Offset(),
OrderBy: "created",
}
err := new(Uncrawlables).List(args, &res)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WritePageResponse(w, res, r, p)
}
func SaveUncrawlableHandler(w http.ResponseWriter, r *http.Request) {
un := &core.Uncrawlable{}
if err := json.NewDecoder(r.Body).Decode(un); err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
res := &core.Uncrawlable{}
if err := new(Uncrawlables).Save(un, res); err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WriteResponse(w, res)
}
func DeleteUncrawlableHandler(w http.ResponseWriter, r *http.Request) {
un := &core.Uncrawlable{}
if err := json.NewDecoder(r.Body).Decode(un); err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
res := &core.Uncrawlable{}
if err := new(Uncrawlables).Save(un, res); err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WriteResponse(w, res)
}