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"
|
|
|
|
|
2016-07-22 21:31:33 -04:00
|
|
|
"github.com/google/jsonapi"
|
2015-07-10 16:50:51 -04:00
|
|
|
)
|
|
|
|
|
2015-07-12 13:46:51 -04:00
|
|
|
func main() {
|
2015-07-31 19:30:54 -04:00
|
|
|
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)
|
2015-07-31 19:30:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-07-31 19:30:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 21:47:43 -05:00
|
|
|
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
|
2017-01-20 20:55:59 -05:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/blogs", nil)
|
2015-07-12 13:46:51 -04:00
|
|
|
|
2017-02-16 23:25:19 -05:00
|
|
|
req.Header.Set(headerAccept, jsonapi.MediaType)
|
2015-07-12 13:46:51 -04:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2017-01-20 20:55:59 -05:00
|
|
|
fmt.Println("============ start list ===========")
|
2015-07-12 13:46:51 -04:00
|
|
|
http.DefaultServeMux.ServeHTTP(w, req)
|
2017-01-20 20:55:59 -05:00
|
|
|
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
|
|
|
|
2017-01-20 20:55:59 -05: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
|
2017-01-20 20:55:59 -05:00
|
|
|
req, _ = http.NewRequest(http.MethodGet, "/blogs?id=1", nil)
|
2015-07-19 13:32:07 -04:00
|
|
|
|
2017-02-16 23:25:19 -05:00
|
|
|
req.Header.Set(headerAccept, jsonapi.MediaType)
|
2015-07-19 13:32:07 -04:00
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
|
2017-01-20 20:55:59 -05:00
|
|
|
fmt.Println("============ start show ===========")
|
2015-07-19 13:32:07 -04:00
|
|
|
http.DefaultServeMux.ServeHTTP(w, req)
|
2017-01-20 20:55:59 -05:00
|
|
|
fmt.Println("============ stop show ===========")
|
2015-07-19 13:32:07 -04:00
|
|
|
|
|
|
|
jsonReply, _ = ioutil.ReadAll(w.Body)
|
|
|
|
|
2017-01-26 18:25:13 -05:00
|
|
|
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
|
2017-02-16 21:47:43 -05:00
|
|
|
blog := fixtureBlogCreate(1)
|
2015-07-12 13:46:51 -04:00
|
|
|
in := bytes.NewBuffer(nil)
|
|
|
|
jsonapi.MarshalOnePayloadEmbedded(in, blog)
|
|
|
|
|
2017-01-20 20:55:59 -05:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/blogs", in)
|
2015-07-12 13:46:51 -04:00
|
|
|
|
2017-02-16 23:25:19 -05:00
|
|
|
req.Header.Set(headerAccept, jsonapi.MediaType)
|
2015-07-12 13:46:51 -04:00
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
|
2017-01-20 20:55:59 -05:00
|
|
|
fmt.Println("============ start create ===========")
|
2015-07-12 13:46:51 -04:00
|
|
|
http.DefaultServeMux.ServeHTTP(w, req)
|
2017-01-20 20:55:59 -05:00
|
|
|
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)
|
|
|
|
|
2017-01-26 18:25:13 -05:00
|
|
|
fmt.Println("============ jsonapi response from create ===========")
|
|
|
|
fmt.Println(buf.String())
|
|
|
|
fmt.Println("============== end raw jsonapi response =============")
|
|
|
|
|
|
|
|
// echo
|
|
|
|
blogs := []interface{}{
|
2017-02-16 21:47:43 -05:00
|
|
|
fixtureBlogCreate(1),
|
|
|
|
fixtureBlogCreate(2),
|
|
|
|
fixtureBlogCreate(3),
|
2017-01-26 18:25:13 -05:00
|
|
|
}
|
|
|
|
in = bytes.NewBuffer(nil)
|
|
|
|
jsonapi.MarshalManyPayload(in, blogs)
|
|
|
|
|
|
|
|
req, _ = http.NewRequest(http.MethodPut, "/blogs", in)
|
|
|
|
|
2017-02-16 23:25:19 -05:00
|
|
|
req.Header.Set(headerAccept, jsonapi.MediaType)
|
2017-01-26 18:25:13 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-01-26 18:25:13 -05:00
|
|
|
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 =================")
|
|
|
|
}
|