Skip to content

Commit

Permalink
Updated PoC structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kdimentionaltree committed Dec 18, 2024
1 parent ea3f08d commit ace14ce
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 48 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified ton-http-api/.DS_Store
Binary file not shown.
13 changes: 9 additions & 4 deletions ton-http-api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ set(USERVER_FEATURE_REDIS TRUE)
set(USERVER_FEATURE_UBOOST_CORO TRUE)
userver_setup_environment()

file(READ openapi/openapi.html OPENAPI_HTML_CONTENT)
file(READ openapi/openapi.json OPENAPI_JSON_CONTENT)
configure_file(openapi/openapi_page.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/openapi/openapi_page.hpp @ONLY)

set(TON_HTTP_API_CPP_SOURCE
main.cpp
handlers.hpp
handlers.cpp
tonlib_component.cpp
tonlib_component.h
tonlib_worker.cpp
tonlib_worker.h
handler_api_v2.cpp
handler_api_v2.h
tonlib_worker.cpp
tonlib_worker.h
handler_api_v2.cpp
handler_api_v2.h
openapi/openapi_page.hpp
)

add_executable(${PROJECT_NAME} ${TON_HTTP_API_CPP_SOURCE})
Expand Down
52 changes: 25 additions & 27 deletions ton-http-api/handler_api_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,45 @@
#include "auto/tl/tonlib_api.h"
#include "auto/tl/tonlib_api_json.h"
#include "td/utils/JsonBuilder.h"
#include "tonlib-multiclient/request.h"
#include "userver/components/component_context.hpp"
#include "userver/http/common_headers.hpp"
#include "openapi/openapi_page.hpp"

namespace ton_http::handlers {
std::string ApiV2Handler::HandleRequestThrow(
const userver::server::http::HttpRequest& request, userver::server::request::RequestContext& context
) const {
request.GetHttpResponse().SetContentType(userver::http::content_type::kApplicationJson);
auto& ton_api_method = request.GetPathArg("ton_api_method");
LOG_WARNING() << "Got request " << ton_api_method;

if ((ton_api_method == "index.html") || ton_api_method.empty()) {
request.GetHttpResponse().SetHeader(userver::http::headers::kContentType, "text/html; charset=utf-8");
return openapi::GetOpenApiPage();
}
if (ton_api_method == "openapi.json") {
request.GetHttpResponse().SetContentType(userver::http::content_type::kApplicationJson);
return openapi::GetOpenApiJson();
}
// call method
td::StringBuilder result_sb;
bool is_ok = false;
request.GetHttpResponse().SetContentType(userver::http::content_type::kApplicationJson);
core::TonlibWorkerResponse response{false, nullptr, td::Status::Error("not implemented")};
if (ton_api_method == "getMasterchainInfo") {
auto resp = tonlib_component_.DoRequest(multiclient::Request<ton::tonlib_api::blocks_getMasterchainInfo>{
.parameters = {.mode = multiclient::RequestMode::Multiple, .clients_number = 1},
.request_creator = [] {
return ton::tonlib_api::blocks_getMasterchainInfo();
},
});
if (resp.is_error()) {
result_sb << resp.move_as_error();
} else {
const auto res = resp.move_as_ok();
auto str = td::json_encode<td::string>(td::ToJson(res));
result_sb << str;
is_ok = true;
}
}
else {
result_sb << "not implemented";
response = tonlib_component_.DoRequest(&core::TonlibWorker::getMasterchainInfo);
}

auto result = userver::formats::json::ValueBuilder();
result["ok"] = is_ok;
result[(is_ok? "result" : "error")] = userver::formats::json::FromString(result_sb.as_cslice().str());
result["ok"] = response.is_ok;
if (response.is_ok) {
result["result"] = userver::formats::json::FromString(td::json_encode<td::string>(td::ToJson(response.result)));
} else {
result["error"] = response.error->to_string();
}
return userver::formats::json::ToString(result.ExtractValue());
}
ApiV2Handler::ApiV2Handler(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context)
: HttpHandlerBase(config, context), tonlib_component_(context.FindComponent<ton_http::core::TonlibComponent>())
{}
ApiV2Handler::ApiV2Handler(
const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context
) :
HttpHandlerBase(config, context), tonlib_component_(context.FindComponent<ton_http::core::TonlibComponent>()) {
}

}
} // namespace ton_http::handlers
8 changes: 5 additions & 3 deletions ton-http-api/handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

