mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 13:54:57 +00:00
docs: dbal,requests,hpp (2 files)
This commit is contained in:
@@ -156,6 +156,21 @@ permissions:
|
||||
|
||||
## API Contract Example
|
||||
|
||||
### HTTP Utilities
|
||||
|
||||
For outbound integrations the daemon can use the new requests-inspired helper `runtime::RequestsClient`. It wraps Drogon’s `HttpClient`, exposes `get`/`post` helpers, parses JSON responses, and throws clean timeouts so code paths stay predictable.
|
||||
|
||||
```cpp
|
||||
using namespace dbal::runtime;
|
||||
|
||||
RequestsClient http("https://api.prisma.example");
|
||||
auto response = http.post("/rpc/execute", jsonPayload.dump(), {{"Authorization", "Bearer ..."}});
|
||||
if (response.statusCode == 200) {
|
||||
const auto result = response.json["result"];
|
||||
// handle Prisma response
|
||||
}
|
||||
```
|
||||
|
||||
### Entity Definition (YAML)
|
||||
|
||||
```yaml
|
||||
|
||||
88
dbal/cpp/src/runtime/requests_client.hpp
Normal file
88
dbal/cpp/src/runtime/requests_client.hpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#ifndef DBAL_REQUESTS_CLIENT_HPP
|
||||
#define DBAL_REQUESTS_CLIENT_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <future>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <drogon/HttpClient.h>
|
||||
#include <drogon/HttpReqImpl.h>
|
||||
#include <drogon/HttpResponse.h>
|
||||
#include <drogon/HttpTypes.h>
|
||||
#include <drogon/Json.h>
|
||||
|
||||
namespace dbal {
|
||||
namespace runtime {
|
||||
|
||||
struct RequestsResponse {
|
||||
int statusCode;
|
||||
std::string body;
|
||||
drogon::Json::Value json;
|
||||
std::unordered_map<std::string, std::string> headers;
|
||||
};
|
||||
|
||||
class RequestsClient {
|
||||
public:
|
||||
explicit RequestsClient(const std::string& baseURL)
|
||||
: client_(drogon::HttpClient::newHttpClient(baseURL)) {}
|
||||
|
||||
RequestsResponse get(const std::string& path,
|
||||
const std::unordered_map<std::string, std::string>& headers = {},
|
||||
int timeoutMs = 30'000) {
|
||||
return request("GET", path, headers, {}, timeoutMs);
|
||||
}
|
||||
|
||||
RequestsResponse post(const std::string& path,
|
||||
const std::string& body,
|
||||
const std::unordered_map<std::string, std::string>& headers = {},
|
||||
int timeoutMs = 30'000) {
|
||||
return request("POST", path, headers, body, timeoutMs);
|
||||
}
|
||||
|
||||
RequestsResponse request(const std::string& method,
|
||||
const std::string& path,
|
||||
const std::unordered_map<std::string, std::string>& headers = {},
|
||||
const std::string& body = {},
|
||||
int timeoutMs = 30'000) {
|
||||
drogon::HttpRequestPtr req = drogon::HttpRequest::newHttpRequest();
|
||||
req->setMethod(drogon::HttpMethod::fromString(method));
|
||||
req->setPath(path);
|
||||
req->setBody(body);
|
||||
for (const auto& [key, value] : headers) {
|
||||
req->addHeader(key, value);
|
||||
}
|
||||
|
||||
std::promise<RequestsResponse> promise;
|
||||
auto future = promise.get_future();
|
||||
|
||||
client_->sendRequest(req, [p = std::move(promise), timeoutMs](drogon::ReqResult result, const drogon::HttpResponsePtr& resp) mutable {
|
||||
RequestsResponse response;
|
||||
response.statusCode = resp ? resp->getStatusCode() : 0;
|
||||
response.body = resp ? resp->getBody() : "";
|
||||
if (resp) {
|
||||
response.headers = resp->getHeaders();
|
||||
try {
|
||||
response.json = drogon::Json::parse(resp->getBody());
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
p.set_value(std::move(response));
|
||||
});
|
||||
|
||||
if (future.wait_for(std::chrono::milliseconds(timeoutMs)) == std::future_status::timeout) {
|
||||
throw std::runtime_error("HTTP request timed out");
|
||||
}
|
||||
|
||||
return future.get();
|
||||
}
|
||||
|
||||
private:
|
||||
drogon::HttpClientPtr client_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user