Code Monkey home page Code Monkey logo

api-resp's Introduction

该模块为远程/异步调用API的返回结果,定义通用的封装结构和基本方法。

返回的数据是JSON格式的,结构示例:

{
  "success": true,
  "code": 0,
  "message": "",
  "data": []
}

四个属性简要说明:

  • success 表示调用是否成功。
  • code 成功为0,失败为非0的整数值。
  • message 在失败时提供简要的说明信息。
  • data 返回的业务数据,也是JSON格式。

用法示例:

#[cfg(test)]
mod tests {
    use serde_json::json;
    use super::*;

    #[derive(Debug, Serialize, Deserialize)]
    struct PingPang {
        color: String,
        weight: f64,
    }

    #[test]
    fn test_resp() {
        // 成功结果,没有业务数据。
        let suc_json = ApiResp::suc().to_json();
        println!("suc_json: {}", suc_json);
        let orig_suc: ApiResp = serde_json::from_str(suc_json.as_str()).unwrap();
        assert!(orig_suc.is_success());

        // 成功结果,带有业务数据。
        let vals = vec![
            PingPang {color: "white".to_string(), weight: 10.0},
            PingPang {color: "yellow".to_string(), weight: 11.5},
        ];
        let suc_data = ApiResp::success(json!(vals)).to_json();
        println!("suc_data: {}", suc_data);
        let orig_suc_data: ApiResp = serde_json::from_str(suc_data.as_str()).unwrap();
        assert!(orig_suc_data.is_success());

        // 失败结果。
        let fail_json = ApiResp::error(-1, String::from("交易出错了")).to_json();
        println!("fail_json: {}", fail_json);
        let orig_fail: ApiResp = serde_json::from_str(fail_json.as_str()).unwrap();
        assert!(!orig_fail.is_success());
    }
}

执行上面的测试方法将打输出类似下面的信息:

suc_json:

{"success":true,"code":0,"message":"","data":null}

suc_data:

{"success":true,"code":0,"message":"","data":[{"color":"white","weight":10.0},{"color":"yellow","weight":11.5}]}

fail_json:

{"success":false,"code":-1,"message":"交易出错了","data":null}

api-resp's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.