forked from Finschia/finschia-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
324 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package rest | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
) | ||
|
||
type commander struct { | ||
storeName string | ||
cdc *wire.Codec | ||
parser sdk.ParseAccount | ||
} | ||
|
||
func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, parser sdk.ParseAccount) func(http.ResponseWriter, *http.Request) { | ||
c := commander{storeName, cdc, parser} | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
addr := vars["address"] | ||
bz, err := hex.DecodeString(addr) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
key := sdk.Address(bz) | ||
|
||
res, err := client.Query(key, c.storeName) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(fmt.Sprintf("Could't query account. Error: %s", err.Error()))) | ||
return | ||
} | ||
|
||
// the query will return empty if there is no data for this account | ||
if len(res) == 0 { | ||
w.WriteHeader(http.StatusNoContent) | ||
return | ||
} | ||
|
||
// parse out the value | ||
account, err := c.parser(res) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(fmt.Sprintf("Could't parse query result. Result: %s. Error: %s", res, err.Error()))) | ||
return | ||
} | ||
|
||
// print out whole account | ||
output, err := json.MarshalIndent(account, "", " ") | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(fmt.Sprintf("Could't marshall query result. Error: %s", err.Error()))) | ||
return | ||
} | ||
|
||
w.Write(output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package rest | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
auth "github.com/cosmos/cosmos-sdk/x/auth/commands" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, storeName string) { | ||
r.HandleFunc("/accounts/{address}", QueryAccountRequestHandler(storeName, cdc, auth.GetParseAccount(cdc))).Methods("GET") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package rest | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func RegisterRoutes(r *mux.Router, cdc *wire.Codec) { | ||
r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc)).Methods("POST") | ||
} |
Oops, something went wrong.