Skip to content

Commit

Permalink
Merge pull request #63 from JackalLabs/marston/backport-api
Browse files Browse the repository at this point in the history
Back-porting API
  • Loading branch information
dahn510 authored Aug 11, 2024
2 parents 2b5d067 + 7287559 commit 4c273d3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
- uses: actions/[email protected]
- uses: actions/setup-go@v5
with:
go-version: 1.21.0 # The Go version to download (if necessary) and use.
go-version: 1.22.2 # The Go version to download (if necessary) and use.
- name: Build CLI
run: go install
2 changes: 1 addition & 1 deletion .github/workflows/cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: actions/[email protected]
- uses: actions/setup-go@v5
with:
go-version: 1.21.0 # The Go version to download (if necessary) and use.
go-version: 1.22.0 # The Go version to download (if necessary) and use.
- name: Get Coverage
run: |
go test ./... -timeout=30m -cover -coverprofile coverage.out
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/setup-go@v5
with:
go-version: 1.21.0
go-version: 1.22.2
- uses: actions/[email protected]
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/[email protected]
- uses: actions/setup-go@v5
with:
go-version: 1.20.2 # The Go version to download (if necessary) and use.
go-version: 1.22.2 # The Go version to download (if necessary) and use.
- name: Build CLI
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/[email protected]
- uses: actions/setup-go@v5
with:
go-version: 1.20.2 # The Go version to download (if necessary) and use.
go-version: 1.22.2 # The Go version to download (if necessary) and use.
- name: Test
shell: bash
run: make test-unit
70 changes: 70 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package api

import (
"context"
"net/http"

"github.com/JackalLabs/sequoia/api/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/cosmos-go-wallet/client"
storageTypes "github.com/jackalLabs/canine-chain/v4/x/storage/types"
"github.com/rs/zerolog/log"
)

func SpaceHandler(c *client.Client, address string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
queryClient := storageTypes.NewQueryClient(c.GRPCConn)

params := &storageTypes.QueryProvider{
Address: address,
}
res, err := queryClient.Provider(context.Background(), params)
if err != nil {
v := types.ErrorResponse{
Error: err.Error(),
}
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
log.Error().Err(err)
}
return
}

totalSpace := res.Provider.Totalspace

fsparams := &storageTypes.QueryFreeSpace{
Address: address,
}
fsres, err := queryClient.FreeSpace(context.Background(), fsparams)
if err != nil {
v := types.ErrorResponse{
Error: err.Error(),
}
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(v)
if err != nil {
log.Error().Err(err)
}
return
}

freeSpace := fsres.Space

ttint, ok := sdk.NewIntFromString(totalSpace)
if !ok {
return
}

v := types.SpaceResponse{
Total: ttint.Int64(),
Free: freeSpace,
Used: ttint.Int64() - freeSpace,
}

err = json.NewEncoder(w).Encode(v)
if err != nil {
log.Error().Err(err)
}
}
}
2 changes: 2 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func (a *API) Serve(f *file_system.FileSystem, p *proofs.Prover, wallet *wallet.
r.HandleFunc("/download/{merkle}", DownloadFileHandler(f))

r.HandleFunc("/list", ListFilesHandler(f))
r.HandleFunc("/api/client/list", ListFilesHandler(f))
r.HandleFunc("/api/data/fids", LegacyListFilesHandler(f))
r.HandleFunc("/api/client/space", SpaceHandler(wallet.Client, wallet.AccAddress()))

r.HandleFunc("/ipfs/peers", IPFSListPeers(f))
r.HandleFunc("/ipfs/hosts", IPFSListHosts(f))
Expand Down
6 changes: 6 additions & 0 deletions api/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type VersionResponse struct {
ChainID string `json:"chain-id"`
}

type SpaceResponse struct {
Total int64 `json:"total_space"`
Used int64 `json:"used_space"`
Free int64 `json:"free_space"`
}

type NetworkResponse struct {
GRPCStatus string `json:"grpc-status"`
RPCStatus *coretypes.ResultStatus `json:"rpc-status"`
Expand Down

0 comments on commit 4c273d3

Please sign in to comment.