Skip to content

Commit

Permalink
Merge pull request #5 from machinefi/device-http-api-1
Browse files Browse the repository at this point in the history
device data http api
  • Loading branch information
huangzhiran authored Aug 28, 2024
2 parents 60a6043 + 3f418f2 commit b378147
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/sequencer/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewHttpServer(ctx context.Context, jwk *ioconnect.JWK, clientMgr *clients.M

s.engine.POST("/issue_vc", s.issueJWTCredential)
s.engine.POST("/device/:imei/confirm", s.verifyToken, s.confirmDevice)
s.engine.POST("/device/:imei/data", s.verifyToken, s.receiveDeviceData)
s.engine.GET("/device/:imei/query", s.verifyToken, s.queryDeviceState)
s.engine.GET("/didDoc", s.didDoc)

Expand Down Expand Up @@ -84,6 +85,26 @@ func (s *httpServer) verifyToken(c *gin.Context) {
c.Request = c.Request.WithContext(ctx)
}

func (s *httpServer) receiveDeviceData(c *gin.Context) {
imei := c.Param("imei")
data, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Wrap(err, "failed to read request body")))
return
}
e := &event.DeviceData{}
if err := e.Unmarshal(data); err != nil {
c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Wrap(err, "failed to unmarshal request body")))
return
}
e.Imei = imei
if err := e.Handle(s.ctx); err != nil {
c.JSON(http.StatusInternalServerError, apitypes.NewErrRsp(errors.Wrap(err, "failed to receive device data")))
return
}
c.Status(http.StatusOK)
}

func (s *httpServer) confirmDevice(c *gin.Context) {
imei := c.Param("imei")
data, err := io.ReadAll(c.Request.Body)
Expand Down

0 comments on commit b378147

Please sign in to comment.