Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 143 additions & 100 deletions code/go/0chain.net/blobbercore/convert/responseHandler.go
Original file line number Diff line number Diff line change
@@ -1,161 +1,204 @@
package convert

import (
"context"
"encoding/json"
"github.com/0chain/blobber/code/go/0chain.net/core/common"

stats2 "github.com/0chain/blobber/code/go/0chain.net/blobbercore/stats"

"github.com/0chain/blobber/code/go/0chain.net/blobbercore/allocation"
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/blobberHTTP"
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/blobbergrpc"
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/reference"
)

func GetAllocationResponseHandler(resp *blobbergrpc.GetAllocationResponse) *allocation.Allocation {
return GRPCAllocationToAllocation(resp.Allocation)
func GetAllocationResponseCreator(resp interface{}) *blobbergrpc.GetAllocationResponse {
if resp == nil {
return nil
}

alloc, _ := resp.(*allocation.Allocation)
return &blobbergrpc.GetAllocationResponse{Allocation: AllocationToGRPCAllocation(alloc)}
}

func GetFileMetaDataResponseHandler(resp *blobbergrpc.GetFileMetaDataResponse) map[string]interface{} {
var collaborators []reference.Collaborator
for _, c := range resp.Collaborators {
collaborators = append(collaborators, *GRPCCollaboratorToCollaborator(c))
func GetFileMetaDataResponseCreator(httpResp interface{}) *blobbergrpc.GetFileMetaDataResponse {
if httpResp == nil {
return nil
}

result := FileRefGRPCToFileRef(resp.MetaData).GetListingData(context.Background())
result["collaborators"] = collaborators
return result
}
r, _ := httpResp.(map[string]interface{})

func GetFileStatsResponseHandler(resp *blobbergrpc.GetFileStatsResponse) map[string]interface{} {
ctx := context.Background()
result := FileRefGRPCToFileRef(resp.MetaData).GetListingData(ctx)
var resp blobbergrpc.GetFileMetaDataResponse
collaborators, _ := r["collaborators"].([]reference.Collaborator)
for _, c := range collaborators {
resp.Collaborators = append(resp.Collaborators, CollaboratorToGRPCCollaborator(&c))
}

statsMap := make(map[string]interface{})
statsBytes, _ := json.Marshal(FileStatsGRPCToFileStats(resp.Stats))
_ = json.Unmarshal(statsBytes, &statsMap)
resp.MetaData = FileRefToFileRefGRPC(reference.ListingDataToRef(r))
return &resp
}

for k, v := range statsMap {
result[k] = v
func GetFileStatsResponseCreator(r interface{}) *blobbergrpc.GetFileStatsResponse {
if r == nil {
return nil
}

return result
httpResp, _ := r.(map[string]interface{})

var resp blobbergrpc.GetFileStatsResponse
resp.MetaData = FileRefToFileRefGRPC(reference.ListingDataToRef(httpResp))

respRaw, _ := json.Marshal(httpResp)
var stats stats2.FileStats
_ = json.Unmarshal(respRaw, &stats)
resp.Stats = FileStatsToFileStatsGRPC(&stats)

return &resp
}

func ListEntitesResponseHandler(resp *blobbergrpc.ListEntitiesResponse) *blobberHTTP.ListResult {
ctx := context.Background()
var entities []map[string]interface{}
for i := range resp.Entities {
entities = append(entities, FileRefGRPCToFileRef(resp.Entities[i]).GetListingData(ctx))
func ListEntitesResponseCreator(r interface{}) *blobbergrpc.ListEntitiesResponse {
if r == nil {
return nil
}

return &blobberHTTP.ListResult{
AllocationRoot: resp.AllocationRoot,
Meta: FileRefGRPCToFileRef(resp.MetaData).GetListingData(ctx),
Entities: entities,
httpResp, _ := r.(*blobberHTTP.ListResult)

var resp blobbergrpc.ListEntitiesResponse
for i := range httpResp.Entities {
resp.Entities = append(resp.Entities, FileRefToFileRefGRPC(reference.ListingDataToRef(httpResp.Entities[i])))
}

resp.MetaData = FileRefToFileRefGRPC(reference.ListingDataToRef(httpResp.Meta))
resp.AllocationRoot = httpResp.AllocationRoot
return &resp
}

func GetReferencePathResponseHandler(getReferencePathResponse *blobbergrpc.GetReferencePathResponse) *blobberHTTP.ReferencePathResult {
var recursionCount int
return &blobberHTTP.ReferencePathResult{
ReferencePath: ReferencePathGRPCToReferencePath(&recursionCount, getReferencePathResponse.ReferencePath),
LatestWM: WriteMarkerGRPCToWriteMarker(getReferencePathResponse.LatestWM),
func GetReferencePathResponseCreator(r interface{}) *blobbergrpc.GetReferencePathResponse {
if r == nil {
return nil
}

httpResp, _ := r.(*blobberHTTP.ReferencePathResult)
var resp blobbergrpc.GetReferencePathResponse

var recursionCount int
resp.LatestWM = WriteMarkerToWriteMarkerGRPC(httpResp.LatestWM)
resp.ReferencePath = ReferencePathToReferencePathGRPC(&recursionCount, httpResp.ReferencePath)
return &resp
}

func GetObjectPathResponseHandler(getObjectPathResponse *blobbergrpc.GetObjectPathResponse) *blobberHTTP.ObjectPathResult {
ctx := context.Background()
path := FileRefGRPCToFileRef(getObjectPathResponse.ObjectPath.Path).GetListingData(ctx)
var pathList []map[string]interface{}
for _, pl := range getObjectPathResponse.ObjectPath.PathList {
pathList = append(pathList, FileRefGRPCToFileRef(pl).GetListingData(ctx))
func GetObjectTreeResponseCreator(r interface{}) *blobbergrpc.GetObjectTreeResponse {
if r == nil {
return nil
}
path["list"] = pathList

return &blobberHTTP.ObjectPathResult{
ObjectPath: &reference.ObjectPath{
RootHash: getObjectPathResponse.ObjectPath.RootHash,
Meta: FileRefGRPCToFileRef(getObjectPathResponse.ObjectPath.Meta).GetListingData(ctx),
Path: path,
FileBlockNum: getObjectPathResponse.ObjectPath.FileBlockNum,
},
LatestWM: WriteMarkerGRPCToWriteMarker(getObjectPathResponse.LatestWriteMarker),
}
}
httpResp, _ := r.(*blobberHTTP.ReferencePathResult)
var resp blobbergrpc.GetObjectTreeResponse

func GetObjectTreeResponseHandler(getObjectTreeResponse *blobbergrpc.GetObjectTreeResponse) *blobberHTTP.ReferencePathResult {
var recursionCount int
return &blobberHTTP.ReferencePathResult{
ReferencePath: ReferencePathGRPCToReferencePath(&recursionCount, getObjectTreeResponse.ReferencePath),
LatestWM: WriteMarkerGRPCToWriteMarker(getObjectTreeResponse.LatestWM),
}
resp.LatestWM = WriteMarkerToWriteMarkerGRPC(httpResp.LatestWM)
resp.ReferencePath = ReferencePathToReferencePathGRPC(&recursionCount, httpResp.ReferencePath)
return &resp
}

func CommitWriteResponseHandler(resp *blobbergrpc.CommitResponse) *blobberHTTP.CommitResult {
return &blobberHTTP.CommitResult{
AllocationRoot: resp.AllocationRoot,
WriteMarker: WriteMarkerGRPCToWriteMarker(resp.WriteMarker),
Success: resp.Success,
ErrorMessage: resp.ErrorMessage,
func GetObjectPathResponseCreator(r interface{}) *blobbergrpc.GetObjectPathResponse {
if r == nil {
return nil
}

httpResp, _ := r.(*blobberHTTP.ObjectPathResult)
var resp blobbergrpc.GetObjectPathResponse

var pathList []*blobbergrpc.FileRef
pl, _ := httpResp.Path["list"].([]map[string]interface{})
for _, v := range pl {
pathList = append(pathList, FileRefToFileRefGRPC(reference.ListingDataToRef(v)))
}
}

func GetCalculateHashResponseHandler(response *blobbergrpc.CalculateHashResponse) interface{} {
result := make(map[string]interface{})
if msg := response.GetMessage(); msg != "" {
result["msg"] = msg
resp.LatestWriteMarker = WriteMarkerToWriteMarkerGRPC(httpResp.LatestWM)
resp.ObjectPath = &blobbergrpc.ObjectPath{
RootHash: httpResp.RootHash,
Meta: FileRefToFileRefGRPC(reference.ListingDataToRef(httpResp.Meta)),
Path: FileRefToFileRefGRPC(reference.ListingDataToRef(httpResp.Path)),
PathList: pathList,
FileBlockNum: httpResp.FileBlockNum,
}

return result
return &resp
}

func GetCommitMetaTxnHandlerResponse(response *blobbergrpc.CommitMetaTxnResponse) interface{} {
msg := response.GetMessage()
if msg == "" {
func CommitWriteResponseHandler(r interface{}) *blobbergrpc.CommitResponse {
if r == nil {
return nil
}

result := struct {
Msg string `json:"msg"`
}{
Msg: msg,
httpResp, _ := r.(*blobberHTTP.CommitResult)

return &blobbergrpc.CommitResponse{
AllocationRoot: httpResp.AllocationRoot,
WriteMarker: WriteMarkerToWriteMarkerGRPC(httpResp.WriteMarker),
ErrorMessage: httpResp.ErrorMessage,
Success: httpResp.Success,
}
}

func GetCalculateHashResponseHandler(r interface{}) *blobbergrpc.CalculateHashResponse {
httpResp, _ := r.(map[string]interface{})
msg, _ := httpResp["msg"].(string)

return &blobbergrpc.CalculateHashResponse{Message: msg}
}

func GetCommitMetaTxnHandlerResponse(r interface{}) *blobbergrpc.CommitMetaTxnResponse {
msg, _ := r.(struct {
Msg string `json:"msg"`
})

return result
return &blobbergrpc.CommitMetaTxnResponse{Message: msg.Msg}
}

func CollaboratorResponse(response *blobbergrpc.CollaboratorResponse) interface{} {
if msg := response.GetMessage(); msg != "" {
return struct {
Msg string `json:"msg"`
}{Msg: msg}
func CollaboratorResponse(r interface{}) *blobbergrpc.CollaboratorResponse {
if r == nil {
return nil
}

if collaborators := response.GetCollaborators(); collaborators != nil {
collabs := make([]reference.Collaborator, 0, len(collaborators))
for _, c := range collaborators {
collabs = append(collabs, *GRPCCollaboratorToCollaborator(c))
}
msg, _ := r.(struct {
Msg string `json:"msg"`
})
var resp blobbergrpc.CollaboratorResponse
if msg.Msg != "" {
resp.Message = msg.Msg
return &resp
}

return collabs
collabs, _ := r.([]reference.Collaborator)
for _, c := range collabs {
resp.Collaborators = append(resp.Collaborators, CollaboratorToGRPCCollaborator(&c))
}

return nil
return &resp
}

func UpdateObjectAttributesResponseHandler(updateAttributesResponse *blobbergrpc.UpdateObjectAttributesResponse) *blobberHTTP.UpdateObjectAttributesResponse {
return &blobberHTTP.UpdateObjectAttributesResponse{
WhoPaysForReads: common.WhoPays(updateAttributesResponse.WhoPaysForReads),
func UpdateObjectAttributesResponseCreator(r interface{}) *blobbergrpc.UpdateObjectAttributesResponse {
if r != nil {
return nil
}

httpResp, _ := r.(*reference.Attributes)
return &blobbergrpc.UpdateObjectAttributesResponse{WhoPaysForReads: int64(httpResp.WhoPaysForReads)}
}

func CopyObjectResponseHandler(copyObjectResponse *blobbergrpc.CopyObjectResponse) *blobberHTTP.UploadResult {
return &blobberHTTP.UploadResult{
Filename: copyObjectResponse.Filename,
Size: copyObjectResponse.Size,
Hash: copyObjectResponse.ContentHash,
MerkleRoot: copyObjectResponse.MerkleRoot,
UploadLength: copyObjectResponse.UploadLength,
UploadOffset: copyObjectResponse.UploadOffset,
func CopyObjectResponseCreator(r interface{}) *blobbergrpc.CopyObjectResponse {
if r == nil {
return nil
}

httpResp, _ := r.(*blobberHTTP.UploadResult)
return &blobbergrpc.CopyObjectResponse{
Filename: httpResp.Filename,
Size: httpResp.Size,
ContentHash: httpResp.Hash,
MerkleRoot: httpResp.MerkleRoot,
UploadLength: httpResp.UploadLength,
UploadOffset: httpResp.UploadOffset,
}
}
Loading