Documentation
¶
Overview ¶
Package apidoc provides html document builder for http requests and responses.
Index ¶
- func DefaultStyle() *Element
- func JsonResponse(resp *http.Response) *Element
- func JsonResponseFrom(h http.Handler, r *http.Request) *Element
- func RawRequest(r *http.Request) *Element
- func RawResponse(resp *http.Response) *Element
- func RawResponseFrom(h http.Handler, r *http.Request) *Element
- type Doc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultStyle ¶
func DefaultStyle() *Element
func JsonResponse ¶
JsonResponse converts the response to a <pre> element including the body.
func JsonResponseFrom ¶
JsonResponseFrom records the response of the request on the handler and returns same as JsonResponse.
Example ¶
package main
import (
"fmt"
"net/http"
"os"
"github.com/gregoryv/web/apidoc"
)
func main() {
r, _ := http.NewRequest("GET", "/", nil)
apidoc.JsonResponseFrom(
http.HandlerFunc(someRouter),
r,
).WriteTo(os.Stdout)
}
func someRouter(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"animal": "Goat","age": 10, "friendly": "hell no, not this one"}`)
}
if r.Method == "POST" {
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{"message": "added"}`)
}
}
Output: <pre class="response">HTTP/1.1 200 OK { "animal": "Goat", "age": 10, "friendly": "hell no, not this one" }</pre>
func RawRequest ¶
RawRequest returns a <pre> element with the request. The request is reusable afterwards.
func RawResponse ¶
RawResponse dumps the response including body
func RawResponseFrom ¶
RawResponseFrom returns the full response from the request to the given handler
Example ¶
package main
import (
"fmt"
"net/http"
"os"
"github.com/gregoryv/web/apidoc"
)
func main() {
r, _ := http.NewRequest("GET", "/", nil)
element := apidoc.RawResponseFrom(
http.HandlerFunc(someRouter),
r,
)
element.WriteTo(os.Stdout)
}
func someRouter(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"animal": "Goat","age": 10, "friendly": "hell no, not this one"}`)
}
if r.Method == "POST" {
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{"message": "added"}`)
}
}
Output: <pre class="response">HTTP/1.1 200 OK {"animal": "Goat","age": 10, "friendly": "hell no, not this one"}</pre>
Types ¶
type Doc ¶
type Doc struct {
// Requst used when calling a response generator, eg. Response()
// or JsonResponse()
*http.Request
// contains filtered or unexported fields
}
func (*Doc) JsonResponse ¶
func (d *Doc) JsonResponse() *Element
JsonResponse returns a tidy json response from the last used request
Example ¶
package main
import (
"fmt"
"net/http"
"os"
"github.com/gregoryv/web/apidoc"
)
func main() {
doc := apidoc.NewDoc(http.HandlerFunc(someRouter))
doc.NewRequest("GET", "/", nil).WriteTo(os.Stdout)
doc.JsonResponse().WriteTo(os.Stdout)
}
func someRouter(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"animal": "Goat","age": 10, "friendly": "hell no, not this one"}`)
}
if r.Method == "POST" {
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{"message": "added"}`)
}
}
Output: <pre class="request">HTTP/1.1 GET / </pre><pre class="response">HTTP/1.1 200 OK { "animal": "Goat", "age": 10, "friendly": "hell no, not this one" }</pre>
func (*Doc) NewRequest ¶
NewRequest returns a <pre> element of a request based on the arguments. For more advanced requests use Doc.Use()