Skip to content

Commit

Permalink
Merge pull request #91 from ndsev/release/1.4.0
Browse files Browse the repository at this point in the history
Release: 1.4.0
  • Loading branch information
josephbirkner authored Nov 25, 2022
2 parents b3d18c8 + 09db638 commit 4d71bbc
Show file tree
Hide file tree
Showing 26 changed files with 252 additions and 124 deletions.
57 changes: 57 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Documentation:
# https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormatStyleOptions.html
# CLion setup guide:
# https://www.jetbrains.com/help/clion/clangformat-as-alternative-formatter.html#clion-support
---
BasedOnStyle: Chromium
IndentWidth: 4
---
Language: Cpp
ColumnLimit: 100

# Always break after an open bracket, if the parameters don’t fit on a single line.
AlignAfterOpenBracket: AlwaysBreak

# Function call’s arguments will either be all on the same line or will have one line each.
BinPackArguments: false
BinPackParameters: false
AllowAllArgumentsOnNextLine: false

BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: MultiLine
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
BeforeLambdaBody: true

# while (true) { continue; }
AllowShortBlocksOnASingleLine: Always
AllowShortLambdasOnASingleLine: Empty

# case 1: return 2;
AllowShortCaseLabelsOnASingleLine: true
IndentCaseBlocks: false
IndentCaseLabels: false

AlignOperands: false
ContinuationIndentWidth: 4

BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: false

IndentAccessModifiers: false
AccessModifierOffset: -4

