Git Product home page Git Product logo

jsonrpc's Introduction

JSON-RPC 2.0 server for Laravel

Latest Version on Packagist Total Downloads

Simple JSON-RPC 2.0 server for Laravel, without batch request.

Installation

You can install the package via composer:

composer require bpartner/jsonrpc

php artisan vendor:publish --tag=config

Configure

in config/jsonrpc.php set namespace and Bearer token (default 1234567890)

If Bearer token not work, add to your .htaccess file this rule

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

Basic Usage

php artisan make:rpc MyHandler 

After creating handler, in handle method put your business logic and return array with response data.

public function handle(): array
{
    $myData = MyBusinessLogicClass::make($this->params);

    return ['data' => $myData];
}

Send POST request to /jsonrpc/v1/endpoint in Json RPC 2.0 standard with Bearer Token.

{
    "jsonrpc": "2.0",
    "method": "myHandler",
    "params": {
        "param1": "param1",
        "param2": {
            "param3": 123,
        }
    },
    "id": "abracadabra"
}

for validate input parameters use rule method in handler.

protected function rule(): array
{
    return [
        'param1' => 'require|string',
        'param2.param3' => 'require|numeric'
    ];
}

Advanced Usage

You can use any route, middleware and guards for your rpc endpoint.

  1. Disable default route in config/jsonrpc.php
  2. Create your custom route
  3. Create your middleware
  4. Create your controller and use RpcService

Middleware example

class AuthToken
{
    public function handle(Request $request, Closure $next)
    {
        if ($request->header('x-auth-token') !== config('jsonrpc.token')) {
            return  response()->json([
                'status' => 'error',
                'code' => 401,
                'message' => 'Unauthorized',
            ]);
        }
        return $next($request);
    }
}

Controller example

namespace App\Http\Controllers


use Bpartner\Jsonrpc\RpcFormRequest;
use Bpartner\Jsonrpc\RpcRequest;
use Bpartner\Jsonrpc\RpcServiceInterface; 
use Illuminate\Routing\Controller;

class MyContoller extends Controller
{
    public function __invoke(RpcFormRequest $request, RpcServiceInterface $rpcService)
    {
        return $rpcService->run();
    }
}

Middleware

Create own middleware for any RpcHandler

 protected string|array $middlewares = 'guest';
//or
 protected string|array $middlewares = [
    MyMiddleware::class,
    SecondMiddleware::class
];

Important

This package not support Batch request. For example, this request not supported:

[
    {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
    {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
    {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
    {"foo": "boo"},
    {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
    {"jsonrpc": "2.0", "method": "get_data", "id": "9"} 
]

Credits

License

The MIT License (MIT). Please see License File for more information.

jsonrpc's People

Contributors

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