fabric-SDK调用参考

区块链Fabric SDK调用参考

  • java语言sdk
    • 框架及秘钥支持
    • 1. **调用前准备**
    • 2. **准备调用**

java语言sdk

框架及秘钥支持

框架 秘钥
fabric SM2,secp256r1

-fabric框架应用使用secp256r1、SM2 秘钥的秘钥托管和秘钥上传两种模式

1. 调用前准备

应用参数

应用参数是用户在创建应用成功之后在应用详情页面获取,或者由本地设置的一些参数,具体包含以下参数

  • 网关接口地址: 网关接口的调用地址
	//网关接口地址ip
    private static String BASE_URL = "http://ip:port";
  • 用户名称: 账本的用户名称
	headers.put("username", "admin")
  • 用户密码: 用户的密码
	headers.put("password", "password");
  • 应用编号: 参与的应用的编号
	headers.put("app_id", "scm_qa");
  • 账本编号: 参与的账本的编号
	headers.put("accountbook_id", "zj_scm_qa");
  • 合约编号: 参与的合约的编号
	headers.put("contract_id", "survey");

2. 准备调用

 	//网关接口地址
    private static String BASE_URL = "http://localhost:8023";
    //URI为固定值,不用修改
    private static String TRANSACTION_URI = "/transaction";
    private static String QUERY_URI = "/query";

    public static void main(String[] args) {
     
        //创建交易
        insert();
        //修改
        updte();
        //取消
        cancel();
        //删除
        delete();
        //查询指定交易
        query();
        //查询指定交易变更历史
        getHistory();
        //查询交易列表
        list();
    }

    /**
     * 获取请求header
     *
     * @return
     */
    private static Map<String, String> getHeader() {
     
        //设置请求header
        Map<String, String> headers = new HashMap<>();
        //应用编号
        headers.put("app_id", "scm_qa");
        //账本编号
        headers.put("accountbook_id", "zj_scm_qa");
        //合约编号
        headers.put("contract_id", "survey");
        //用户名称
        headers.put("username", "admin");
        //用户密码
        headers.put("password", "password");
        return headers;
    }

    /**
     * 创建
     */
    private static void insert() {
     
        // 设置请求param
        InsertSurveyRequest surveyRequest = new InsertSurveyRequest();
        surveyRequest.setKey("123456");
        surveyRequest.setTxType(SurveyTxTypeEnum.INSERT_SURVEY.getTxType());
        Map<String, String> value = new HashMap<>();
        value.put("info", "info");
        surveyRequest.setValue(value);
        String params = JsonUtils.toJson(surveyRequest);
        String resp = HttpUtils.sendURLPost(BASE_URL + TRANSACTION_URI, params, getHeader());
        doSomething(resp);
    }

    /**
     * 更新
     */
    private static void updte() {
     
        // 设置请求param
        UpdateSurveyRequest surveyRequest = new UpdateSurveyRequest();
        surveyRequest.setKey("123456");
        surveyRequest.setTxType(SurveyTxTypeEnum.UPDATE_SURVEY.getTxType());
        Map<String, String> value = new HashMap<>();
        value.put("info", "info");
        surveyRequest.setValue(value);
        String params = JsonUtils.toJson(surveyRequest);
        String resp = HttpUtils.sendURLPost(BASE_URL + TRANSACTION_URI, params, getHeader());
        doSomething(resp);
    }

    /**
     * 删除
     */
    private static void delete() {
     
        // 设置请求param
        DeleteSurveyRequest surveyRequest = new DeleteSurveyRequest();
        surveyRequest.setKey("123456");
        surveyRequest.setTxType(SurveyTxTypeEnum.DELETE_SURVEY.getTxType());
        String params = JsonUtils.toJson(surveyRequest);
        String resp = HttpUtils.sendURLPost(BASE_URL + TRANSACTION_URI, params, getHeader());
        doSomething(resp);
    }

    /**
     * 取消
     */
    private static void cancel() {
     
        // 设置请求param
        CancelSurveyRequest surveyRequest = new CancelSurveyRequest();
        surveyRequest.setKey("123456");
        surveyRequest.setTxType(SurveyTxTypeEnum.CANCEL_SURVEY.getTxType());
        String params = JsonUtils.toJson(surveyRequest);
        String resp = HttpUtils.sendURLPost(BASE_URL + TRANSACTION_URI, params, getHeader());
        doSomething(resp);
    }

    /**
     * 查询指定信息
     */
    private static void query() {
     
        // 设置请求param
        BaseSurveyRequest cancelBatchRequest = new BaseSurveyRequest();
        cancelBatchRequest.setKey("123456");
        cancelBatchRequest.setTxType(SurveyTxTypeEnum.QUERY_SURVEY.getTxType());
        String params = JsonUtils.toJson(cancelBatchRequest);
        String resp = HttpUtils.sendURLPost(BASE_URL + QUERY_URI, params, getHeader());
        doSomething(resp);
    }

    /**
     * 查询变更历史记录
     */
    private static void getHistory() {
     
        // 设置请求param
        GetSurveyHistoryRequest surveyRequest = new GetSurveyHistoryRequest();
        surveyRequest.setKey("123456");
        surveyRequest.setChangeType("insert");//查询所有时不传该参数
        surveyRequest.setTxType(SurveyTxTypeEnum.GET_HISTORY.getTxType());
        String params = JsonUtils.toJson(surveyRequest);
        String resp = HttpUtils.sendURLPost(BASE_URL + QUERY_URI, params, getHeader());
        doSomething(resp);
    }

    /**
     * 查询列表
     */
    private static void list() {
     
        // 设置请求param
        ListSurveyRequest surveyRequest = new ListSurveyRequest();
        surveyRequest.setStartTime("20200720010101");
        surveyRequest.setEndTime("20200820010101");
        surveyRequest.setTxType(SurveyTxTypeEnum.LIST_SURVEY.getTxType());
        String params = JsonUtils.toJson(surveyRequest);
        String resp = HttpUtils.sendURLPost(BASE_URL + QUERY_URI, params, getHeader());
        doSomething(resp);
    }

    /**
     * 处理业务逻辑
     *
     * @param resp
     */
    private static void doSomething(String resp) {
     
        if (resp != null) {
     
            //此处为查询接口示例,其它接口resp解析参考此处
            DataResponse<QuerySurveyResponse> response = JsonUtils.json2Object(resp, new TypeReference<DataResponse<QuerySurveyResponse>>() {
     
            });
            if (response.getResult() == 1 && CommonResponse.SUCCEED.equals(response.getCode())) {
     
                //success
            } else {
     
                //fail
            }
        }
    }

SDK源码链接:https://gitee.com/luowenxing/fabric-sdk.git

你可能感兴趣的:(区块链-fabric,区块链,java)