Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preparing v5.0.0 #13

Draft
wants to merge 46 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
fc579f9
Add clang-format
wtlgo Jan 10, 2023
0444084
Add clang-format
wtlgo Jan 10, 2023
1bf9e0d
Adjust CmakeLists.txt
wtlgo Jan 10, 2023
6b6d16f
Add missing const qualifiers
wtlgo Jan 10, 2023
7c49425
Move old implementation to wtlgo::old namespace
wtlgo Jan 11, 2023
0a55dfc
Rename old implementation
wtlgo Jan 11, 2023
fddad41
Add Config class
wtlgo Jan 11, 2023
b8dd724
Rename old tests
wtlgo Jan 11, 2023
45f90fe
Add CurlController class
wtlgo Jan 11, 2023
b5c4a92
Add PATCH HTTP method
wtlgo Jan 11, 2023
02ffe64
Add Client class base
wtlgo Jan 11, 2023
cae2f27
Add data to config
wtlgo Jan 12, 2023
fb0077c
Add timeout to config
wtlgo Jan 12, 2023
79d269e
Add HttpBasicAuth class
wtlgo Jan 12, 2023
292432e
Add auth to config
wtlgo Jan 12, 2023
67205f8
Screw it. from the scratch. Add HttpBasicAuth
wtlgo Jan 23, 2023
51aa921
Make destructor virtual
wtlgo Jan 23, 2023
963eb33
Rename sptr_t to ptr_t
wtlgo Jan 23, 2023
56d1617
Add HttpMethod
wtlgo Jan 23, 2023
38be017
Add Config
wtlgo Jan 23, 2023
a51ff2b
Add DefaultHttpBasicAuth.Clone test
wtlgo Jan 23, 2023
99ef731
Update DefaultHttpBasicAuth.Clone
wtlgo Jan 23, 2023
50e83a0
Add DefaultConfig.Clone test
wtlgo Jan 23, 2023
de5f0d3
Remove unnecessary assertions
wtlgo Jan 23, 2023
ef528f0
Add Url
wtlgo Jan 23, 2023
b2120b5
Add merge to Config
wtlgo Jan 23, 2023
fd3a27a
Add alias to DefaultConfig
wtlgo Jan 23, 2023
1cc2572
Factor out merge and copy shenenigans
wtlgo Jan 23, 2023
6747523
Add missing override keywork
wtlgo Jan 27, 2023
c66c685
Add merge to Config
wtlgo Jan 27, 2023
b3e0f34
Add base_url to Config
wtlgo Jan 31, 2023
9d27561
Remove dead code
wtlgo Jan 31, 2023
e7b3ba0
Add Headers interface and HttpHeaders class
wtlgo Jan 31, 2023
0d58e87
Apply appropriate access modifier
wtlgo Jan 31, 2023
1b06660
Rename HttpBasicAuth to Auth
wtlgo Jan 31, 2023
75e0fa8
Rename DefaultHttpBasicAuth to HttpBasicAuth
wtlgo Jan 31, 2023
4d6c8f3
Rename DefaultConfig to ClientConfig
wtlgo Jan 31, 2023
4d1c0aa
Add const qualifier
wtlgo Jan 31, 2023
a302798
Add header to Config
wtlgo Jan 31, 2023
a10feef
Create client base
wtlgo Feb 2, 2023
6b7557c
Create internal subdir structure
wtlgo Feb 2, 2023
10cefc0
Create CurlHandler template
wtlgo Feb 2, 2023
9d6f1a4
Remove private implementation in internal classes
wtlgo Feb 2, 2023
98039d0
Add CurlHandler Builder template
wtlgo Feb 2, 2023
bc29be7
Implement CurlEasyHandler
wtlgo Feb 2, 2023
32f243a
Add some methods to CurlEasyHandlerBuilder
wtlgo Feb 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BasedOnStyle: Google
IndentWidth: 4
AccessModifierOffset: -4
52 changes: 46 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,52 @@ project(

find_package(CURL REQUIRED)

add_library(Network src/Network.cpp include/wtlgo/Network.hpp)
set(PUBLIC_HEADERS
"include/wtlgo/NetworkOld.hpp"

"include/wtlgo/network/Auth.hpp"
"include/wtlgo/network/HttpBasicAuth.hpp"

"include/wtlgo/network/HttpMethod.hpp"

"include/wtlgo/network/Config.hpp"
"include/wtlgo/network/ClientConfig.hpp"

"include/wtlgo/network/Headers.hpp"
"include/wtlgo/network/HttpHeaders.hpp"

"include/wtlgo/network/Client.hpp"
"include/wtlgo/network/CurlClient.hpp"
)

set(SOURCES
"src/NetworkOld.cpp"
"src/HttpBasicAuth.cpp"
"src/ClientConfig.cpp"
"src/HttpHeaders.cpp"
"src/CurlClient.cpp"

"src/internal/config/MergedConfig.hpp"
"src/internal/config/MergedConfig.cpp"
"src/internal/config/CopyOnWriteConfig.hpp"
"src/internal/config/CopyOnWriteConfig.cpp"

"src/internal/curl/Types.hpp"
"src/internal/curl/CurlEasyHandler.hpp"
"src/internal/curl/CurlEasyHandler.cpp"
"src/internal/curl/CurlEasyHandlerBuilder.hpp"
"src/internal/curl/CurlEasyHandlerBuilder.cpp"
)

add_library(Network ${SOURCES})
target_compile_features(Network PUBLIC cxx_std_17)
target_link_libraries(Network PUBLIC ${CURL_LIBRARIES})
target_link_libraries(Network PUBLIC CURL::libcurl)

target_include_directories(Network PUBLIC include/)
target_include_directories(Network PRIVATE include/wtlgo/ ${CURL_INCLUDE_DIR})
target_include_directories(Network PRIVATE include/)

set_target_properties(Network PROPERTIES
PUBLIC_HEADER include/wtlgo/Network.hpp
PUBLIC_HEADER "${PUBLIC_HEADERS}"
VERSION ${PROJECT_VERSION}
)

Expand All @@ -28,5 +66,7 @@ install(
PUBLIC_HEADER DESTINATION include/wtlgo COMPONENT Development
)

enable_testing()
add_subdirectory(test)
if(DEFINED ENABLE_TESTING)
enable_testing()
add_subdirectory(test)
endif()
41 changes: 24 additions & 17 deletions include/wtlgo/Network.hpp → include/wtlgo/NetworkOld.hpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
#ifndef Network_hpp
#define Network_hpp
#ifndef __NETWORK_HPP__
#define __NETWORK_HPP__

#include <cstdio>
#include <string>
#include <vector>
#include <map>

namespace wtlgo {
namespace old {

class Network {
public:
static Network& instance();

Network(Network const&) = delete;
void operator=(Network const&) = delete;

void set_proxy(const std::string& proxy);

std::string request(std::string url, const std::map<std::string, std::string>& args = {}, bool post = false) const;

std::string upload(std::string url, const std::string& fieldname, const std::string& filename) const;

bool download(const std::string& url, const std::string& save_as = "") const;


std::string request(std::string url,
const std::map<std::string, std::string>& args = {},
bool post = false) const;

std::string upload(std::string url, const std::string& fieldname,
const std::string& filename) const;

bool download(const std::string& url,
const std::string& save_as = "") const;

private:
std::string proxy;

Network();
~Network();

static size_t string_writer(char* contents, size_t size, size_t nmemb, std::string* stream);
static size_t file_writer(void* contents, size_t size, size_t nmemb, FILE* stream);

std::string join(const std::vector<std::string> lst, const std::string& delim) const;
static size_t string_writer(char* contents, size_t size, size_t nmemb,
std::string* stream);
static size_t file_writer(void* contents, size_t size, size_t nmemb,
FILE* stream);

std::string join(const std::vector<std::string> lst,
const std::string& delim) const;
std::string url_encode(const std::string& str) const;
};

extern Network& network;

}

}
#endif /* Network_hpp */
29 changes: 29 additions & 0 deletions include/wtlgo/network/Auth.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef __WTLGO__NETWORK__AUTH__
#define __WTLGO__NETWORK__AUTH__

#include <memory>
#include <string_view>

namespace wtlgo {
namespace network {
struct Auth {
using ptr_t = std::shared_ptr<Auth>;
using cptr_t = std::shared_ptr<const Auth>;

using username_ref_t = std::string_view;
using password_ref_t = std::string_view;

virtual ~Auth() = default;

virtual ptr_t clone() const = 0;

virtual username_ref_t username() const = 0;
virtual ptr_t username(username_ref_t) = 0;

virtual password_ref_t password() const = 0;
virtual ptr_t password(password_ref_t) = 0;
};
}
}

#endif
24 changes: 24 additions & 0 deletions include/wtlgo/network/Client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __WTLGO__NETWORK__CLIENT__
#define __WTLGO__NETWORK__CLIENT__

#include <memory>

#include <wtlgo/network/Config.hpp>

namespace wtlgo {
namespace network {
struct Client {
using ptr_t = std::shared_ptr<Client>;
using cptr_t = std::shared_ptr<const Client>;

virtual ~Client() = default;

using config_ptr_t = Config::cptr_t;

virtual config_ptr_t config() const = 0;
virtual ptr_t config(config_ptr_t) = 0;
};
}
}

#endif
51 changes: 51 additions & 0 deletions include/wtlgo/network/ClientConfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef __WTLGO__NETWORK__CLIENT_CONFIG__
#define __WTLGO__NETWORK__CLIENT_CONFIG__

#include <memory>

#include <wtlgo/network/Config.hpp>

namespace wtlgo {
namespace network {
class ClientConfig : public Config,
public std::enable_shared_from_this<ClientConfig> {
public:
using ptr_t = std::shared_ptr<ClientConfig>;
using cptr_t = std::shared_ptr<const ClientConfig>;

virtual ~ClientConfig();

[[nodiscard]] static ClientConfig::ptr_t create();
[[nodiscard]] static ClientConfig::ptr_t clone(Config::cptr_t);

Config::ptr_t clone() const override;
Config::ptr_t merge(Config::cptr_t) const override;

Config::url_opt_ref_t url() const override;
Config::ptr_t url(Config::url_ref_t) override;
Config::ptr_t clear_url() override;

Config::method_opt_t method() const override;
Config::ptr_t method(Config::method_t) override;
Config::ptr_t clear_method() override;

Config::url_opt_ref_t base_url() const override;
Config::ptr_t base_url(Config::url_ref_t) override;
Config::ptr_t clear_base_url() override;

Config::headers_opt_t headers() const override;
Config::ptr_t headers(headers_opt_t) override;
Config::ptr_t clear_headers() override;

protected:
ClientConfig();
ClientConfig(const Config::cptr_t);

private:
struct Impl;
const std::unique_ptr<Impl> impl;
};
}
}

#endif
49 changes: 49 additions & 0 deletions include/wtlgo/network/Config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef __WTLGO__NETWORK__CONFIG__
#define __WTLGO__NETWORK__CONFIG__

#include <memory>
#include <string_view>
#include <optional>

#include <wtlgo/network/HttpMethod.hpp>
#include <wtlgo/network/Headers.hpp>

namespace wtlgo {
namespace network {
struct Config {
using ptr_t = std::shared_ptr<Config>;
using cptr_t = std::shared_ptr<const Config>;

virtual ~Config() = default;

[[nodiscard]] virtual ptr_t clone() const = 0;
[[nodiscard]] virtual ptr_t merge(cptr_t) const = 0;

using url_ref_t = std::string_view;
using url_opt_ref_t = std::optional<url_ref_t>;

virtual url_opt_ref_t url() const = 0;
virtual ptr_t url(url_ref_t) = 0;
virtual ptr_t clear_url() = 0;

using method_t = HttpMethod;
using method_opt_t = std::optional<method_t>;

virtual method_opt_t method() const = 0;
virtual ptr_t method(method_t) = 0;
virtual ptr_t clear_method() = 0;

virtual url_opt_ref_t base_url() const = 0;
virtual ptr_t base_url(url_ref_t) = 0;
virtual ptr_t clear_base_url() = 0;

using headers_opt_t = Headers::cptr_t;

virtual headers_opt_t headers() const = 0;
virtual ptr_t headers(headers_opt_t) = 0;
virtual ptr_t clear_headers() = 0;
};
}
}

#endif
33 changes: 33 additions & 0 deletions include/wtlgo/network/CurlClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef __WTLGO__NETWORK__CURL_CLIENT__
#define __WTLGO__NETWORK__CURL_CLIENT__

#include <memory>

#include <wtlgo/network/Client.hpp>

namespace wtlgo {
namespace network {
class CurlClient : public Client,
public std::enable_shared_from_this<CurlClient> {
public:
using ptr_t = std::shared_ptr<CurlClient>;
using cptr_t = std::shared_ptr<const CurlClient>;

virtual ~CurlClient();

[[nodiscard]] static CurlClient::ptr_t create();

Client::config_ptr_t config() const override;
Client::ptr_t config(Client::config_ptr_t) override;

protected:
CurlClient();

private:
struct Impl;
const std::unique_ptr<Impl> impl;
};
}
}

#endif
33 changes: 33 additions & 0 deletions include/wtlgo/network/Headers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef __WTLGO__NETWORK__HEADERS__
#define __WTLGO__NETWORK__HEADERS__

#include <memory>
#include <string_view>
#include <set>
#include <optional>

namespace wtlgo {
namespace network {
struct Headers {
using ptr_t = std::shared_ptr<Headers>;
using cptr_t = std::shared_ptr<const Headers>;

virtual ~Headers() = default;

using key_ref_t = std::string_view;
using key_ref_list_t = std::set<key_ref_t>;

using value_ref_t = std::string_view;
using value_ref_opt_t = std::optional<value_ref_t>;

virtual ptr_t clone() const = 0;

virtual key_ref_list_t keys() const = 0;
virtual value_ref_opt_t get(key_ref_t header) const = 0;
virtual ptr_t set(key_ref_t header, value_ref_t value) = 0;
virtual ptr_t erase(key_ref_t header) = 0;
};
}
}

#endif
Loading