@@ -257,7 +257,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
257257 c := api .contextPool .Get ().(APIContexter )
258258 c .Reset ()
259259 api .middlewareChain (c , w , r )
260- err := res .handleIndex (w , r , api .info )
260+ err := res .handleIndex (c , w , r , api .info )
261261 api .contextPool .Put (c )
262262 if err != nil {
263263 handleError (err , w , r , marshalers )
@@ -268,7 +268,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
268268 c := api .contextPool .Get ().(APIContexter )
269269 c .Reset ()
270270 api .middlewareChain (c , w , r )
271- err := res .handleRead (w , r , ps , api .info )
271+ err := res .handleRead (c , w , r , ps , api .info )
272272 api .contextPool .Put (c )
273273 if err != nil {
274274 handleError (err , w , r , marshalers )
@@ -285,7 +285,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
285285 c := api .contextPool .Get ().(APIContexter )
286286 c .Reset ()
287287 api .middlewareChain (c , w , r )
288- err := res .handleReadRelation (w , r , ps , api .info , relation )
288+ err := res .handleReadRelation (c , w , r , ps , api .info , relation )
289289 api .contextPool .Put (c )
290290 if err != nil {
291291 handleError (err , w , r , marshalers )
@@ -298,7 +298,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
298298 c := api .contextPool .Get ().(APIContexter )
299299 c .Reset ()
300300 api .middlewareChain (c , w , r )
301- err := res .handleLinked (api , w , r , ps , relation , api .info )
301+ err := res .handleLinked (c , api , w , r , ps , relation , api .info )
302302 api .contextPool .Put (c )
303303 if err != nil {
304304 handleError (err , w , r , marshalers )
@@ -311,7 +311,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
311311 c := api .contextPool .Get ().(APIContexter )
312312 c .Reset ()
313313 api .middlewareChain (c , w , r )
314- err := res .handleReplaceRelation (w , r , ps , relation )
314+ err := res .handleReplaceRelation (c , w , r , ps , relation )
315315 api .contextPool .Put (c )
316316 if err != nil {
317317 handleError (err , w , r , marshalers )
@@ -326,7 +326,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
326326 c := api .contextPool .Get ().(APIContexter )
327327 c .Reset ()
328328 api .middlewareChain (c , w , r )
329- err := res .handleAddToManyRelation (w , r , ps , relation )
329+ err := res .handleAddToManyRelation (c , w , r , ps , relation )
330330 api .contextPool .Put (c )
331331 if err != nil {
332332 handleError (err , w , r , marshalers )
@@ -339,7 +339,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
339339 c := api .contextPool .Get ().(APIContexter )
340340 c .Reset ()
341341 api .middlewareChain (c , w , r )
342- err := res .handleDeleteToManyRelation (w , r , ps , relation )
342+ err := res .handleDeleteToManyRelation (c , w , r , ps , relation )
343343 api .contextPool .Put (c )
344344 if err != nil {
345345 handleError (err , w , r , marshalers )
@@ -354,7 +354,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
354354 c := api .contextPool .Get ().(APIContexter )
355355 c .Reset ()
356356 api .middlewareChain (c , w , r )
357- err := res .handleCreate (w , r , api .prefix , api .info )
357+ err := res .handleCreate (c , w , r , api .prefix , api .info )
358358 api .contextPool .Put (c )
359359 if err != nil {
360360 handleError (err , w , r , marshalers )
@@ -365,7 +365,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
365365 c := api .contextPool .Get ().(APIContexter )
366366 c .Reset ()
367367 api .middlewareChain (c , w , r )
368- err := res .handleDelete (w , r , ps )
368+ err := res .handleDelete (c , w , r , ps )
369369 api .contextPool .Put (c )
370370 if err != nil {
371371 handleError (err , w , r , marshalers )
@@ -376,7 +376,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
376376 c := api .contextPool .Get ().(APIContexter )
377377 c .Reset ()
378378 api .middlewareChain (c , w , r )
379- err := res .handleUpdate (w , r , ps )
379+ err := res .handleUpdate (c , w , r , ps )
380380 api .contextPool .Put (c )
381381 if err != nil {
382382 handleError (err , w , r , marshalers )
@@ -388,26 +388,27 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
388388 return & res
389389}
390390
391- func buildRequest (r * http.Request ) Request {
391+ func buildRequest (c APIContexter , r * http.Request ) Request {
392392 req := Request {PlainRequest : r }
393393 params := make (map [string ][]string )
394394 for key , values := range r .URL .Query () {
395395 params [key ] = strings .Split (values [0 ], "," )
396396 }
397397 req .QueryParams = params
398398 req .Header = r .Header
399+ req .Context = c
399400 return req
400401}
401402
402- func (res * resource ) handleIndex (w http.ResponseWriter , r * http.Request , info information ) error {
403+ func (res * resource ) handleIndex (c APIContexter , w http.ResponseWriter , r * http.Request , info information ) error {
403404 pagination := newPaginationQueryParams (r )
404405 if pagination .isValid () {
405406 source , ok := res .source .(PaginatedFindAll )
406407 if ! ok {
407408 return NewHTTPError (nil , "Resource does not implement the PaginatedFindAll interface" , http .StatusNotFound )
408409 }
409410
410- count , response , err := source .PaginatedFindAll (buildRequest (r ))
411+ count , response , err := source .PaginatedFindAll (buildRequest (c , r ))
411412 if err != nil {
412413 return err
413414 }
@@ -424,18 +425,18 @@ func (res *resource) handleIndex(w http.ResponseWriter, r *http.Request, info in
424425 return NewHTTPError (nil , "Resource does not implement the FindAll interface" , http .StatusNotFound )
425426 }
426427
427- response , err := source .FindAll (buildRequest (r ))
428+ response , err := source .FindAll (buildRequest (c , r ))
428429 if err != nil {
429430 return err
430431 }
431432
432433 return respondWith (response , info , http .StatusOK , w , r , res .marshalers )
433434}
434435
435- func (res * resource ) handleRead (w http.ResponseWriter , r * http.Request , ps httprouter.Params , info information ) error {
436+ func (res * resource ) handleRead (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params , info information ) error {
436437 id := ps .ByName ("id" )
437438
438- response , err := res .source .FindOne (id , buildRequest (r ))
439+ response , err := res .source .FindOne (id , buildRequest (c , r ))
439440
440441 if err != nil {
441442 return err
@@ -444,10 +445,10 @@ func (res *resource) handleRead(w http.ResponseWriter, r *http.Request, ps httpr
444445 return respondWith (response , info , http .StatusOK , w , r , res .marshalers )
445446}
446447
447- func (res * resource ) handleReadRelation (w http.ResponseWriter , r * http.Request , ps httprouter.Params , info information , relation jsonapi.Reference ) error {
448+ func (res * resource ) handleReadRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params , info information , relation jsonapi.Reference ) error {
448449 id := ps .ByName ("id" )
449450
450- obj , err := res .source .FindOne (id , buildRequest (r ))
451+ obj , err := res .source .FindOne (id , buildRequest (c , r ))
451452 if err != nil {
452453 return err
453454 }
@@ -500,11 +501,11 @@ func (res *resource) handleReadRelation(w http.ResponseWriter, r *http.Request,
500501}
501502
502503// try to find the referenced resource and call the findAll Method with referencing resource id as param
503- func (res * resource ) handleLinked (api * API , w http.ResponseWriter , r * http.Request , ps httprouter.Params , linked jsonapi.Reference , info information ) error {
504+ func (res * resource ) handleLinked (c APIContexter , api * API , w http.ResponseWriter , r * http.Request , ps httprouter.Params , linked jsonapi.Reference , info information ) error {
504505 id := ps .ByName ("id" )
505506 for _ , resource := range api .resources {
506507 if resource .name == linked .Type {
507- request := buildRequest (r )
508+ request := buildRequest (c , r )
508509 request .QueryParams [res .name + "ID" ] = []string {id }
509510 request .QueryParams [res .name + "Name" ] = []string {linked .Name }
510511
@@ -555,7 +556,7 @@ func (res *resource) handleLinked(api *API, w http.ResponseWriter, r *http.Reque
555556
556557}
557558
558- func (res * resource ) handleCreate (w http.ResponseWriter , r * http.Request , prefix string , info information ) error {
559+ func (res * resource ) handleCreate (c APIContexter , w http.ResponseWriter , r * http.Request , prefix string , info information ) error {
559560 ctx , err := unmarshalRequest (r , res .marshalers )
560561 if err != nil {
561562 return err
@@ -578,7 +579,7 @@ func (res *resource) handleCreate(w http.ResponseWriter, r *http.Request, prefix
578579 //TODO create multiple objects not only one.
579580 newObj := newObjs .Index (0 ).Interface ()
580581
581- response , err := res .source .Create (newObj , buildRequest (r ))
582+ response , err := res .source .Create (newObj , buildRequest (c , r ))
582583 if err != nil {
583584 return err
584585 }
@@ -605,8 +606,8 @@ func (res *resource) handleCreate(w http.ResponseWriter, r *http.Request, prefix
605606 }
606607}
607608
608- func (res * resource ) handleUpdate (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) error {
609- obj , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (r ))
609+ func (res * resource ) handleUpdate (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params ) error {
610+ obj , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
610611 if err != nil {
611612 return err
612613 }
@@ -669,7 +670,7 @@ func (res *resource) handleUpdate(w http.ResponseWriter, r *http.Request, ps htt
669670
670671 updatingObj := updatingObjs .Index (0 ).Interface ()
671672
672- response , err := res .source .Update (updatingObj , buildRequest (r ))
673+ response , err := res .source .Update (updatingObj , buildRequest (c , r ))
673674
674675 if err != nil {
675676 return err
@@ -679,7 +680,7 @@ func (res *resource) handleUpdate(w http.ResponseWriter, r *http.Request, ps htt
679680 case http .StatusOK :
680681 updated := response .Result ()
681682 if updated == nil {
682- internalResponse , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (r ))
683+ internalResponse , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
683684 if err != nil {
684685 return err
685686 }
@@ -703,13 +704,13 @@ func (res *resource) handleUpdate(w http.ResponseWriter, r *http.Request, ps htt
703704 }
704705}
705706
706- func (res * resource ) handleReplaceRelation (w http.ResponseWriter , r * http.Request , ps httprouter.Params , relation jsonapi.Reference ) error {
707+ func (res * resource ) handleReplaceRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params , relation jsonapi.Reference ) error {
707708 var (
708709 err error
709710 editObj interface {}
710711 )
711712
712- response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (r ))
713+ response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
713714 if err != nil {
714715 return err
715716 }
@@ -737,22 +738,22 @@ func (res *resource) handleReplaceRelation(w http.ResponseWriter, r *http.Reques
737738 }
738739
739740 if resType == reflect .Struct {
740- _ , err = res .source .Update (reflect .ValueOf (editObj ).Elem ().Interface (), buildRequest (r ))
741+ _ , err = res .source .Update (reflect .ValueOf (editObj ).Elem ().Interface (), buildRequest (c , r ))
741742 } else {
742- _ , err = res .source .Update (editObj , buildRequest (r ))
743+ _ , err = res .source .Update (editObj , buildRequest (c , r ))
743744 }
744745
745746 w .WriteHeader (http .StatusNoContent )
746747 return err
747748}
748749
749- func (res * resource ) handleAddToManyRelation (w http.ResponseWriter , r * http.Request , ps httprouter.Params , relation jsonapi.Reference ) error {
750+ func (res * resource ) handleAddToManyRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params , relation jsonapi.Reference ) error {
750751 var (
751752 err error
752753 editObj interface {}
753754 )
754755
755- response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (r ))
756+ response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
756757 if err != nil {
757758 return err
758759 }
@@ -801,22 +802,22 @@ func (res *resource) handleAddToManyRelation(w http.ResponseWriter, r *http.Requ
801802 targetObj .AddToManyIDs (relation .Name , newIDs )
802803
803804 if resType == reflect .Struct {
804- _ , err = res .source .Update (reflect .ValueOf (targetObj ).Elem ().Interface (), buildRequest (r ))
805+ _ , err = res .source .Update (reflect .ValueOf (targetObj ).Elem ().Interface (), buildRequest (c , r ))
805806 } else {
806- _ , err = res .source .Update (targetObj , buildRequest (r ))
807+ _ , err = res .source .Update (targetObj , buildRequest (c , r ))
807808 }
808809
809810 w .WriteHeader (http .StatusNoContent )
810811
811812 return err
812813}
813814
814- func (res * resource ) handleDeleteToManyRelation (w http.ResponseWriter , r * http.Request , ps httprouter.Params , relation jsonapi.Reference ) error {
815+ func (res * resource ) handleDeleteToManyRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params , relation jsonapi.Reference ) error {
815816 var (
816817 err error
817818 editObj interface {}
818819 )
819- response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (r ))
820+ response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
820821 if err != nil {
821822 return err
822823 }
@@ -865,9 +866,9 @@ func (res *resource) handleDeleteToManyRelation(w http.ResponseWriter, r *http.R
865866 targetObj .DeleteToManyIDs (relation .Name , obsoleteIDs )
866867
867868 if resType == reflect .Struct {
868- _ , err = res .source .Update (reflect .ValueOf (targetObj ).Elem ().Interface (), buildRequest (r ))
869+ _ , err = res .source .Update (reflect .ValueOf (targetObj ).Elem ().Interface (), buildRequest (c , r ))
869870 } else {
870- _ , err = res .source .Update (targetObj , buildRequest (r ))
871+ _ , err = res .source .Update (targetObj , buildRequest (c , r ))
871872 }
872873
873874 w .WriteHeader (http .StatusNoContent )
@@ -883,8 +884,8 @@ func getPointerToStruct(oldObj interface{}) interface{} {
883884 return ptr .Interface ()
884885}
885886
886- func (res * resource ) handleDelete (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) error {
887- response , err := res .source .Delete (ps .ByName ("id" ), buildRequest (r ))
887+ func (res * resource ) handleDelete (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params ) error {
888+ response , err := res .source .Delete (ps .ByName ("id" ), buildRequest (c , r ))
888889 if err != nil {
889890 return err
890891 }
0 commit comments