From d9741da43bc265448bb4df0eb327f6d2c0a59521 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Mon, 22 Mar 2021 11:53:24 +0100 Subject: [PATCH] Add API versioning --- api/api.go | 44 +++++++++++++++++++++++--------------------- api/api_test.go | 12 ++++++------ api/swagger.yml | 4 ++-- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/api/api.go b/api/api.go index 90e147b..926bc86 100644 --- a/api/api.go +++ b/api/api.go @@ -50,38 +50,40 @@ func NewAPI( hermezAddress: consts.HermezAddress, } + v1 := server.Group("/v1") + // Add coordinator endpoints if coordinatorEndpoints { // Account - server.POST("/account-creation-authorization", a.postAccountCreationAuth) - server.GET("/account-creation-authorization/:hezEthereumAddress", a.getAccountCreationAuth) + v1.POST("/account-creation-authorization", a.postAccountCreationAuth) + v1.GET("/account-creation-authorization/:hezEthereumAddress", a.getAccountCreationAuth) // Transaction - server.POST("/transactions-pool", a.postPoolTx) - server.GET("/transactions-pool/:id", a.getPoolTx) + v1.POST("/transactions-pool", a.postPoolTx) + v1.GET("/transactions-pool/:id", a.getPoolTx) } // Add explorer endpoints if explorerEndpoints { // Account - server.GET("/accounts", a.getAccounts) - server.GET("/accounts/:accountIndex", a.getAccount) - server.GET("/exits", a.getExits) - server.GET("/exits/:batchNum/:accountIndex", a.getExit) + v1.GET("/accounts", a.getAccounts) + v1.GET("/accounts/:accountIndex", a.getAccount) + v1.GET("/exits", a.getExits) + v1.GET("/exits/:batchNum/:accountIndex", a.getExit) // Transaction - server.GET("/transactions-history", a.getHistoryTxs) - server.GET("/transactions-history/:id", a.getHistoryTx) + v1.GET("/transactions-history", a.getHistoryTxs) + v1.GET("/transactions-history/:id", a.getHistoryTx) // Status - server.GET("/batches", a.getBatches) - server.GET("/batches/:batchNum", a.getBatch) - server.GET("/full-batches/:batchNum", a.getFullBatch) - server.GET("/slots", a.getSlots) - server.GET("/slots/:slotNum", a.getSlot) - server.GET("/bids", a.getBids) - server.GET("/state", a.getState) - server.GET("/config", a.getConfig) - server.GET("/tokens", a.getTokens) - server.GET("/tokens/:id", a.getToken) - server.GET("/coordinators", a.getCoordinators) + v1.GET("/batches", a.getBatches) + v1.GET("/batches/:batchNum", a.getBatch) + v1.GET("/full-batches/:batchNum", a.getFullBatch) + v1.GET("/slots", a.getSlots) + v1.GET("/slots/:slotNum", a.getSlot) + v1.GET("/bids", a.getBids) + v1.GET("/state", a.getState) + v1.GET("/config", a.getConfig) + v1.GET("/tokens", a.getTokens) + v1.GET("/tokens/:id", a.getToken) + v1.GET("/coordinators", a.getCoordinators) } return a, nil diff --git a/api/api_test.go b/api/api_test.go index b2ceeaa..001aed1 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -40,8 +40,8 @@ type Pendinger interface { New() Pendinger } -const apiAddr = ":4010" -const apiURL = "http://localhost" + apiAddr + "/" +const apiPort = "4010" +const apiURL = "http://localhost:" + apiPort + "/v1/" var SetBlockchain = ` Type: Blockchain @@ -258,7 +258,7 @@ func TestMain(m *testing.M) { panic(err) } // Start server - listener, err := net.Listen("tcp", apiAddr) //nolint:gosec + listener, err := net.Listen("tcp", ":"+apiPort) //nolint:gosec if err != nil { panic(err) } @@ -622,7 +622,7 @@ func TestTimeout(t *testing.T) { apiGinTO := gin.Default() finishWait := make(chan interface{}) startWait := make(chan interface{}) - apiGinTO.GET("/wait", func(c *gin.Context) { + apiGinTO.GET("/v1/wait", func(c *gin.Context) { cancel, err := apiConnConTO.Acquire() defer cancel() require.NoError(t, err) @@ -650,9 +650,9 @@ func TestTimeout(t *testing.T) { require.NoError(t, err) client := &http.Client{} - httpReq, err := http.NewRequest("GET", "http://localhost:4444/tokens", nil) + httpReq, err := http.NewRequest("GET", "http://localhost:4444/v1/tokens", nil) require.NoError(t, err) - httpReqWait, err := http.NewRequest("GET", "http://localhost:4444/wait", nil) + httpReqWait, err := http.NewRequest("GET", "http://localhost:4444/v1/wait", nil) require.NoError(t, err) // Request that will get timed out var wg sync.WaitGroup diff --git a/api/swagger.yml b/api/swagger.yml index 2feb1b2..a1e70e8 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -60,9 +60,9 @@ externalDocs: url: 'https://hermez.io' servers: - description: Hosted mock up - url: https://apimock.hermez.network + url: https://apimock.hermez.network/v1 - description: Localhost mock Up - url: http://localhost:4010 + url: http://localhost:4010/v1 tags: - name: Coordinator description: Endpoints used by the nodes running in coordinator mode. They are used to interact with the network.