Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sas1024 committed Apr 6, 2021
0 parents commit d0f64ec
Show file tree
Hide file tree
Showing 6 changed files with 5,045 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Alexander Korelskiy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# PHP JSON-RPC 2.0 Client example for zenrpc

PHP JSON-RPC 2.0 Client usage example for [vmkteam/zenrpc](https://github.com/vmkteam/zenrpc).

---
RPC client:
- generated by [rpcgen](https://github.com/vmkteam/rpcgen) using public [MyShows](https://myshows.me) API SMD scheme
- included in composer autoloading with `ClassMap loader`:
```json
{
"classmap": [
"src/MyShowsRpc/RpcClient.php"
]
}
```


Just check `index.php` for example.
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "sas1024/php-rpc-client-example",
"description": "Example usage of eazy-jsonrpc with generated rpc client by rpcgen",
"authors": [
{
"name": "Alexander Korelskiy",
"email": "[email protected]"
}
],
"autoload":{
"classmap":[
"src/MyShowsRpc/RpcClient.php"
],
"psr-4": {
"App\\": "src/App"
}
},
"minimum-stability": "dev",
"require": {
"php": ">=7.2",
"sergeyfast/eazy-jsonrpc": "v3.0.1"
}
}
23 changes: 23 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use MyShowsRpc\ApiNews;
use MyShowsRpc\RpcClient;
use App\News\NewsManager;

include_once 'vendor/autoload.php';


$rpcClient = RpcClient::GetInstance( 'https://api.myshows.me/v3/rpc/' );
$newsManager = new NewsManager( $rpcClient );

$title = $newsManager->GetNewsTitleById( 1 );
echo $title ."\n"; // will be "Russian Doll. Жизни матрешки"

$title = $newsManager->GetNewsTitleById( -1 );
echo $title ."\n"; // will be "News with newsId=-1 is not found", because we catch rpc error exception in our manager.

/** @var ApiNews[] $news */
$news = $newsManager->GetLastThreeNews();
foreach ( $news as $item ) {
echo $item->title ."\n"; // will output last 3 news titles.
}
51 changes: 51 additions & 0 deletions src/App/News/NewsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\News;


use EazyJsonRpc\BaseJsonRpcException;
use GuzzleHttp\Exception\GuzzleException;
use MyShowsRpc\RpcClient;

class NewsManager {

/**
* RpcClient
*/
private $rpcClient;


public function __construct( RpcClient $rpcClient ) {
$this->rpcClient = $rpcClient;
}


/**
* @param int $id
* @return string
* @throws GuzzleException
* @throws \JsonMapper_Exception
*/
public function GetNewsTitleById( int $id ): string {
try {
$news = $this->rpcClient->news_GetById( $id );
$result = $news->title;
} catch ( BaseJsonRpcException $e ) {
$result = $e->getMessage();
}

return $result;
}


/**
* @return array
* @throws BaseJsonRpcException
* @throws GuzzleException
* @throws \JsonMapper_Exception
*/
public function GetLastThreeNews(): array {
return $this->rpcClient->news_Get(null, null, 3);
}

}
Loading

0 comments on commit d0f64ec

Please sign in to comment.