REST API Response to client delay, but the data successfully added to database

5/29/2020

The REST API build using Go, that connect to Postgresql in Docker and Google Kubernetes Engine. There's a problem when doing a POST Request in the client (Web or Mobile) it is successfully added in the database but the data in client didn't updated after refresh several times.

It takes 1-3 minutes then the data updated in client after a refresh, another case if I call GET request in postman first, the client will be directly updated.

POST

func addBlock(ir iris.Context) {
    request := md.Block{}
    err := ir.ReadJSON(&request)
    if err != nil {
        ir.StatusCode(iris.StatusBadRequest)
        ir.WriteString(err.Error())
        return
    }

    if request.Block_name == "" {
        ir.StatusCode(iris.StatusBadRequest)
        ir.Write([]byte(`{"Message": "StatusBadRequest"}`))
        return
    }

    db.Create(&request)

    a := db.NewRecord(request)
    fmt.Println(a)
    if a == true {
        ir.StatusCode(iris.StatusBadRequest)
    } else {
        status := md.ResponseStatus{}
        status.Status = "Success"
        status.Message = "add Blocks"
        status.Id = request.Block_id
        json.NewEncoder(ir).Encode(status)
    }
}

GET

func getBlocksByfarmID(ir iris.Context) {
    farmID, err := ir.Params().GetInt("farm_id")
    if err != nil {
        farmID = 0
    }

    var blockModel []md.Block
    db.Where("farm_id = ?", farmID).Find(&blockModel)

    if len(blockModel) == 0 {
        status := md.ResponseStatus{}
        ir.StatusCode(iris.StatusNotFound)
        status.Status = "Not Found"
        status.Message = fmt.Sprintf("No Found block with farm id : %d", farmID)
        json.NewEncoder(ir).Encode(status)
    } else {
        json.NewEncoder(ir).Encode(blockModel)
    }
}

Client Android using Retrofit2 GET

@Headers({"Content-Type: application/json"})
@GET("farm/{farm_id}/blocks")
Call<List<BlocksModel>> getBlockByFarmID(@Path("farm_id") String farm_id);

POST

@Headers({"Content-Type: application/json"})
@POST("farm/blocks/{action}")
Call<AddBlock> blockFunc(@Path("action") String action,
                         @Body String jsonBody);

I'm expecting the client realtime update when data added to the database, without waiting 1-3 minutes or call the API using postman first.

-- Arrival Sentosa
api
docker
go
kubernetes
postman

0 Answers