Переглянути джерело

:gear: Add summary to client API

Ettore Di Giacinto 3 роки тому
батько
коміт
aaa97f3e7b
5 змінених файлів з 56 додано та 5 видалено
  1. 10 4
      api/api.go
  2. 17 0
      api/client/client.go
  3. 7 0
      api/client/client_test.go
  4. 1 1
      cmd/api.go
  5. 21 0
      pkg/types/summary.go

+ 10 - 4
api/api.go

@@ -86,10 +86,16 @@ func API(ctx context.Context, l string, defaultInterval, timeout time.Duration,
 
 		blockchain := ledger.Index()
 
-		return c.JSON(http.StatusOK, struct {
-			Files, Machines, Users, Services, BlockChain, OnChainNodes, Peers int
-			NodeID                                                            string
-		}{files, machines, users, services, blockchain, onChainNodes, p2pPeers, nodeID})
+		return c.JSON(http.StatusOK, types.Summary{
+			Files:        files,
+			Machines:     machines,
+			Users:        users,
+			Services:     services,
+			BlockChain:   blockchain,
+			OnChainNodes: onChainNodes,
+			Peers:        p2pPeers,
+			NodeID:       nodeID,
+		})
 	})
 
 	ec.GET("/api/machines", func(c echo.Context) error {

+ 17 - 0
api/client/client.go

@@ -40,6 +40,7 @@ const (
 	serviceURL    = "/api/services"
 	blockchainURL = "/api/blockchain"
 	ledgerURL     = "/api/ledger"
+	summaryURL    = "/api/summary"
 	fileURL       = "/api/files"
 )
 
@@ -157,6 +158,22 @@ func (c *Client) Ledger() (data map[string]map[string]blockchain.Data, err error
 	return
 }
 
+func (c *Client) Summary() (data types.Summary, err error) {
+	res, err := c.do(http.MethodGet, summaryURL, nil)
+	if err != nil {
+		return
+	}
+	defer res.Body.Close()
+	body, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return data, err
+	}
+	if err = json.Unmarshal(body, &data); err != nil {
+		return data, err
+	}
+	return
+}
+
 func (c *Client) Blockchain() (data blockchain.Block, err error) {
 	res, err := c.do(http.MethodGet, blockchainURL, nil)
 	if err != nil {

+ 7 - 0
api/client/client_test.go

@@ -52,6 +52,13 @@ var _ = Describe("Client", func() {
 			testBucket = randStringBytes(10)
 		})
 
+		It("Summary returns some info", func() {
+			Eventually(func() string {
+				s, _ := c.Summary()
+				return s.NodeID
+			}, 100*time.Second, 1*time.Second).ShouldNot(BeEmpty())
+		})
+
 		It("Puts string data", func() {
 			err := c.Put(testBucket, "foo", "bar")
 			Expect(err).ToNot(HaveOccurred())

+ 1 - 1
cmd/api.go

@@ -35,7 +35,7 @@ A simple UI interface is available to display network data.`,
 			&cli.StringFlag{
 				Name:  "listen",
 				Value: ":8080",
-				Usage: "Listening address",
+				Usage: "Listening address. To listen to a socket, prefix with unix://, e.g. unix:///socket.path",
 			},
 		),
 		Action: func(c *cli.Context) error {

+ 21 - 0
pkg/types/summary.go

@@ -0,0 +1,21 @@
+// Copyright © 2022 Ettore Di Giacinto <[email protected]>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+type Summary struct {
+	Files, Machines, Users, Services, BlockChain, OnChainNodes, Peers int
+	NodeID                                                            string
+}