Skip to content

Commit

Permalink
api: supports GetRegion by hex key (#7160)
Browse files Browse the repository at this point in the history
close #7159

api: supports GetRegion by hex key

Signed-off-by: nolouch <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
nolouch and ti-chi-bot[bot] authored Sep 28, 2023
1 parent 7a9e566 commit 849d80d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ func (h *regionHandler) GetRegion(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
// decode hex if query has params with hex format
formatStr := r.URL.Query().Get("format")
if formatStr == "hex" {
keyBytes, err := hex.DecodeString(key)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
key = string(keyBytes)
}

regionInfo := rc.GetRegionByKey([]byte(key))
h.rd.JSON(w, http.StatusOK, NewAPIRegionInfo(regionInfo))
}
Expand Down
6 changes: 6 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ func (suite *regionTestSuite) TestRegion() {
suite.NoError(tu.ReadGetJSON(re, testDialClient, url, r2))
r2.Adjust()
suite.Equal(NewAPIRegionInfo(r), r2)

url = fmt.Sprintf("%s/region/key/%s?format=hex", suite.urlPrefix, hex.EncodeToString([]byte("a")))
r2 = &RegionInfo{}
suite.NoError(tu.ReadGetJSON(re, testDialClient, url, r2))
r2.Adjust()
suite.Equal(NewAPIRegionInfo(r), r2)
}

func (suite *regionTestSuite) TestRegionCheck() {
Expand Down

0 comments on commit 849d80d

Please sign in to comment.