Code Monkey home page Code Monkey logo

json_rpc's Introduction

json rpc

JSON-RPC 2.0 Implementation in Rust

Crate Travis
Build Status

#Overview

Currently in development. Look at the examples in ./examples for more information.

##Todo Items

[0.2.0]

  • Server side

[0.3.0]

  • Client side
  • Refactor server code

[0.4.0]

  • Allow batch requests
  • Parallel or series execution of batch requests

[1.0.0]

  • Full documentation and examples

[2.0.0]

  • json-rpc 1.0

#License

Dual-licensed to be compatible with the Rust project.

Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 or the MIT license http://opensource.org/licenses/MIT, at your option. This file may not be copied, modified, or distributed except according to those terms.

Examples

This is a basic example with two methods:

#[macro_use(rpc_method)]
extern crate json_rpc;
use json_rpc::{Server, Json, Error};

fn main() {
    let mut rpc_server = Server::new(); 

    // Registers a Rpc Method named "Subtract" with two parameter "by Name".
    rpc_method!(rpc_server, Subtract, oper1<u64>;oper2<u64>, {                
        Ok(Json::U64(oper1 - oper2))        
    });
            
    // Registers a Rpc Method named "Multiply" with N parameteres "by Position".
    rpc_method!(rpc_server, Multiply, values[u64], {        
        let mut r = 1;
        for v in values { r *= v }
        Ok(Json::U64(r))
    });    

    let str_request = "{\"jsonrpc\":\"2.0\",\"method\":\"Subtract\", \"params\":{\"oper1\":23, \"oper2\":4}, \"id\":2}".to_string();
    match rpc_server.request(str_request) {    
        Some(str_response) => assert_eq!(str_response, "{\"id\":2,\"jsonrpc\":\"2.0\",\"result\":19}") ,
        None => unreachable!(),
    };

    let str_request = "{\"jsonrpc\":\"2.0\",\"method\":\"Multiply\", \"params\":[5, 6, 7], \"id\":3}".to_string();
    match rpc_server.request(str_request) {    
        Some(str_response) => assert_eq!(str_response, "{\"id\":3,\"jsonrpc\":\"2.0\",\"result\":210}") ,
        None => unreachable!(),
    };
}

json_rpc's People

Contributors

bcndanos avatar

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.