Skip to content

Commit da2ab09

Browse files
committed
all request and response info is now recorded to apib doc file; still need to group resources together
1 parent 5fd21dc commit da2ab09

20 files changed

+587
-264
lines changed

Diff for: doc/action.go

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package doc
22

3-
import "text/template"
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"text/template"
7+
)
48

59
var (
610
actionTmpl *template.Template
7-
actionFmt = `### {{.Title}} [{{.HTTPMethod}}]
11+
actionFmt = `### {{.Title}} [{{.Method}}]
812
{{.Description}}
913
{{with .Request}}
1014
{{.Render}}{{end}}
@@ -19,13 +23,31 @@ func init() {
1923
type Action struct {
2024
Title string
2125
Description string
22-
HTTPMethod string
23-
Request *Request // status OK
24-
Response *Response
26+
Method httpMethod
27+
Request Request // status OK
28+
Response Response
2529

2630
// TODO: document non-OK requests ??
2731
}
2832

2933
func (a *Action) Render() string {
3034
return render(actionTmpl, a)
3135
}
36+
37+
func NewAction(req *http.Request, resp *httptest.ResponseRecorder) (*Action, error) {
38+
docReq, err := NewRequest(req)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
docResp := NewResponse(resp)
44+
45+
return &Action{
46+
Title: "Some Action",
47+
Description: "Some description.",
48+
Method: httpMethod(req.Method),
49+
Request: *docReq,
50+
Response: *docResp,
51+
}, nil
52+
53+
}

Diff for: doc/body.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import "text/template"
44

55
var (
66
bodyTmpl *template.Template
7-
bodyFmt = `
8-
+ Body
7+
bodyFmt = ` + Body
98
10-
{{.BodyStr}}
11-
9+
{{.FormattedJSON}}
1210
`
1311
)
1412

@@ -21,3 +19,13 @@ type Body []byte
2119
func (b *Body) Render() string {
2220
return render(bodyTmpl, b)
2321
}
22+
23+
// TODO: support other content-types
24+
func (b *Body) FormattedJSON() string {
25+
fbody, err := indentJSONBody(string(*b))
26+
if err != nil {
27+
panic(err.Error())
28+
}
29+
30+
return fbody
31+
}

Diff for: doc/doc.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const (
1616

1717
var (
1818
docTmpl *template.Template
19-
docFmt = `FORMAT: {{.Metadata.Format}}
20-
HOST: {{.Metadata.Host}}
19+
docFmt = `FORMAT: {{with .Metadata}}{{.Format}}
20+
HOST: {{.Host}}{{end}}
2121
2222
# {{.Title}}
2323
{{.Description}}
@@ -32,7 +32,7 @@ func init() {
3232
type Doc struct {
3333
Title string
3434
Description string
35-
Metadata *Metadata
35+
Metadata Metadata
3636
ResourceGroups []*ResourceGroup
3737
file *os.File
3838

@@ -57,14 +57,20 @@ func NewDoc(outDir string) (doc *Doc, err error) {
5757
doc = tmpDoc
5858
doc.file = fi
5959

60-
err = docTmpl.Execute(fi, doc)
61-
if err != nil {
62-
return
63-
}
64-
6560
return
6661
}
6762

63+
// TODO: add Resource to appropriate ResourceGroup,
64+
// not just to ResourceGroups[0]
65+
func (d *Doc) AddResource(resource *Resource) {
66+
group := d.ResourceGroups[0]
67+
group.Resources = append(group.Resources, *resource)
68+
}
69+
70+
func (d *Doc) Write() error {
71+
return docTmpl.Execute(d.file, d)
72+
}
73+
6874
func getPayload(req *http.Request) (body []byte, err error) {
6975
body, err = ioutil.ReadAll(req.Body)
7076
if err != nil {

Diff for: doc/request.go

+3-28
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package doc
22

33
import (
4-
"fmt"
54
"net/http"
65
"text/template"
76
)
87

98
var (
109
requestTmpl *template.Template
1110
requestFmt = `{{if or .Header .Body}}
12-
+ Request {{with .Header}}({{.ContentType}}){{end}}{{end}}
13-
{{.Header.Render}}
14-
{{.Body.Render}}
15-
`
11+
+ Request {{with .Header}}({{.ContentType}}){{end}}
12+
{{with .Header}}{{.Render}}{{end}}
13+
{{with .Body}}{{.Render}}{{end}}{{end}}`
1614
)
1715

1816
func init() {
@@ -58,26 +56,3 @@ func (r *Request) BodyStr() string {
5856

5957
return fbody
6058
}
61-
62-
func RecordRequest(doc *Doc, req *http.Request) error {
63-
body, err := getPayload(req)
64-
if err != nil {
65-
return err
66-
}
67-
68-
fmt.Println(body)
69-
70-
// err = doc.WriteRequestTitle("")
71-
// if err != nil {
72-
// return err
73-
// }
74-
75-
// err = doc.WriteHeaders(req.Header)
76-
// if err != nil {
77-
// return err
78-
// }
79-
80-
// return doc.WriteBody(string(body))
81-
82-
return nil
83-
}

Diff for: doc/resource.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,44 @@ var (
66
resourceTmpl *template.Template
77
resourceFmt = `## {{.Title}} [{{with .URL}}{{.ParameterizedPath}}{{end}}]
88
{{.Description}}
9-
{{with .URL}}{{with .Parameters}}+ Parameters
10-
{{range .}}
11-
{{.Render}}{{end}}{{end}}{{end}}{{range .Actions}}
9+
{{with .URL}}{{if .Parameters}}+ Parameters
10+
{{range .Parameters}}
11+
{{.Render}}{{end}}{{end}}{{end}}
12+
{{range .Actions}}
1213
{{.Render}}{{end}}`
1314
)
1415

1516
func init() {
1617
resourceTmpl = template.Must(template.New("resource").Parse(resourceFmt))
1718
}
1819

20+
type httpMethod string
21+
1922
type Resource struct {
2023
Title string
2124
Description string
2225
//Model *Model
2326
URL *URL
24-
Actions []*Action
27+
Actions map[httpMethod]*Action
2528

2629
// TODO:
2730
// Attributes
2831
}
2932

33+
func NewResource(urlPath string) *Resource {
34+
resource := &Resource{}
35+
resource.Actions = map[httpMethod]*Action{}
36+
return resource
37+
}
38+
39+
func (r *Resource) AddAction(action *Action) {
40+
if r.Actions == nil {
41+
r.Actions = map[httpMethod]*Action{}
42+
}
43+
44+
r.Actions[action.Method] = action
45+
}
46+
3047
func (r *Resource) Render() string {
3148
return render(resourceTmpl, r)
3249
}

Diff for: doc/resourcegroup.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func init() {
1818
type ResourceGroup struct {
1919
Title string
2020
Description string
21-
Resources []*Resource
21+
Resources []Resource
2222
}
2323

2424
func (rg *ResourceGroup) Render() string {

Diff for: doc/response.go

+8-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package doc
22

33
import (
4-
"net/http"
54
"net/http/httptest"
65
"text/template"
76
)
@@ -10,8 +9,8 @@ var (
109
responseTmpl *template.Template
1110
responseFmt = `
1211
+ Response {{.StatusCode}} {{with .ContentType}}({{.}}){{end}}
13-
{{.Header.Render}}
14-
{{.Body.Render}}`
12+
{{with .Header}}{{.Render}}{{end}}
13+
{{with .Body}}{{.Render}}{{end}}`
1514
)
1615

1716
func init() {
@@ -22,28 +21,19 @@ type Response struct {
2221
StatusCode int
2322
Description string
2423
Header Header
25-
Body []byte
24+
Body Body
2625

2726
// TODO:
2827
// Attributes
2928
// Schema
3029
}
3130

32-
func NewResponse(description string, w *httptest.ResponseRecorder) (*Response, error) {
33-
34-
body1, body2, err := cloneBody(w.Body)
35-
if err != nil {
36-
return nil, err
37-
}
38-
39-
w.Body = body1
40-
31+
func NewResponse(resp *httptest.ResponseRecorder) *Response {
4132
return &Response{
42-
StatusCode: w.Code,
43-
Description: description,
44-
Header: Header(w.Header()),
45-
Body: body2.Bytes(),
46-
}, nil
33+
StatusCode: resp.Code,
34+
Header: Header(resp.Header()),
35+
Body: resp.Body.Bytes(),
36+
}
4737
}
4838

4939
func (r *Response) Render() string {
@@ -62,21 +52,3 @@ func (r *Response) BodyStr() string {
6252

6353
return fbody
6454
}
65-
66-
func RecordResponse(doc *Doc, handler http.Handler, req *http.Request) (resp *httptest.ResponseRecorder, err error) {
67-
resp = httptest.NewRecorder()
68-
handler.ServeHTTP(resp, req)
69-
70-
// err = doc.WriteResponseTitle(resp.Code, resp.Header().Get("Content-Type"))
71-
// if err != nil {
72-
// return
73-
// }
74-
75-
// err = doc.WriteHeaders(resp.Header())
76-
// if err != nil {
77-
// return
78-
// }
79-
80-
// err = doc.WriteBody(string(resp.Body.String()))
81-
return
82-
}

Diff for: doc/response_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ func (t *suite) TestNewResponse_ResponseBodyIsCorrectlyCopied() {
1717
w := httptest.NewRecorder()
1818
testResponseHandler(w, req)
1919

20-
apiResp, err := NewResponse("example", w)
21-
t.Must(t.Nil(err))
20+
apiResp := NewResponse(w)
2221

2322
t.Equal(string(apiResp.Body), testResponseBody)
2423
}
@@ -31,8 +30,7 @@ func (t *suite) TestNewResponse_OriginalResponseBodyDoesNotChange() {
3130
w := httptest.NewRecorder()
3231
testResponseHandler(w, req)
3332

34-
_, err = NewResponse("example", w)
35-
t.Must(t.Nil(err))
33+
_ = NewResponse(w)
3634

3735
t.Equal(w.Body.String(), testRequestBody)
3836
}

Diff for: doc/tmp.go

+2-49
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,18 @@
11
package doc
22

3-
import (
4-
"fmt"
5-
"net/url"
6-
"strings"
7-
)
8-
93
var (
10-
tmpResourceCount = 0
11-
tmpResourceNames = []string{"Thing", "Foozle", "Bardy", "Wallet"}
12-
tmpDoc = &Doc{
4+
tmpDoc = &Doc{
135
Title: "JSON Placeholder API",
146
Description: "Fake Online REST API for Testing and Prototyping",
15-
Metadata: &Metadata{
7+
Metadata: Metadata{
168
Format: FORMAT,
179
Host: "http://jsonplaceholder.typicode.com",
1810
},
1911
ResourceGroups: []*ResourceGroup{
2012
&ResourceGroup{
2113
Title: "A Lovely Resource Group",
2214
Description: "All CRUD endpoints for A Lovely Resource Group",
23-
Resources: newTmpResources(1),
24-
},
25-
&ResourceGroup{
26-
Title: "An Awesome Resource Group",
27-
Description: "All non-CRUD endpoints",
28-
Resources: newTmpResources(2),
2915
},
3016
},
3117
}
3218
)
33-
34-
func newTmpResources(n int) []*Resource {
35-
resources := make([]*Resource, n)
36-
37-
for i := 0; i < n; i++ {
38-
resources[i] = newTmpResource()
39-
}
40-
return resources
41-
}
42-
43-
func newTmpResource() *Resource {
44-
tmpResourceCount += 1
45-
index := tmpResourceCount % len(tmpResourceNames)
46-
name := tmpResourceNames[index]
47-
48-
var query string
49-
if index == 0 {
50-
query = fmt.Sprintf("?a=0")
51-
} else if index == 1 {
52-
query = fmt.Sprintf("?b=hello+world")
53-
}
54-
55-
u, err := url.Parse(fmt.Sprintf("/%s/id/%d"+query, strings.ToLower(name), tmpResourceCount))
56-
if err != nil {
57-
panic(err.Error())
58-
}
59-
60-
return &Resource{
61-
Title: fmt.Sprintf("Resource #%d", tmpResourceCount),
62-
Description: "This resource is just like all the others.",
63-
URL: &URL{u, nil},
64-
}
65-
}

0 commit comments

Comments
 (0)