PenaltyIndentedWhitespace: 100
PenaltyBreakAssignment: 100
PenaltyReturnTypeOnItsOwnLine: 100
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyBreakOpenParenthesis: 200
10 changes: 5 additions & 5 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ jobs:
run: |
choco install --no-progress -y openssl
echo "cmake -DPython3_ROOT_DIR=$env:pythonLocation"
cmake "-DPython3_ROOT_DIR=$env:pythonLocation" -DPython3_FIND_REGISTRY=LAST -DCMAKE_BUILD_TYPE=Release ..
cmake "-DPython3_ROOT_DIR=$env:pythonLocation" -DPython3_FIND_REGISTRY=LAST -DHTTPLIB_USE_ZLIB_IF_AVAILABLE=OFF -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release
- name: Test
working-directory: build
run: |
ctest -C Release --verbose --no-test=fail
- name: Deploy
uses: actions/upload-artifact@v2
with:
name: zswag-py${{ matrix.python-version }}-${{ matrix.os }}
path: build/bin/wheel/*.whl
- name: Test
working-directory: build
run: |
ctest -C Release --verbose --no-test=fail
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include(FetchContent)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ZSWAG_VERSION 1.3.0)
set(ZSWAG_VERSION 1.4.0)

option(ZSWAG_BUILD_WHEELS "Enable zswag whl-output to WHEEL_DEPLOY_DIRECTORY." ON)
option(ZSWAG_KEYCHAIN_SUPPORT "Enable zswag keychain support." ON)
Expand Down
2 changes: 1 addition & 1 deletion deps/Catch2
Submodule Catch2 updated 654 files
2 changes: 1 addition & 1 deletion deps/zserio
Submodule zserio updated 629 files
9 changes: 9 additions & 0 deletions libs/httpcl/include/httpcl/http-settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ using Query = std::multimap<std::string, std::string>;
*/
struct Config
{
Config() = default;
Config(std::string const& yamlConf);

struct BasicAuthentication {
std::string user;
std::string password;
Expand Down Expand Up @@ -55,6 +58,12 @@ struct Config
* May read keychain passwords which can block and require user interaction.
*/
void apply(httplib::Client& cl) const;

/**
* Convert this configuration to a YAML string, which may
* be passed to the respective `Config(yamlConf)` constructor.
*/
std::string toYaml() const;
};

/**
Expand Down
48 changes: 30 additions & 18 deletions libs/httpcl/src/http-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ auto makeClientAndApplyQuery(
config.apply(*client);

applyQuery(uri, config);
if (httpcl::log().should_log(spdlog::level::debug)) {
httpcl::log().debug(" ... full URI: {}", uri.build());
}
return client;
}

Expand Down Expand Up @@ -58,52 +61,61 @@ Result HttpLibHttpClient::get(const std::string& uriStr,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Get(
uri.buildPath().c_str()));
return makeResult(
makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)
->Get(uri.buildPath().c_str()));
}

Result HttpLibHttpClient::post(const std::string& uriStr,
const std::optional<BodyAndContentType>& body,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Post(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
return makeResult(
makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)
->Post(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
}

Result HttpLibHttpClient::put(const std::string& uriStr,
const std::optional<BodyAndContentType>& body,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Put(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
return makeResult(
makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)
->Put(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
}

Result HttpLibHttpClient::del(const std::string& uriStr,
const std::optional<BodyAndContentType>& body,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Delete(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
return makeResult(
makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)
->Delete(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
}

Result HttpLibHttpClient::patch(const std::string& uriStr,
const std::optional<BodyAndContentType>& body,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Patch(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
return makeResult(
makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)
->Patch(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
}

Result MockHttpClient::get(const std::string& uri,
Expand Down
138 changes: 78 additions & 60 deletions libs/httpcl/src/http-settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,71 @@ struct convert<Config::Proxy>
};
}

namespace {
YAML::Node configToNode(Config const& config, std::string const& url=".*") {
YAML::Node result;
result["url"] = url;

if (!config.cookies.empty())
result["cookies"] = config.cookies;

if (!config.headers.empty())
result["headers"] =
std::map<std::string, std::string>{config.headers.begin(), config.headers.end()};

if (!config.query.empty())
result["query"] =
std::map<std::string, std::string>{config.query.begin(), config.query.end()};

if (config.auth)
result["basic-auth"] = *config.auth;

if (config.proxy)
result["proxy"] = *config.proxy;

if (config.apiKey)
result["api-key"] = *config.apiKey;

return result;
}

std::pair<Config, std::string> configFromNode(YAML::Node const& node)
{
std::string urlPattern;
Config conf;

if (auto entryParam = node["url"])
urlPattern = entryParam.as<std::string>();
else
throw std::runtime_error(
"HTTP Settings: Missing 'url' field in: " + YAML::Dump(node));

if (auto cookies = node["cookies"])
conf.cookies = cookies.as<std::map<std::string, std::string>>();

if (auto headers = node["headers"]) {
auto headersMap = headers.as<std::map<std::string, std::string>>();
conf.headers.insert(headersMap.begin(), headersMap.end());
}

if (auto query = node["query"]) {
auto queryMap = query.as<std::map<std::string, std::string>>();
conf.query.insert(queryMap.begin(), queryMap.end());
}

if (auto basicAuth = node["basic-auth"])
conf.auth = basicAuth.as<Config::BasicAuthentication>();

if (auto proxy = node["proxy"])
conf.proxy = proxy.as<Config::Proxy>();

if (auto apiKey = node["api-key"])
conf.apiKey = apiKey.as<std::string>();

return {std::move(conf), std::move(urlPattern)};
}
}

std::string secret::load(
const std::string &service,
const std::string &user)
Expand Down Expand Up @@ -241,38 +306,7 @@ void Settings::load()
uint32_t idx = 0;

for (auto const& entry : node.as<std::vector<YAML::Node>>()) {
Config conf;
std::string urlPattern;

if (auto entryParam = entry["url"])
urlPattern = entryParam.as<std::string>();
else
throw std::runtime_error(
"Settings: Failed to read 'url' of entry #"s + std::to_string(idx) +
" in " + cookieJar);

if (auto cookies = entry["cookies"])
conf.cookies = cookies.as<std::map<std::string, std::string>>();

if (auto headers = entry["headers"]) {
auto headersMap = headers.as<std::map<std::string, std::string>>();
conf.headers.insert(headersMap.begin(), headersMap.end());
}

if (auto query = entry["query"]) {
auto queryMap = query.as<std::map<std::string, std::string>>();
conf.query.insert(queryMap.begin(), queryMap.end());
}

if (auto basicAuth = entry["basic-auth"])
conf.auth = basicAuth.as<Config::BasicAuthentication>();

if (auto proxy = entry["proxy"])
conf.proxy = proxy.as<Config::Proxy>();

if (auto apiKey = entry["api-key"])
conf.apiKey = apiKey.as<std::string>();

auto [conf, urlPattern] = configFromNode(entry);
settings[urlPattern] = std::move(conf);
++idx;
}
Expand All @@ -296,34 +330,8 @@ void Settings::store()
try {
auto node = YAML::Node();

for (const auto& pair : settings) {
auto settingsNode = YAML::Node();

settingsNode["url"] = pair.first;
const auto& entry = pair.second;

if (!entry.cookies.empty())
settingsNode["cookies"] = entry.cookies;

if (!entry.headers.empty())
settingsNode["headers"] = std::map<std::string, std::string>{
entry.headers.begin(), entry.headers.end()};

if (!entry.query.empty())
settingsNode["query"] = std::map<std::string, std::string>{
entry.query.begin(), entry.query.end()};

if (entry.auth)
settingsNode["basic-auth"] = *entry.auth;

if (entry.proxy)
settingsNode["proxy"] = *entry.proxy;

if (entry.apiKey)
settingsNode["api-key"] = *entry.apiKey;

node.push_back(settingsNode);
}
for (const auto& [key, config] : settings)
node.push_back(configToNode(config));

log().debug("Saving HTTP settings to '{}'...", cookieJar);
std::ofstream os(cookieJar);
Expand All @@ -334,6 +342,12 @@ void Settings::store()
}
}

Config::Config(const std::string& yamlConf)
{
YAML::Node parsedYaml = YAML::Load(yamlConf);
*this = configFromNode(parsedYaml).first;
}

Config Settings::operator[] (const std::string &url) const
{
Config result;
Expand Down Expand Up @@ -389,6 +403,10 @@ void Config::apply(httplib::Client &cl) const
cl.set_default_headers(httpLibHeaders);
}

std::string Config::toYaml() const {
return YAML::Dump(configToNode(*this));
}

Config& Config::operator |= (Config const& other) {
cookies.insert(other.cookies.begin(), other.cookies.end());
headers.insert(other.headers.begin(), other.headers.end());
Expand Down
2 changes: 1 addition & 1 deletion libs/httpcl/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_executable(httpcl-test
target_link_libraries(httpcl-test
PUBLIC
httpcl
Catch2::Catch2)
Catch2::Catch2WithMain)

if (ZSWAG_ENABLE_TESTING)
add_test(NAME httpcl-test
Expand Down
2 changes: 1 addition & 1 deletion libs/httpcl/test/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
2 changes: 1 addition & 1 deletion libs/httpcl/test/src/uri.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>

#include "httpcl/uri.hpp"

Expand Down
Loading

0 comments on commit 4d71bbc

Please sign in to comment.