Giter Club home page Giter Club logo

json-rpc's Introduction

json-rpc

This library is a highly flexible implementation of the JSON-RPC 1.0 specification written for PHP 5.3+, adhering to the PSR standards [where applicable].

It has been designed to allow for many of the mechanisms to be changed without editing core files. It is possible to run different transports, authentication schemes, method authorisation systems, argument type validation & data sanitizing schemes, proxy system, and method list caching/parsing systems without modifying the library.

Admittedly, it's currently missing the necessary unit tests that will allow you to check the behaviour of some of your components.

If you have written any modules that could be included, or would like to discuss the library- please do!

How to Use Client - Basic

<?php
namespace MyCompany\Package;

use \Ndm\JsonRpc\Client\HttpClient;
// use vendor autoload from composer
require('vendor/autoload.php');

// create a Client using the HttpTransport layer
$client = HttpClient::connect('http://api.somesite.com/');
// call a method, using named parameters
$client->call('somemethod', array('abc'=>123));

// alternatively, use the "native" interface
$nativeClient = $client->getNativeClient();
// however calls must use positional parameters
$nativeClient->somemethod(123);

How To Use Server - Basic

<?php


namespace MyCompany\Package;

use Ndm\JsonRpc\HttpServer;
use \Ndm\JsonRpc\Server\Exception\TransportReceiveException;
use \Ndm\JsonRpc\Server\Exception\TransportReplyException;

require ('vendor/autoload.php'); // require autoloader created by composer

$api = new SomeClass();
$methods =  array (
    'static_func'=> 'AnotherStatic::Func',
    'global_func' => 'do_abc',
    'some_func' => function($p) { return $p + 1; }
);
// register the server with a set of methods, either from an instance, a class with static methods, or as a map of 'callables'
$server = HttpServer::register( $api, 'StaticClass',$methods);

// process the request!
try {
    $server->process();
} catch (TransportReceiveException $treceive){
    // exceptions on this layer - like not using HTTP-POST
    header('HTTP/1.0 400 Bad Request');
    exit;
} catch (TransportReplyException $treply){
    header('HTTP/1.0 500 Internal Server Error');
    exit;
}

How To Use Server - Advanced

<?php


namespace MyCompany\Package;

use \Ndm\JsonRpc\Server\Server;
use \Ndm\JsonRpc\Server\Exception\TransportReceiveException;
use \Ndm\JsonRpc\Server\Exception\TransportReplyException;
use \Ndm\JsonRpc\Server\Transport\HttpTransport;
use \Ndm\JsonRpc\Server\Dispatch as Dispatch;

require ('vendor/autoload.php'); // require autoloader created by composer

// init procedure -
// 1. perform any external checks / tests on your transport layer (ie Authentication via OAuth)
// 2. initialise a transport system to obtain the rpc calls from, and return results to
// 3. get some functions to provide
// 4. register them with a dispatch system
// 5. create a server with the aforementioned dispatch & transport systems

// the transport - a simple http wrapper
$transport = new HttpTransport(HttpTransport::OPT_REQUIRE_HTTPS | HttpTransport::OPT_SEND_OUTPUT_HEADERS);


$api = new SomeClass();

//create a set of methods from the instance of SomeClass
$methods = Dispatch\ReflectionMethod::createFrom($api);
// dispatch system is responsible for invoking methods called by clients
$dispatch = new Dispatch\MapDispatch();
// register all the methods with the dispatch system
$dispatch->registerAll($methods);

// start the server
$server = new Server($transport, $dispatch);
// process the request!
try {
    $server->process();
} catch (TransportReceiveException $treceive){
    header('HTTP/1.0 400 Bad Request');
    exit;
} catch (TransportReplyException $treply){
     header('HTTP/1.0 500 Internal Server Error');
     exit;
}

Todo

Documentation:

  • Client Documentation

Testing:

  • Unit Test

    • Core\ResponseParser
    • Server\Server [mock the 'receive' function, or wrap the combination of TransportInterface & RequestParser]
    • Client\Client [mock the 'send' function, or wrap the combination of TransportInterface & ResponseParser]
    • Client\BatchClient
    • Server\Transport\HttpTransport
    • Un-implemented Classes / Transports

Implementation / Functionality:

  • OAuth & Basic Auth Client Wrapper using HttpTransport
  • More comprehensive Dispatch system implementations (Caching, Docblock Parsing, Type Checking)

Server Structure / Work-flow

Transport Lifecycle:

  1. Reads the transport layer / source - provides string/text only
  2. Receives a text reply to render

Server Lifecycle:

  1. Obtains Requests text from Transport via 'receive'.
  2. Uses \JsonRpc\RequestParser->parse() to parse in to objects
  3. Iterates through each requests, obtaining the result through Dispatch->invoke. Exceptions are caught and turned into ResponseError.
  4. Passes the result, back to the Transport to be rendered.

Dispatch Lifecycle:

  1. Is passed a method-alias and arguments, and must return a result or throw an exception.

json-rpc's People

Contributors

nathan-muir avatar

Watchers

 avatar  avatar  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.