jsonapi/examples/app.go

136 lines
3.7 KiB
Go
Raw Normal View History

2015-07-10 16:50:51 -04:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
2015-07-19 13:32:07 -04:00
"io/ioutil"
2015-07-10 16:50:51 -04:00
"net/http"
"net/http/httptest"
"time"
"github.com/google/jsonapi"
2015-07-10 16:50:51 -04:00
)
2015-07-12 13:46:51 -04:00
func main() {
jsonapi.Instrumentation = func(r *jsonapi.Runtime, eventType jsonapi.Event, callGUID string, dur time.Duration) {
metricPrefix := r.Value("instrument").(string)
if eventType == jsonapi.UnmarshalStart {
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now())
}
if eventType == jsonapi.UnmarshalStop {
2016-03-27 14:44:52 -04:00
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to unmarshal payload\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now(), dur)
}
if eventType == jsonapi.MarshalStart {
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now())
}
if eventType == jsonapi.MarshalStop {
2016-03-27 14:44:52 -04:00
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to marshal payload\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now(), dur)
}
}
exampleHandler := &ExampleHandler{}
http.HandleFunc("/blogs", exampleHandler.ServeHTTP)
2015-07-12 13:46:51 -04:00
exerciseHandler()
2015-07-10 16:50:51 -04:00
}
2015-07-12 13:46:51 -04:00
func exerciseHandler() {
2015-07-19 13:32:07 -04:00
// list
req, _ := http.NewRequest(http.MethodGet, "/blogs", nil)
2015-07-12 13:46:51 -04:00
req.Header.Set("Accept", jsonapi.MediaType)
2015-07-12 13:46:51 -04:00
w := httptest.NewRecorder()
fmt.Println("============ start list ===========")
2015-07-12 13:46:51 -04:00
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop list ===========")
2015-07-12 13:46:51 -04:00
2015-07-19 13:32:07 -04:00
jsonReply, _ := ioutil.ReadAll(w.Body)
2015-07-12 13:46:51 -04:00
fmt.Println("============ jsonapi response from list ===========")
2015-07-19 13:32:07 -04:00
fmt.Println(string(jsonReply))
2015-07-12 13:46:51 -04:00
fmt.Println("============== end raw jsonapi from list =============")
2015-07-19 13:32:07 -04:00
// show
req, _ = http.NewRequest(http.MethodGet, "/blogs?id=1", nil)
2015-07-19 13:32:07 -04:00
req.Header.Set("Accept", jsonapi.MediaType)
2015-07-19 13:32:07 -04:00
w = httptest.NewRecorder()
fmt.Println("============ start show ===========")
2015-07-19 13:32:07 -04:00
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop show ===========")
2015-07-19 13:32:07 -04:00
jsonReply, _ = ioutil.ReadAll(w.Body)
fmt.Println("============ jsonapi response from show ===========")
2015-07-19 13:32:07 -04:00
fmt.Println(string(jsonReply))
fmt.Println("============== end raw jsonapi from show =============")
// create
blog := fixtureBlogCreate(1)
2015-07-12 13:46:51 -04:00
in := bytes.NewBuffer(nil)
jsonapi.MarshalOnePayloadEmbedded(in, blog)
req, _ = http.NewRequest(http.MethodPost, "/blogs", in)
2015-07-12 13:46:51 -04:00
req.Header.Set("Accept", jsonapi.MediaType)
2015-07-12 13:46:51 -04:00
w = httptest.NewRecorder()
fmt.Println("============ start create ===========")
2015-07-12 13:46:51 -04:00
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop create ===========")
2015-07-12 13:46:51 -04:00
2015-07-19 13:32:07 -04:00
buf := bytes.NewBuffer(nil)
2015-07-12 13:46:51 -04:00
io.Copy(buf, w.Body)
fmt.Println("============ jsonapi response from create ===========")
fmt.Println(buf.String())
fmt.Println("============== end raw jsonapi response =============")
// echo
blogs := []interface{}{
fixtureBlogCreate(1),
fixtureBlogCreate(2),
fixtureBlogCreate(3),
}
in = bytes.NewBuffer(nil)
jsonapi.MarshalManyPayload(in, blogs)
req, _ = http.NewRequest(http.MethodPut, "/blogs", in)
req.Header.Set("Accept", jsonapi.MediaType)
w = httptest.NewRecorder()
fmt.Println("============ start echo ===========")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop echo ===========")
buf = bytes.NewBuffer(nil)
io.Copy(buf, w.Body)
fmt.Println("============ jsonapi response from create ===========")
2015-07-12 13:46:51 -04:00
fmt.Println(buf.String())
fmt.Println("============== end raw jsonapi response =============")
responseBlog := new(Blog)
jsonapi.UnmarshalPayload(buf, responseBlog)
out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(responseBlog)
fmt.Println("================ Viola! Converted back our Blog struct =================")
fmt.Println(string(out.Bytes()))
2015-07-12 13:46:51 -04:00
fmt.Println("================ end marshal materialized Blog struct =================")
}