Refactor code according to code review

This commit is contained in:
HaoyangLiu 2018-09-02 21:42:17 +08:00
parent 8e032f850a
commit 9a1dac2f87
4 changed files with 38 additions and 28 deletions

View File

@ -266,8 +266,8 @@ type RecoverKeyBody struct {
Seed string `json:"seed"`
}
// RecoverResuestHandler is the handler of creating seed in swagger rest server
func RecoverResuestHandler(w http.ResponseWriter, r *http.Request) {
// RecoverKeyResuestHandler is the handler of creating seed in swagger rest server
func RecoverKeyResuestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
var m RecoverKeyBody
@ -275,14 +275,14 @@ func RecoverResuestHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
@ -290,21 +290,21 @@ func RecoverResuestHandler(w http.ResponseWriter, r *http.Request) {
info, err := kb.CreateKey(name, m.Seed, m.Password)
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
keyOutput, err := Bech32KeyOutput(info)
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
bz, err := cdc.MarshalJSON(keyOutput)
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

View File

@ -33,7 +33,7 @@ func Commands() *cobra.Command {
func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/keys", QueryKeysRequestHandler).Methods("GET")
r.HandleFunc("/keys", AddNewKeyRequestHandler).Methods("POST")
r.HandleFunc("/keys/{name}/recover", RecoverResuestHandler).Methods("POST")
r.HandleFunc("/keys/{name}/recover", RecoverKeyResuestHandler).Methods("POST")
r.HandleFunc("/keys/{name}/sign", SignResuest).Methods("POST")
r.HandleFunc("/keys/{name}", GetKeyRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", UpdateKeyRequestHandler).Methods("PUT")

View File

@ -4,17 +4,19 @@ Due to the rest handlers and related data structures are distributed in many sub
## Steps
* Install the command line tool first.
```
go get github.com/rakyll/statik
```
* Directly Edit API docs: client/lcd/swaggerui/swagger.json
* Edit API docs within this [website](https://app.swaggerhub.com). Please refer to this [link](https://app.swaggerhub.com/help/index) for how to use the about website to edit API docs.
* Download swagger.json and replace the old swagger.json under client/lcd/swaggerui folds
* Regenerate statik.go file
```
statik -src=client/lcd/swaggerui -dest=client/lcd
```
1. Install the command line tool first.
```
go get github.com/rakyll/statik
```
2. Edit API docs
1. Directly Edit API docs manually: `client/lcd/swaggerui/swagger.jso`
2. Edit API docs within this [SwaggerHub](https://app.swaggerhub.com). Please refer to this [link](https://app.swaggerhub.com/help/index) for how to use the about website to edit API docs.
3. Download `swagger.json` and replace the old `swagger.json` under `client/lcd/swaggerui` folds
4. Regenerate `client/lcd/statik/statik.go` file
```
statik -src=client/lcd/swaggerui -dest=client/lcd
```
5. Compile new gaiacli
```
make install
```

View File

@ -43,12 +43,11 @@ func TestKeys(t *testing.T) {
defer cleanup()
// recover key
recoverKeyURL := fmt.Sprintf("/keys/%s/recover", "test_recover")
seedRecover := "divorce meat banana embody near until uncover wait uniform capital crawl test praise cloud foil monster garbage hedgehog wrong skate there bonus box odor"
passwordRecover := "1234567890"
jsonStrRecover := []byte(fmt.Sprintf(`{"seed":"%s", "password":"%s"}`, seedRecover, passwordRecover))
res, body := Request(t, port, "POST", recoverKeyURL, jsonStrRecover)
require.Equal(t, http.StatusOK, res.StatusCode, body)
res, body := doRecoverKey(t, port, seed)
var response keys.KeyOutput
err := wire.Cdc.UnmarshalJSON([]byte(body), &response)
require.Nil(t, err, body)
reg, err := regexp.Compile(`([a-z]+ ){12}`)
require.Nil(t, err)
match := reg.MatchString(seed)
@ -788,6 +787,15 @@ func doSend(t *testing.T, port, seed, name, password string, addr sdk.AccAddress
return receiveAddr, resultTx
}
func doRecoverKey(t *testing.T, port, seed string) (*http.Response, string) {
recoverKeyURL := fmt.Sprintf("/keys/%s/recover", "test_recover")
passwordRecover := "1234567890"
jsonStrRecover := []byte(fmt.Sprintf(`{"seed":"%s", "password":"%s"}`, seed, passwordRecover))
res, body := Request(t, port, "POST", recoverKeyURL, jsonStrRecover)
require.Equal(t, http.StatusOK, res.StatusCode, body)
return res, body
}
func doIBCTransfer(t *testing.T, port, seed, name, password string, addr sdk.AccAddress) (resultTx ctypes.ResultBroadcastTxCommit) {
// create receive address
kb := client.MockKeyBase()