⚙️ Add summary to client API

This commit is contained in:
Ettore Di Giacinto
2022-02-08 21:27:39 +01:00
parent 3bf4316e28
commit aaa97f3e7b
5 changed files with 56 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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())

View File

@@ -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
pkg/types/summary.go Normal file
View File

@@ -0,0 +1,21 @@
// Copyright © 2022 Ettore Di Giacinto <mudler@mocaccino.org>
//
// 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
}