| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"io/ioutil" |
| 9 |
"net/http" |
| 10 |
"net/http/httptest" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/google/jsonapi" |
| 14 |
) |
| 15 |
|
| 16 |
func main() { |
| 17 |
jsonapi.Instrumentation = func(r *jsonapi.Runtime, eventType jsonapi.Event, callGUID string, dur time.Duration) { |
| 18 |
metricPrefix := r.Value("instrument").(string) |
| 19 |
|
| 20 |
if eventType == jsonapi.UnmarshalStart { |
| 21 |
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now()) |
| 22 |
} |
| 23 |
|
| 24 |
if eventType == jsonapi.UnmarshalStop { |
| 25 |
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to unmarshal payload\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now(), dur) |
| 26 |
} |
| 27 |
|
| 28 |
if eventType == jsonapi.MarshalStart { |
| 29 |
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now()) |
| 30 |
} |
| 31 |
|
| 32 |
if eventType == jsonapi.MarshalStop { |
| 33 |
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to marshal payload\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now(), dur) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
exampleHandler := &ExampleHandler{} |
| 38 |
http.HandleFunc("/blogs", exampleHandler.ServeHTTP) |
| 39 |
exerciseHandler() |
| 40 |
} |
| 41 |
|
| 42 |
func exerciseHandler() { |
| 43 |
|
| 44 |
req, _ := http.NewRequest(http.MethodGet, "/blogs", nil) |
| 45 |
|
| 46 |
req.Header.Set(headerAccept, jsonapi.MediaType) |
| 47 |
|
| 48 |
w := httptest.NewRecorder() |
| 49 |
|
| 50 |
fmt.Println("============ start list ===========") |
| 51 |
http.DefaultServeMux.ServeHTTP(w, req) |
| 52 |
fmt.Println("============ stop list ===========") |
| 53 |
|
| 54 |
jsonReply, _ := ioutil.ReadAll(w.Body) |
| 55 |
|
| 56 |
fmt.Println("============ jsonapi response from list ===========") |
| 57 |
fmt.Println(string(jsonReply)) |
| 58 |
fmt.Println("============== end raw jsonapi from list =============") |
| 59 |
|
| 60 |
|
| 61 |
req, _ = http.NewRequest(http.MethodGet, "/blogs?id=1", nil) |
| 62 |
|
| 63 |
req.Header.Set(headerAccept, jsonapi.MediaType) |
| 64 |
|
| 65 |
w = httptest.NewRecorder() |
| 66 |
|
| 67 |
fmt.Println("============ start show ===========") |
| 68 |
http.DefaultServeMux.ServeHTTP(w, req) |
| 69 |
fmt.Println("============ stop show ===========") |
| 70 |
|
| 71 |
jsonReply, _ = ioutil.ReadAll(w.Body) |
| 72 |
|
| 73 |
fmt.Println("============ jsonapi response from show ===========") |
| 74 |
fmt.Println(string(jsonReply)) |
| 75 |
fmt.Println("============== end raw jsonapi from show =============") |
| 76 |
|
| 77 |
|
| 78 |
blog := fixtureBlogCreate(1) |
| 79 |
in := bytes.NewBuffer(nil) |
| 80 |
jsonapi.MarshalOnePayloadEmbedded(in, blog) |
| 81 |
|
| 82 |
req, _ = http.NewRequest(http.MethodPost, "/blogs", in) |
| 83 |
|
| 84 |
req.Header.Set(headerAccept, jsonapi.MediaType) |
| 85 |
|
| 86 |
w = httptest.NewRecorder() |
| 87 |
|
| 88 |
fmt.Println("============ start create ===========") |
| 89 |
http.DefaultServeMux.ServeHTTP(w, req) |
| 90 |
fmt.Println("============ stop create ===========") |
| 91 |
|
| 92 |
buf := bytes.NewBuffer(nil) |
| 93 |
io.Copy(buf, w.Body) |
| 94 |
|
| 95 |
fmt.Println("============ jsonapi response from create ===========") |
| 96 |
fmt.Println(buf.String()) |
| 97 |
fmt.Println("============== end raw jsonapi response =============") |
| 98 |
|
| 99 |
|
| 100 |
blogs := []interface{}{ |
| 101 |
fixtureBlogCreate(1), |
| 102 |
fixtureBlogCreate(2), |
| 103 |
fixtureBlogCreate(3), |
| 104 |
} |
| 105 |
in = bytes.NewBuffer(nil) |
| 106 |
jsonapi.MarshalPayload(in, blogs) |
| 107 |
|
| 108 |
req, _ = http.NewRequest(http.MethodPut, "/blogs", in) |
| 109 |
|
| 110 |
req.Header.Set(headerAccept, jsonapi.MediaType) |
| 111 |
|
| 112 |
w = httptest.NewRecorder() |
| 113 |
|
| 114 |
fmt.Println("============ start echo ===========") |
| 115 |
http.DefaultServeMux.ServeHTTP(w, req) |
| 116 |
fmt.Println("============ stop echo ===========") |
| 117 |
|
| 118 |
buf = bytes.NewBuffer(nil) |
| 119 |
io.Copy(buf, w.Body) |
| 120 |
|
| 121 |
fmt.Println("============ jsonapi response from create ===========") |
| 122 |
fmt.Println(buf.String()) |
| 123 |
fmt.Println("============== end raw jsonapi response =============") |
| 124 |
|
| 125 |
responseBlog := new(Blog) |
| 126 |
|
| 127 |
jsonapi.UnmarshalPayload(buf, responseBlog) |
| 128 |
|
| 129 |
out := bytes.NewBuffer(nil) |
| 130 |
json.NewEncoder(out).Encode(responseBlog) |
| 131 |
|
| 132 |
fmt.Println("================ Viola! Converted back our Blog struct =================") |
| 133 |
fmt.Println(string(out.Bytes())) |
| 134 |
fmt.Println("================ end marshal materialized Blog struct =================") |
| 135 |
} |