示例#1
0
func v2Request(req *primitives.JSON2Request) (*primitives.JSON2Response, error) {
	j, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

	resp, err := http.Post(
		"http://localhost:8088/v2",
		"application/json",
		bytes.NewBuffer(j))
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	r := primitives.NewJSON2Response()
	if err := json.Unmarshal(body, r); err != nil {
		return nil, err
	}

	return r, nil
}
示例#2
0
func HandleV2Error(ctx *web.Context, j *primitives.JSON2Request, err *primitives.JSONError) {
	resp := primitives.NewJSON2Response()
	if j != nil {
		resp.ID = j.ID
	} else {
		resp.ID = nil
	}
	resp.Error = err

	ctx.WriteHeader(httpBad)
	ctx.Write([]byte(resp.String()))
}
示例#3
0
func HandleV2Request(state interfaces.IState, j *primitives.JSON2Request) (*primitives.JSON2Response, *primitives.JSONError) {
	var resp interface{}
	var jsonError *primitives.JSONError
	params := j.Params
	fmt.Println(j.Method)
	switch j.Method {
	case "chain-head":
		resp, jsonError = HandleV2ChainHead(state, params)
		break
	case "commit-chain":
		resp, jsonError = HandleV2CommitChain(state, params)
		break
	case "commit-entry":
		resp, jsonError = HandleV2CommitEntry(state, params)
		break
	case "directory-block":
		resp, jsonError = HandleV2DirectoryBlock(state, params)
		break
	case "directory-block-head":
		resp, jsonError = HandleV2DirectoryBlockHead(state, params)
		break
	case "entry-block":
		resp, jsonError = HandleV2EntryBlock(state, params)
		break
	case "entry":
		resp, jsonError = HandleV2Entry(state, params)
		break
	case "entry-credit-balance":
		resp, jsonError = HandleV2EntryCreditBalance(state, params)
		break
	case "entry-credit-rate":
		resp, jsonError = HandleV2EntryCreditRate(state, params)
		break
	case "factoid-balance":
		resp, jsonError = HandleV2FactoidBalance(state, params)
		break
	case "factoid-submit":
		resp, jsonError = HandleV2FactoidSubmit(state, params)
		break
	case "heights":
		resp, jsonError = HandleV2Heights(state, params)
		break
	case "raw-data":
		resp, jsonError = HandleV2RawData(state, params)
		break
	case "receipt":
		resp, jsonError = HandleV2Receipt(state, params)
		break
	case "properties":
		resp, jsonError = HandleV2Properties(state, params)
		break
	case "reveal-chain":
		resp, jsonError = HandleV2RevealChain(state, params)
		break
	case "reveal-entry":
		resp, jsonError = HandleV2RevealEntry(state, params)
		break
	case "factoid-ack":
		resp, jsonError = HandleV2FactoidACK(state, params)
		break
	case "entry-ack":
		resp, jsonError = HandleV2EntryACK(state, params)
		break
	case "send-raw-message":
		resp, jsonError = HandleV2SendRawMessage(state, params)
		break
	case "get-transaction":
		resp, jsonError = HandleV2GetTranasction(state, params)
		break
	case "get-pending-entries":
		resp, jsonError = HandleV2GetPendingEntries(state, params)
		break
	case "get-pending-transactions":
		resp, jsonError = HandleV2GetPendingTransactions(state, params)
		break
	default:
		jsonError = NewMethodNotFoundError()
		break
	}
	if jsonError != nil {
		return nil, jsonError
	}

	jsonResp := primitives.NewJSON2Response()
	jsonResp.ID = j.ID
	jsonResp.Result = resp

	return jsonResp, nil
}