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")
5654They 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- ```
0 commit comments