Skip to content

Commit

Permalink
Merge branch 'api' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
#	go.sum
  • Loading branch information
scorpiotzh committed Jul 22, 2024
2 parents bb6e269 + ebc659b commit 1ce675e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
37 changes: 37 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [Get Batch register Info](#get-batch-register-info)
* [Get Did Cell List](#get-did-cell-list)
* [Get Account Records Info V2](#get-account-records-info-v2)
* [Get Did Number](#get-did-number)

* [<em>Deprecated API List</em>](#deprecated-api-list)
* [<em>Get Account Basic Info And Records</em>](#get-account-basic-info-and-records-deprecated)
Expand Down Expand Up @@ -807,6 +808,42 @@ or json rpc style:
curl -X POST https://indexer-v1.did.id -d'{"jsonrpc": "2.0","id": 1,"method": "das_subAccountList","params": [{"account":"0x.bit","page":1,"size":20}]}'
```


### Get Did Number

**Request**
* host: `indexer-v1.did.id`
* path: `/v1/did/number`
* param: none

**Response**

```json
{
"err_no": 0,
"err_msg": "",
"data": {
"total": 0,
"tl_did": 0,
"sl_did": "0" ,
"dobs": 0
}
}
```

**Usage**

```shell
curl -X POST https://indexer-v1.did.id/v1/did/number
```

or json rpc style:

```shell
curl -X POST https://indexer-v1.did.id -d'{"jsonrpc": "2.0","id": 1,"method": "das_didNumber","params": []}'
```


## _Deprecated API List_

### _Get Account Basic Info And Records_ `Deprecated`
Expand Down
19 changes: 19 additions & 0 deletions dao/t_account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,22 @@ func (d *DbDao) AccountUpgrade(accountInfo tables.TableAccountInfo, didCellInfo
return nil
})
}

func (d *DbDao) GetTotalTLDid() (count int64, err error) {
err = d.db.Model(tables.TableAccountInfo{}).
Where("parent_account_id=''").Count(&count).Error
return
}

func (d *DbDao) GetTotalSLDid() (count int64, err error) {
err = d.db.Model(tables.TableAccountInfo{}).
Where("parent_account_id!=''").Count(&count).Error
return
}

func (d *DbDao) GetTotalDobs() (count int64, err error) {
timestamp := tables.GetDidCellRecycleExpiredAt()
err = d.db.Model(tables.TableDidCellInfo{}).
Where("expired_at>?", timestamp).Count(&count).Error
return
}
1 change: 1 addition & 0 deletions http_server/code/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type JsonRpcMethod = string

const (
MethodVersion JsonRpcMethod = "das_version"
MethodDidNumber JsonRpcMethod = "das_didNumber"
MethodServerInfo JsonRpcMethod = "das_serverInfo"

MethodSearchAccount JsonRpcMethod = "das_searchAccount"
Expand Down
90 changes: 90 additions & 0 deletions http_server/handle/did_number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package handle

import (
"encoding/json"
"fmt"
"github.com/dotbitHQ/das-lib/http_api"
"github.com/gin-gonic/gin"
"github.com/scorpiotzh/toolib"
"net/http"
)

type ReqDidNumber struct {
}

type RespDidNumber struct {
Total int64 `json:"total"`
TLDid int64 `json:"tl_did"`
SLDid int64 `json:"sl_did"`
Dobs int64 `json:"dobs"`
}

func (h *HttpHandle) JsonRpcDidNumber(p json.RawMessage, apiResp *http_api.ApiResp) {
var req []ReqDidNumber
err := json.Unmarshal(p, &req)
if err != nil {
log.Error("json.Unmarshal err:", err.Error())
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "params invalid")
return
}
if len(req) != 1 {
log.Error("len(req) is :", len(req))
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "params invalid")
return
}

if err = h.doDidNumber(&req[0], apiResp); err != nil {
log.Error("doDidNumber err:", err.Error())
}
}

func (h *HttpHandle) DidNumber(ctx *gin.Context) {
var (
funcName = "DidNumber"
req ReqDidNumber
apiResp http_api.ApiResp
err error
)

if err := ctx.ShouldBindJSON(&req); err != nil {
log.Error("ShouldBindJSON err: ", err.Error(), funcName)
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "params invalid")
ctx.JSON(http.StatusOK, apiResp)
return
}
log.Info("ApiReq:", funcName, toolib.JsonString(req))

if err = h.doDidNumber(&req, &apiResp); err != nil {
log.Error("doDidNumber err:", err.Error(), funcName)
}

ctx.JSON(http.StatusOK, apiResp)
}

func (h *HttpHandle) doDidNumber(req *ReqDidNumber, apiResp *http_api.ApiResp) error {
var resp RespDidNumber

tldid, err := h.DbDao.GetTotalTLDid()
if err != nil {
apiResp.ApiRespErr(http_api.ApiCodeError500, err.Error())
return fmt.Errorf("GetTotalTLDid err: %s", err.Error())
}
sldid, err := h.DbDao.GetTotalSLDid()
if err != nil {
apiResp.ApiRespErr(http_api.ApiCodeError500, err.Error())
return fmt.Errorf("GetTotalSLDid err: %s", err.Error())
}
dobs, err := h.DbDao.GetTotalDobs()
if err != nil {
apiResp.ApiRespErr(http_api.ApiCodeError500, err.Error())
return fmt.Errorf("GetTotalDobs err: %s", err.Error())
}

resp.TLDid = tldid
resp.SLDid = sldid
resp.Total = tldid + sldid
resp.Dobs = dobs

apiResp.ApiRespOK(resp)
return nil
}
2 changes: 2 additions & 0 deletions http_server/handle/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func (h *HttpHandle) QueryIndexer(ctx *gin.Context) {
h.JsonRpcSearchAccount(req.Params, &apiResp)
case code.MethodAddressAccount:
h.JsonRpcAddressAccount(req.Params, &apiResp)
case code.MethodDidNumber:
h.JsonRpcDidNumber(req.Params, &apiResp)
case code.MethodServerInfo:
h.JsonRpcServerInfo(req.Params, &apiResp)
case code.MethodAccountInfo:
Expand Down
1 change: 1 addition & 0 deletions http_server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (h *HttpServer) initRouter() {
{
//v1Indexer.POST("/search/account", code.DoMonitorLog(code.MethodSearchAccount), cacheHandle, h.H.SearchAccount)
//v1Indexer.POST("/address/account", code.DoMonitorLog(code.MethodAddressAccount), cacheHandle, h.H.AddressAccount)
v1Indexer.POST("/did/number", code.DoMonitorLog(code.MethodDidNumber), cacheHandle, h.H.DidNumber)
v1Indexer.POST("/server/info", code.DoMonitorLog(code.MethodServerInfo), cacheHandle, h.H.ServerInfo)
v1Indexer.POST("/account/info", code.DoMonitorLog(code.MethodAccountInfo), cacheHandle, h.H.AccountInfo)
v1Indexer.POST("/account/list", code.DoMonitorLog(code.MethodAccountList), cacheHandle, h.H.AccountList)
Expand Down

0 comments on commit 1ce675e

Please sign in to comment.