Code Monkey home page Code Monkey logo

guzzle-jsonrpc's Introduction

[ABANDONED] Guzzle JSON-RPC

No Maintenance Intended

This is no longer supported, please consider forking this repository to make any desired changes.

Master branch build status Coverage Status Quality Score Published version PHP ~5.5 MIT Licensed

This library implements JSON-RPC 2.0 for the Guzzle HTTP client. We try to support all commonly used versions of Guzzle including:

It can be installed in whichever way you prefer, but we recommend Composer.

{
    "require": {
        "graze/guzzle-jsonrpc": "~3.0"
    }
}

Documentation

<?php
use Graze\GuzzleHttp\JsonRpc\Client;

// Create the client
$client = Client::factory('http://localhost:8000');

// Send a notification
$client->send($client->notification('method', ['key'=>'value']));

// Send a request that expects a response
$client->send($client->request(123, 'method', ['key'=>'value']));

// Send a batch of requests
$client->sendAll([
    $client->request(123, 'method', ['key'=>'value']),
    $client->request(456, 'method', ['key'=>'value']),
    $client->notification('method', ['key'=>'value'])
]);

Async requests

Asynchronous requests are supported by making use of the Guzzle Promises library; an implementation of Promises/A+.

<?php
use Graze\GuzzleHttp\JsonRpc\Client;

// Create the client
$client = Client::factory('http://localhost:8000');

// Send an async notification
$promise = $client->sendAsync($client->notification('method', ['key'=>'value']));
$promise->then(function () {
    // Do something
});

// Send an async request that expects a response
$promise = $client->sendAsync($client->request(123, 'method', ['key'=>'value']));
$promise->then(function ($response) {
    // Do something with the response
});

// Send a batch of requests
$client->sendAllAsync([
    $client->request(123, 'method', ['key'=>'value']),
    $client->request(456, 'method', ['key'=>'value']),
    $client->notification('method', ['key'=>'value'])
])->then(function ($responses) {
    // Do something with the list of responses
});

Throw exception on RPC error

You can throw an exception if you receive an RPC error response by adding the option [rpc_error => true] in the client constructor.

<?php
use Graze\GuzzleHttp\JsonRpc\Client;
use Graze\GuzzleHttp\JsonRpc\Exception\RequestException;

// Create the client with the `rpc_error`
$client = Client::factory('http://localhost:8000', ['rpc_error'=>true]);

// Create a request
$request = $client->request(123, 'method', ['key'=>'value']);

// Send the request
try {
    $client->send($request);
} catch (RequestException $e) {
    die($e->getResponse()->getRpcErrorMessage());
}

Contributing

We accept contributions to the source via Pull Request, but passing unit tests must be included before it will be considered for merge.

~ $ make deps
~ $ make lint test

License

The content of this library is released under the MIT License by Nature Delivered Ltd.

You can find a copy of this license at mit or in LICENSE

guzzle-jsonrpc's People

Contributors

adlawson avatar biggianteye avatar dovys avatar flip111 avatar nadsella avatar psfpro avatar sjparkinson avatar zinovyev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

guzzle-jsonrpc's Issues

Upgrade to Guzzle 6?

I use this package on Drupal 8, but composer can no longer resolve because Drupal needs Guzzle 6.

EventDispatcher problem

Hi, first of all, thank you for your implementation of JSON-RPC for Guzzle.

I had some problems with it, specially with EventDispatcher. I think when your create your Request type, you must set to it EventDispatcher from client:

// path/to/vendor/Graze/Guzzle/JsonRpc/Message/Request.php

public function __construct(RequestInterface $request, $method, $id = null)
    {
        parent::__construct($request->getMethod(), $request->getUrl(), $request->getHeaders());

        $this->setClient($request->getClient());
        // this fixes the problem
        $this->setEventDispatcher($request->getClient()->getEventDispatcher());
        $this->rpcFields = new Collection(array(
            'jsonrpc' => JsonRpcClientInterface::VERSION,
            'method'  => (string) $method
        ));

        if (null !== $id) {
            $this->setRpcField('id', $id);
        }
    }

I had problems when used MisdGuzzleBundle. This bundle allows to log requests, but it does not work with your client, because it has not correct EventDispatcher.

Undeclared InvalidArgumentException

src/Message/MessageFactory.php contains 2 methods: fromResponse() and createRequest() that can throws undeclared InvalidArgumentException. Fix it, please.

Error code accepted range

The current implementation accepts only the following error codes:

array_merge(array(-32700), range(-32600, -32603), range(-32000, -32099))

This is the source code from "JsonRpc/Message/ErrorResponse.php".

However, the JSON-RPC specification allows any integer. See (http://www.jsonrpc.org/specification) which says: "5.1 Error object -> code -> A Number that indicates the error type that occurred. This MUST be an integer."

That's all. After that a few reserved codes for pre-defined errors are listed. It seems that you've misinterpreted those reserved codes as the only one which are accepted. While actually, those reserved codes should be used as specified and "the remainder of the space is available for application defined errors."

Please allow us to use arbitrary integer error codes in our applications.

Debug PHP5.6 functional test server issues

The server fails to run in PHP 5.6.0 on Travis-CI but work fine on 5.6.0-1 in development.

# Travis CI
$ php --version
PHP 5.6.0 (cli) (built: Sep  3 2014 23:16:07) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

# Development
php --version
PHP 5.6.0-1+deb.sury.org~precise+1 (cli) (built: Sep 10 2014 14:24:05)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

Missing ability to send array of requests

In JSON-RPC specification you can send an array or request and receive an array of responses.
In some scenarios, it may slightly increase performance in the library will send N asynced HTTP requests with M JSON-RPC requests in each.

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.