Skip to content

Commit 040244e

Browse files
committed
Prepare export_cli_python
1 parent 1b67e73 commit 040244e

File tree

11 files changed

+558
-177
lines changed

11 files changed

+558
-177
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
.wsjcpp/*
2+
.logs/
23
tmp/*
4+
example-of-exported/py3/dist/*
5+
example-of-exported/py3/*.egg-info
6+
example-of-exported/py3/build
37
wsjcpp-jsonrpc2
48
wsjcpp-jsonrpc20

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ list (APPEND WSJCPP_SOURCES "src/main.cpp")
1414
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20.h")
1515
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20.cpp")
1616

17+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_python.h")
18+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_jsonrpc20_export_cli_python.cpp")
19+
1720
# examples
1821
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_json_rpc20_handler_game_create.h")
1922
list (APPEND WSJCPP_SOURCES "./src/examples/wsjcpp_json_rpc20_handler_game_create.cpp")

README.md

Lines changed: 102 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
[![Build Status](https://api.travis-ci.com/wsjcpp/wsjcpp-jsonrpc20.svg?branch=master)](https://travis-ci.com/wsjcpp/wsjcpp-jsonrpc20)
44

5-
C++ Implementation for JsonRPC 2.0
5+
C++ Implementation for JsonRPC 2.0 (oriented on websockets)
66

77
## Features
88

99
* Collect all handlers for jsonrpc20
1010
* Including system of define validators
11-
*
12-
1311

1412
## Integration
1513

@@ -32,7 +30,7 @@ $ wsjcpp install https://github.com/wsjcpp/wsjcpp-jsonrpc20:master
3230

3331
## Prepare handler
3432

35-
easy way:
33+
### via wsjcpp
3634

3735
```
3836
$ wsjcpp generate WsjcppJsonRpc20Handler GameCreate
@@ -56,43 +54,117 @@ list (APPEND WSJCPP_SOURCES "./src/wsjcpp_json_rpc20_handler_game_create.cpp")
5654
They will contains class `WsjcppJsonRpc20HandlerGameCreate` with method `game_create`
5755

5856

59-
## Example
57+
### code sample
58+
59+
File `./src/wsjcpp_json_rpc20_handler_game_create.h`:
6060

6161
``` cpp
62+
#ifndef WSJCPP_JSON_RPC20_HANDLER_GAME_CREATE_H
63+
#define WSJCPP_JSON_RPC20_HANDLER_GAME_CREATE_H
6264

63-
// method: order_remove or remove_order
65+
#include <wsjcpp_jsonrpc20.h>
6466

65-
MethodJsonRpc20_OrderRemove::MethodJsonRpc20_OrderRemove()
66-
: WsjcppJsonRpc20Base({"order_remove", "remove_order"}, "Remove order by id") {
67-
68-
// restriction for different roles
69-
setAccessUnauthorized(false); // this define "false" says that UserSession will be not nullptr
70-
setAccessUser(true);
71-
setAccessAdmin(true);
67+
class WsjcppJsonRpc20HandlerGameCreate : public WsjcppJsonRpc20HandlerBase {
68+
public:
69+
WsjcppJsonRpc20HandlerGameCreate();
70+
virtual void handle(WsjcppJsonRpc20Request *pRequest) override;
71+
};
72+
73+
#endif // WSJCPP_JSON_RPC20_HANDLER_GAME_CREATE_H
74+
```
7275

73-
// just information for documentation
74-
setActivatedFromVersion("0.2.32");
76+
File `./src/wsjcpp_json_rpc20_handler_game_create.cpp`:
7577

76-
// validation and description input fields
77-
requireIntegerParam("id", "Id of user");
78+
``` cpp
79+
#include "wsjcpp_json_rpc20_handler_game_create.h"
80+
#include <wsjcpp_core.h>
81+
#include <wsjcpp_jsonrpc20.h>
82+
83+
// ---------------------------------------------------------------------
84+
// WsjcppJsonRpc20HandlerGameCreate
85+
86+
WsjcppJsonRpc20HandlerGameCreate::WsjcppJsonRpc20HandlerGameCreate()
87+
: WsjcppJsonRpc20HandlerBase("game_create", "TODO description") {
88+
TAG = "WsjcppJsonRpc20HandlerGameCreate";
89+
// setAccessUnauthorized(true);
90+
// setAccessUser(true);
91+
// setAccessTester(true);
92+
// setAccessAdmin(true);
93+
// void setActivatedFromVersion("v0.0.1"); // TODO authomatic set WSJCPP_APP_VERSION
94+
// void setDeprecatedFromVersion("");
95+
96+
// description of input params
97+
// requireStringParam("uuid", "object uuid")
98+
// .addValidator(new WsjcppValidatorUUID());
99+
// optionalStringParam("name", "Name of object")
100+
// .addValidator(new WsjcppValidatorStringLength(3,10));
101+
102+
// requireIntegerParam("cost", "Name of object")
103+
// .addValidator(new WsjcppValidatorIntegerMinValue(3))
104+
// .addValidator(new WsjcppValidatorIntegerMaxValue(1000));
105+
// optionalIntegerParam("age", "Name of object")
106+
// .addValidator(new WsjcppValidatorIntegerMinValue(0))
107+
108+
// requireBooleanParam("public", "True if object is public");
109+
// optionalBooleanParam("activated", "If object can handle");
78110
}
79111

80-
void MethodJsonRpc20_OrderRemove::handle(ModelRequest *pRequest) {
81-
WsjcppUserSession *pUserSession = pRequest->getUserSession();
82-
83-
int nId = pRequest->getInputInteger("id", -1);
112+
// ---------------------------------------------------------------------
84113

85-
// find id in database if not then return NOT_FOUND
114+
void WsjcppJsonRpc20HandlerGameCreate::handle(WsjcppJsonRpc20Request *pRequest) {
115+
WsjcppLog::err(TAG, "Not implemented");
116+
// TODO
117+
pRequest->fail(WsjcppJsonRpc20Error(501, "NOT_IMPLEMENTED"));
118+
}
119+
```
86120
87-
if (nId == -1) {
88-
pRequest->fail(404, "NOT_FOUND");
89-
return;
90-
}
91121
92-
// do remove order here
122+
## Automaticly generate client libraries on different languages
123+
124+
### Generate Python Library + PyPi Package
93125
94-
nlohmann::json jsonResponse;
95-
pRequest->done(jsonResponse);
126+
``` cpp
127+
128+
#include <wsjcpp_jsonrpc20_export_cli_python.h>
129+
#include <iostream>
130+
...
131+
void main() {
132+
std::string sExportDir = "./example-of-exported-client-libraries";
133+
134+
WsjcppJsonRpc20ExportCliPython exportCliPython(
135+
"./example-of-exported/py3",
136+
"libwsjcppjson20client"
137+
);
138+
exportCliPython.setAuthorName("Evgenii Sopov");
139+
exportCliPython.setAuthorEmail("mrseakg@gmail.com");
140+
exportCliPython.setAppName(std::string(WSJCPP_APP_NAME));
141+
exportCliPython.setAppVersion(std::string(WSJCPP_APP_VERSION));
142+
exportCliPython.setUrl("https://github.com/user/repo");
143+
exportCliPython.setDownloadUrl("https://github.com/user/repo/archive/" + std::string(WSJCPP_APP_NAME) + ".tar.gz");
144+
exportCliPython.setKeywords({std::string(WSJCPP_APP_NAME), "wsjcpp", "wsjcpp-jsonrpc20", "example-python-client"});
145+
146+
if (!exportCliPython.doExportLib()) {
147+
std::cout << "Failed!" << std::endl;
148+
} else {
149+
std::cout << "Success!" << std::endl;
150+
}
96151
}
152+
```
153+
154+
Usefull (official docs): https://packaging.python.org/tutorials/packaging-projects/
155+
156+
157+
In next step you need:
158+
159+
```
160+
$ cd ./example-of-exported/py3
161+
$ python3 setup.py sdist bdist_wheel
162+
... here will prepared dist/* directory
163+
$ twine check dist/*
164+
Checking dist/libwsjcppjson20client-0.0.2-py3-none-any.whl: PASSED
165+
Checking dist/libwsjcppjson20client-0.0.2.tar.gz: PASSED
166+
$ python3 -m pip install --user --upgrade twine
167+
$ python3 -m twine upload dist/*
168+
... here you need registered account on pypi
169+
```
97170

98-
```

example-of-exported/py3/API.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SomeClient Python Library
2+
3+
Automatically generated by wsjcpp-jsonrpc20.
4+
* Version: v0.0.2
5+
* Date: Thu, 10 Sep 2020 17:14:24 GMT
6+
7+
Example connect/disconnect:
8+
```
9+
from libwsjcppjson20client import SomeClient
10+
11+
client = SomeClient('ws://host:1234')
12+
...
13+
client.close()
14+
```
15+
16+
<details>
17+
<summary>game_create</summary>
18+
19+
## game_create
20+
21+
TODO description
22+
23+
Access: unauthorized - **no**, user - **yes**, tester - **yes**, admin - **yes**
24+
25+
#### Input params
26+
27+
* uuid - string, required; object uuid
28+
* name - string, optional; Name of object
29+
* cost - integer, required; Name of object
30+
* age - integer, optional; Name of object
31+
* public - boolean, required; True if object is public
32+
* activated - boolean, optional; If object can handle
33+
34+
35+
#### example call method
36+
37+
```
38+
response = client.game_create(
39+
uuid="",
40+
name="",
41+
cost=0,
42+
age=0,
43+
public=False,
44+
activated=False
45+
)
46+
```
47+
48+
</details>
49+
50+
<details>
51+
<summary>server_api</summary>
52+
53+
## server_api
54+
55+
This method Will be return list of all handlers
56+
57+
Access: unauthorized - **yes**, user - **yes**, tester - **no**, admin - **yes**
58+
59+
#### Input params
60+
61+
62+
63+
#### example call method
64+
65+
```
66+
response = client.server_api(
67+
68+
)
69+
```
70+
71+
</details>
72+

example-of-exported/py3/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#libwsjcppjson20client
2+
3+
SomeClient Python Library for wsjcpp-jsonrpc20
4+
5+
## Install
6+
7+
```
8+
$ pip3 install libwsjcppjson20client --upgrade
9+
```
10+
11+
## Example code
12+
13+
```
14+
#!/usr/bin/env python3
15+
# -*- coding: utf-8 -*-
16+
from libwsjcppjson20client import SomeClient
17+
18+
client = SomeClient("ws://host/ws-api/")
19+
20+
resp = client.server_api({})
21+
22+
print(resp)
23+
```
24+
25+
Full description API here: [API.md](./API.md)

example-of-exported/py3/setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import setuptools
2+
3+
with open('README.md', 'r') as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name='libwsjcppjson20client',
8+
version='v0.0.2',
9+
packages=['libwsjcppjson20client'],
10+
install_requires=['websocket-client>=0.56.0', 'requests>=2.21.0'],
11+
keywords=['wsjcpp-jsonrpc20', 'wsjcpp', 'wsjcpp-jsonrpc20', 'example-python-client'],
12+
author='Evgenii Sopov',
13+
author_email='mrseakg@gmail.com',
14+
description='SomeClient Python Library for wsjcpp-jsonrpc20',
15+
long_description=long_description,
16+
long_description_content_type='text/markdown',
17+
url='https://github.com/wsjcpp/wsjcpp-jsonrpc20',
18+
license='MIT',
19+
download_url='https://github.com/wsjcpp/wsjcpp-jsonrpc20/archive/wsjcpp-jsonrpc20.tar.gz',
20+
classifiers=[
21+
'Development Status :: 5 - Production/Stable',
22+
'License :: OSI Approved :: MIT License',
23+
'Operating System :: OS Independent',
24+
'Programming Language :: Python :: 3',
25+
'Programming Language :: Python :: 3.4',
26+
'Programming Language :: Python :: 3.5',
27+
'Programming Language :: Python :: 3.6',
28+
'Programming Language :: Python :: 3.7',
29+
],
30+
python_requires='>=3.6',
31+
)
32+

scripts.wsjcpp/generate.WsjcppJsonRpc20Handler

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,35 @@ concat content_source "
5454
// ---------------------------------------------------------------------
5555
// " class_name "
5656

57+
REGISTRY_WSJCPP_JSONRPC20_HANDLER(" class_name ")
58+
5759
" class_name "::" class_name "()
5860
: WsjcppJsonRpc20HandlerBase(\"" method_name "\", \"TODO description\") {
5961
TAG = \"" class_name "\";
6062
// setAccessUnauthorized(true);
6163
// setAccessUser(true);
6264
// setAccessTester(true);
6365
// setAccessAdmin(true);
64-
// void setActivatedFromVersion(\"v0.0.1\"); // TODO authomatic set WSJCPP_APP_VERSION
65-
// void setDeprecatedFromVersion(\"\");
66+
// setActivatedFromVersion(\"v0.0.1\"); // TODO authomatic set WSJCPP_APP_VERSION
67+
// setDeprecatedFromVersion(\"\");
6668

6769
// description of input params
70+
6871
// requireStringParam(\"uuid\", \"object uuid\")
6972
// .addValidator(new WsjcppValidatorUUID());
73+
7074
// optionalStringParam(\"name\", \"Name of object\")
7175
// .addValidator(new WsjcppValidatorStringLength(3,10));
7276

7377
// requireIntegerParam(\"cost\", \"Name of object\")
7478
// .addValidator(new WsjcppValidatorIntegerMinValue(3))
7579
// .addValidator(new WsjcppValidatorIntegerMaxValue(1000));
80+
7681
// optionalIntegerParam(\"age\", \"Name of object\")
77-
// .addValidator(new WsjcppValidatorIntegerMinValue(0))
82+
// .addValidator(new WsjcppValidatorIntegerMinValue(0));
7883

7984
// requireBooleanParam(\"public\", \"True if object is public\");
85+
8086
// optionalBooleanParam(\"activated\", \"If object can handle\");
8187
}
8288

0 commit comments

Comments
 (0)