namespace ton_http::handlers {

std::string HelloHandler::HandleRequestThrow(const userver::server::http::HttpRequest &request, userver::server::request::RequestContext &context) const {
std::string HelloHandler::HandleRequestThrow(
const userver::server::http::HttpRequest& request, userver::server::request::RequestContext& context
) const {
request.GetHttpResponse().SetContentType(userver::http::content_type::kApplicationJson);
auto &name = request.GetArg("name");
auto& name = request.GetArg("name");

auto result = userver::formats::json::ValueBuilder();
result["ok"] = true;
result["result"] = "Hello!";
return userver::formats::json::ToString(result.ExtractValue());
}
}
} // namespace ton_http::handlers
1 change: 0 additions & 1 deletion ton-http-api/handlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ class HelloHandler final : public userver::server::handlers::HttpHandlerBase {
using HttpHandlerBase::HttpHandlerBase;
std::string HandleRequestThrow(const userver::server::http::HttpRequest &request, userver::server::request::RequestContext &context) const override;
};

}
35 changes: 35 additions & 0 deletions ton-http-api/openapi/openapi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '/api/v2/openapi.json',
layout: "BaseLayout",
deepLinking: true,
dom_id: '#swagger-ui',
plugins: [SwaggerUIBundle.plugins.DownloadURL],
defaultModelRendering: "example",
docExpansion:"list",
tryItOutEnabled:true,
showMutatedRequest:false,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
syntaxHighlight: {"activate":true,"theme":"agate"}
});
};
</script>
</body>
</html>
158 changes: 158 additions & 0 deletions ton-http-api/openapi/openapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"openapi": "3.1.0",
"info": {
"title": "TON HTTP API C++",
"description": "\nThis API enables HTTP access to TON blockchain - getting accounts and wallets information, looking up blocks and transactions, sending messages to the blockchain, calling get methods of smart contracts, and more.\n\nIn addition to REST API, all methods are available through [JSON-RPC endpoint](#json%20rpc) with `method` equal to method name and `params` passed as a dictionary.\n\nThe response contains a JSON object, which always has a boolean field `ok` and either `error` or `result`. If `ok` equals true, the request was successful and the result of the query can be found in the `result` field. In case of an unsuccessful request, `ok` equals false and the error is explained in the `error`.\n\nAPI Key should be sent either as `api_key` query parameter or `X-API-Key` header.\n",
"version": "2.1.0"
},
"paths": {
"/api/v2/getMasterchainInfo": {
"get": {
"tags": [
"blocks"
],
"summary": "Get Masterchain Info",
"description": "Get up-to-date masterchain state.",
"operationId": "get_masterchain_info_getMasterchainInfo_get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TonResponse"
}
}
}
},
"422": {
"description": "Validation Error"
},
"504": {
"description": "Lite Server Timeout"
}
},
"security": [
{
"APIKeyHeader": []
},
{
"APIKeyQuery": []
}
]
}
}
},
"components": {
"schemas": {
"TonRequestJsonRPC": {
"properties": {
"method": {
"type": "string",
"title": "Method"
},
"params": {
"type": "object",
"title": "Params",
"default": {

}
},
"id": {
"type": "string",
"title": "Id"
},
"jsonrpc": {
"type": "string",
"title": "Jsonrpc"
}
},
"type": "object",
"required": [
"method"
],
"title": "TonRequestJsonRPC"
},
"TonResponse": {
"properties": {
"ok": {
"type": "boolean",
"title": "Ok"
},
"result": {
"anyOf": [
{
"type": "string"
},
{
"items": {

},
"type": "array"
},
{
"type": "object"
}
],
"title": "Result"
},
"error": {
"type": "string",
"title": "Error"
},
"code": {
"type": "integer",
"title": "Code"
}
},
"type": "object",
"required": [
"ok"
],
"title": "TonResponse"
}
},
"securitySchemes": {
"APIKeyHeader": {
"type": "apiKey",
"in": "header",
"name": "X-API-Key"
},
"APIKeyQuery": {
"type": "apiKey",
"in": "query",
"name": "api_key"
}
}
},
"tags": [
{
"name": "accounts",
"description": "Information about accounts."
},
{
"name": "blocks",
"description": "Information about blocks."
},
{
"name": "transactions",
"description": "Fetching and locating transactions."
},
{
"name": "get config",
"description": "Get blockchain config"
},
{
"name": "run method",
"description": "Run get method of smart contract."
},
{
"name": "send",
"description": "Send data to blockchain."
},
{
"name": "json rpc",
"description": "JSON-RPC endpoint."
}
]
}
Loading

0 comments on commit ace14ce

Please sign in to comment.