Skip to content

Commit

Permalink
revert request context changes to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
potterbm-cb committed Nov 7, 2024
1 parent 98f4705 commit 1eaae78
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 185 deletions.
2 changes: 0 additions & 2 deletions examples/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ func NewBlockchainRouter(
networkAPIController := server.NewNetworkAPIController(
networkAPIService,
asserter,
nil,
)

blockAPIService := services.NewBlockAPIService(network)
blockAPIController := server.NewBlockAPIController(
blockAPIService,
asserter,
nil,
)

return server.NewRouter(networkAPIController, blockAPIController)
Expand Down
26 changes: 6 additions & 20 deletions server/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package server

import (
"context"
"encoding/json"
"net/http"
"strings"
Expand All @@ -29,21 +28,18 @@ import (
// A AccountAPIController binds http requests to an api service and writes the service results to
// the http response
type AccountAPIController struct {
service AccountAPIServicer
asserter *asserter.Asserter
contextFromRequest func(*http.Request) context.Context
service AccountAPIServicer
asserter *asserter.Asserter
}

// NewAccountAPIController creates a default api controller
func NewAccountAPIController(
s AccountAPIServicer,
asserter *asserter.Asserter,
contextFromRequest func(*http.Request) context.Context,
) Router {
return &AccountAPIController{
service: s,
asserter: asserter,
contextFromRequest: contextFromRequest,
service: s,
asserter: asserter,
}
}

Expand All @@ -65,16 +61,6 @@ func (c *AccountAPIController) Routes() Routes {
}
}

func (c *AccountAPIController) ContextFromRequest(r *http.Request) context.Context {
ctx := r.Context()

if c.contextFromRequest != nil {
ctx = c.contextFromRequest(r)
}

return ctx
}

// AccountBalance - Get an Account's Balance
func (c *AccountAPIController) AccountBalance(w http.ResponseWriter, r *http.Request) {
accountBalanceRequest := &types.AccountBalanceRequest{}
Expand All @@ -95,7 +81,7 @@ func (c *AccountAPIController) AccountBalance(w http.ResponseWriter, r *http.Req
return
}

result, serviceErr := c.service.AccountBalance(c.ContextFromRequest(r), accountBalanceRequest)
result, serviceErr := c.service.AccountBalance(r.Context(), accountBalanceRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down Expand Up @@ -125,7 +111,7 @@ func (c *AccountAPIController) AccountCoins(w http.ResponseWriter, r *http.Reque
return
}

result, serviceErr := c.service.AccountCoins(c.ContextFromRequest(r), accountCoinsRequest)
result, serviceErr := c.service.AccountCoins(r.Context(), accountCoinsRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
26 changes: 6 additions & 20 deletions server/api_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package server

import (
"context"
"encoding/json"
"net/http"
"strings"
Expand All @@ -29,21 +28,18 @@ import (
// A BlockAPIController binds http requests to an api service and writes the service results to the
// http response
type BlockAPIController struct {
service BlockAPIServicer
asserter *asserter.Asserter
contextFromRequest func(*http.Request) context.Context
service BlockAPIServicer
asserter *asserter.Asserter
}

// NewBlockAPIController creates a default api controller
func NewBlockAPIController(
s BlockAPIServicer,
asserter *asserter.Asserter,
contextFromRequest func(*http.Request) context.Context,
) Router {
return &BlockAPIController{
service: s,
asserter: asserter,
contextFromRequest: contextFromRequest,
service: s,
asserter: asserter,
}
}

Expand All @@ -65,16 +61,6 @@ func (c *BlockAPIController) Routes() Routes {
}
}

func (c *BlockAPIController) ContextFromRequest(r *http.Request) context.Context {
ctx := r.Context()

if c.contextFromRequest != nil {
ctx = c.contextFromRequest(r)
}

return ctx
}

// Block - Get a Block
func (c *BlockAPIController) Block(w http.ResponseWriter, r *http.Request) {
blockRequest := &types.BlockRequest{}
Expand All @@ -95,7 +81,7 @@ func (c *BlockAPIController) Block(w http.ResponseWriter, r *http.Request) {
return
}

result, serviceErr := c.service.Block(c.ContextFromRequest(r), blockRequest)
result, serviceErr := c.service.Block(r.Context(), blockRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down Expand Up @@ -126,7 +112,7 @@ func (c *BlockAPIController) BlockTransaction(w http.ResponseWriter, r *http.Req
}

result, serviceErr := c.service.BlockTransaction(
c.ContextFromRequest(r),
r.Context(),
blockTransactionRequest,
)
if serviceErr != nil {
Expand Down
24 changes: 5 additions & 19 deletions server/api_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package server

import (
"context"
"encoding/json"
"net/http"
"strings"
Expand All @@ -29,21 +28,18 @@ import (
// A CallAPIController binds http requests to an api service and writes the service results to the
// http response
type CallAPIController struct {
service CallAPIServicer
asserter *asserter.Asserter
contextFromRequest func(*http.Request) context.Context
service CallAPIServicer
asserter *asserter.Asserter
}

// NewCallAPIController creates a default api controller
func NewCallAPIController(
s CallAPIServicer,
asserter *asserter.Asserter,
contextFromRequest func(*http.Request) context.Context,
) Router {
return &CallAPIController{
service: s,
asserter: asserter,
contextFromRequest: contextFromRequest,
service: s,
asserter: asserter,
}
}

Expand All @@ -59,16 +55,6 @@ func (c *CallAPIController) Routes() Routes {
}
}

func (c *CallAPIController) ContextFromRequest(r *http.Request) context.Context {
ctx := r.Context()

if c.contextFromRequest != nil {
ctx = c.contextFromRequest(r)
}

return ctx
}

// Call - Make a Network-Specific Procedure Call
func (c *CallAPIController) Call(w http.ResponseWriter, r *http.Request) {
callRequest := &types.CallRequest{}
Expand All @@ -89,7 +75,7 @@ func (c *CallAPIController) Call(w http.ResponseWriter, r *http.Request) {
return
}

result, serviceErr := c.service.Call(c.ContextFromRequest(r), callRequest)
result, serviceErr := c.service.Call(r.Context(), callRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
38 changes: 12 additions & 26 deletions server/api_construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package server

import (
"context"
"encoding/json"
"net/http"
"strings"
Expand All @@ -29,21 +28,18 @@ import (
// A ConstructionAPIController binds http requests to an api service and writes the service results
// to the http response
type ConstructionAPIController struct {
service ConstructionAPIServicer
asserter *asserter.Asserter
contextFromRequest func(*http.Request) context.Context
service ConstructionAPIServicer
asserter *asserter.Asserter
}

// NewConstructionAPIController creates a default api controller
func NewConstructionAPIController(
s ConstructionAPIServicer,
asserter *asserter.Asserter,
contextFromRequest func(*http.Request) context.Context,
) Router {
return &ConstructionAPIController{
service: s,
asserter: asserter,
contextFromRequest: contextFromRequest,
service: s,
asserter: asserter,
}
}

Expand Down Expand Up @@ -101,16 +97,6 @@ func (c *ConstructionAPIController) Routes() Routes {
}
}

func (c *ConstructionAPIController) ContextFromRequest(r *http.Request) context.Context {
ctx := r.Context()

if c.contextFromRequest != nil {
ctx = c.contextFromRequest(r)
}

return ctx
}

// ConstructionCombine - Create Network Transaction from Signatures
func (c *ConstructionAPIController) ConstructionCombine(w http.ResponseWriter, r *http.Request) {
constructionCombineRequest := &types.ConstructionCombineRequest{}
Expand All @@ -132,7 +118,7 @@ func (c *ConstructionAPIController) ConstructionCombine(w http.ResponseWriter, r
}

result, serviceErr := c.service.ConstructionCombine(
c.ContextFromRequest(r),
r.Context(),
constructionCombineRequest,
)
if serviceErr != nil {
Expand Down Expand Up @@ -165,7 +151,7 @@ func (c *ConstructionAPIController) ConstructionDerive(w http.ResponseWriter, r
}

result, serviceErr := c.service.ConstructionDerive(
c.ContextFromRequest(r),
r.Context(),
constructionDeriveRequest,
)
if serviceErr != nil {
Expand Down Expand Up @@ -198,7 +184,7 @@ func (c *ConstructionAPIController) ConstructionHash(w http.ResponseWriter, r *h
}

result, serviceErr := c.service.ConstructionHash(
c.ContextFromRequest(r),
r.Context(),
constructionHashRequest,
)
if serviceErr != nil {
Expand Down Expand Up @@ -231,7 +217,7 @@ func (c *ConstructionAPIController) ConstructionMetadata(w http.ResponseWriter,
}

result, serviceErr := c.service.ConstructionMetadata(
c.ContextFromRequest(r),
r.Context(),
constructionMetadataRequest,
)
if serviceErr != nil {
Expand Down Expand Up @@ -264,7 +250,7 @@ func (c *ConstructionAPIController) ConstructionParse(w http.ResponseWriter, r *
}

result, serviceErr := c.service.ConstructionParse(
c.ContextFromRequest(r),
r.Context(),
constructionParseRequest,
)
if serviceErr != nil {
Expand Down Expand Up @@ -297,7 +283,7 @@ func (c *ConstructionAPIController) ConstructionPayloads(w http.ResponseWriter,
}

result, serviceErr := c.service.ConstructionPayloads(
c.ContextFromRequest(r),
r.Context(),
constructionPayloadsRequest,
)
if serviceErr != nil {
Expand Down Expand Up @@ -330,7 +316,7 @@ func (c *ConstructionAPIController) ConstructionPreprocess(w http.ResponseWriter
}

result, serviceErr := c.service.ConstructionPreprocess(
c.ContextFromRequest(r),
r.Context(),
constructionPreprocessRequest,
)
if serviceErr != nil {
Expand Down Expand Up @@ -363,7 +349,7 @@ func (c *ConstructionAPIController) ConstructionSubmit(w http.ResponseWriter, r
}

result, serviceErr := c.service.ConstructionSubmit(
c.ContextFromRequest(r),
r.Context(),
constructionSubmitRequest,
)
if serviceErr != nil {
Expand Down
24 changes: 5 additions & 19 deletions server/api_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package server

import (
"context"
"encoding/json"
"net/http"
"strings"
Expand All @@ -29,21 +28,18 @@ import (
// A EventsAPIController binds http requests to an api service and writes the service results to the
// http response
type EventsAPIController struct {
service EventsAPIServicer
asserter *asserter.Asserter
contextFromRequest func(*http.Request) context.Context
service EventsAPIServicer
asserter *asserter.Asserter
}

// NewEventsAPIController creates a default api controller
func NewEventsAPIController(
s EventsAPIServicer,
asserter *asserter.Asserter,
contextFromRequest func(*http.Request) context.Context,
) Router {
return &EventsAPIController{
service: s,
asserter: asserter,
contextFromRequest: contextFromRequest,
service: s,
asserter: asserter,
}
}

Expand All @@ -59,16 +55,6 @@ func (c *EventsAPIController) Routes() Routes {
}
}

func (c *EventsAPIController) ContextFromRequest(r *http.Request) context.Context {
ctx := r.Context()

if c.contextFromRequest != nil {
ctx = c.contextFromRequest(r)
}

return ctx
}

// EventsBlocks - [INDEXER] Get a range of BlockEvents
func (c *EventsAPIController) EventsBlocks(w http.ResponseWriter, r *http.Request) {
eventsBlocksRequest := &types.EventsBlocksRequest{}
Expand All @@ -89,7 +75,7 @@ func (c *EventsAPIController) EventsBlocks(w http.ResponseWriter, r *http.Reques
return
}

result, serviceErr := c.service.EventsBlocks(c.ContextFromRequest(r), eventsBlocksRequest)
result, serviceErr := c.service.EventsBlocks(r.Context(), eventsBlocksRequest)
if serviceErr != nil {
EncodeJSONResponse(serviceErr, http.StatusInternalServerError, w)

Expand Down
Loading

0 comments on commit 1eaae78

Please sign in to comment.