Skip to content

Commit

Permalink
Helper Improvements (#33)
Browse files Browse the repository at this point in the history
* Network - Add WebClient and CurlEasy Class

Replaces `Nickvision::WebHelpers` namespace

* Helpers - Improve Namespace Names

* Fix Namespace Usings
  • Loading branch information
nlogozzo authored Jun 11, 2024
1 parent 904d41d commit 7fcc304
Show file tree
Hide file tree
Showing 32 changed files with 542 additions and 244 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## 2024.6.4 (next)
### Breaking Changes
#### App
- `Nickvision::App::Aura::init()` will no longer initialize `libcurl`
#### Helpers
- Renamed `Nickvision::CodeHelpers` namespace to `Nickvision::Helpers::CodeHelpers`
- Renamed `Nickvision::StringHelpers` namespace to `Nickvision::Helpers::StringHelpers`
- Removed `Nickvision::WebHelpers` namespace
### New APIs
#### Network
- `Nickvision::Network::CurlEasy` class
- `Nickvision::Network::WebClient` class
### Fixes
None

## 2024.6.3
### Breaking Changes
- Removed `enumflags.h`
Expand Down
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

#libnick Definition
project ("libnick" LANGUAGES C CXX VERSION 2024.6.3 DESCRIPTION "A cross-platform base for native Nickvision applications.")
project ("libnick" LANGUAGES C CXX VERSION 2024.6.4 DESCRIPTION "A cross-platform base for native Nickvision applications.")
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
include(CTest)
Expand All @@ -46,7 +46,6 @@ add_library (${PROJECT_NAME}
"src/filesystem/systemdirectories.cpp"
"src/filesystem/userdirectories.cpp"
"src/helpers/stringhelpers.cpp"
"src/helpers/webhelpers.cpp"
"src/keyring/credential.cpp"
"src/keyring/keyring.cpp"
"src/keyring/keyringdialogcontroller.cpp"
Expand All @@ -56,8 +55,10 @@ add_library (${PROJECT_NAME}
"src/keyring/systemcredentials.cpp"
"src/localization/gettext.cpp"
"src/logging/logger.cpp"
"src/network/curleasy.cpp"
"src/network/networkmonitor.cpp"
"src/network/networkstatechangedeventargs.cpp"
"src/network/webclient.cpp"
"src/notifications/notificationsenteventargs.cpp"
"src/notifications/notifyicon.cpp"
"src/notifications/notifyiconmenu.cpp"
Expand Down Expand Up @@ -145,8 +146,7 @@ if (BUILD_TESTING)
"tests/systemtests.cpp"
"tests/taskbartests.cpp"
"tests/updatertests.cpp"
"tests/versiontests.cpp"
"tests/webtests.cpp")
"tests/versiontests.cpp")
find_package(GTest CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME}_test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main ${PROJECT_NAME})
endif()
1 change: 0 additions & 1 deletion docs/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ Path: `Nickvision::App::Aura`
```
- Accepts: An application id, id, an application name, name, an application english short name, englishShortName, and the application log level, logLevel.
- Returns: True if initialized, else false
- Throws: `std::runtime_error` if libcurl fails to initialize
- Throws: `std::runtime_error` if the gettext system fails to initialize
- Throws: `std::runtime_error` if unable to get the executable directory path
- Note: This also calls curl_global_init().
Expand Down
40 changes: 2 additions & 38 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ This module contains various helper namespaces that making working with C++ a bi
## Table of Contents
- [CodeHelpers](#codehelpers)
- [StringHelpers](#stringhelpers)
- [WebHelpers](#webhelpers)

## CodeHelpers
Description: Helper functions for working with C++
Expand All @@ -14,7 +13,7 @@ Interface: [codehelpers.h](/include/helpers/codehelpers.h)

Type: `namespace`

Path: `Nickvision::CodeHelpers`
Path: `Nickvision::Helpers::CodeHelpers`

### Functions
- ```cpp
Expand All @@ -30,7 +29,7 @@ Interface: [stringhelpers.h](/include/helpers/stringhelpers.h)
Type: `namespace`
Path: `Nickvision::StringHelpers`
Path: `Nickvision::Helpers::StringHelpers`
### Functions
- ```cpp
Expand Down Expand Up @@ -120,38 +119,3 @@ Path: `Nickvision::StringHelpers`
- Accepts: A non-wide string parameter, s.
- Returns: s as a wide string.
- Ex: `StringHelpers::wstr("abc")` will return `L"abc"`.

## WebHelpers
Description: Helper functions for working with websites

Interface: [webhelpers.h](/include/helpers/webhelpers.h)

Type: `namespace`

Path: `Nickvision::WebHelpers`

### Functions
- ```cpp
bool downloadFile(const std::string& url, const std::filesystem::path& path, const CurlProgressFunction& progress = {}, bool overwrite = true)
```
- Accepts: A url string, url, a path of disk, path, a CurlProgressFunction, progress, and a overwrite boolean, overwrite.
- Returns: `true` if the file provided by url was successfully downloaded to path.
- Returns: `false` if the download failed.
- Note: If progress points to a valid CurlProgressFunction, progress will be called as the file is downloaded to provide the caller with download progress.
- Note: If a file at path exists and overwrite is true, the file will be overwritten, else downloadFile will fail and return false.
- Note: `CurlProgressFunction` is a `std::function` with the signature `int func(curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)`
- Ex: `WebHelpers::downloadFile(https://raw.githubusercontent.com/nlogozzo/nlogozzo/main/README.md, "readme.md")` will return `true` and download the file to path `./readme.md`.
- ```cpp
std::string fetchJsonString(const std::string& url)
```
- Accepts: A url string, url.
- Returns: The json string hosted at url.
- Note: If this function fails, an empty string will be returned.
- Ex: `WebHelpers::fetchJsonString("https://api.github.com/repos/nickvisionapps/denaro/tags")` will return [this json string](https://api.github.com/repos/nickvisionapps/denaro/tags).
- ```cpp
bool isValidWebsite(const std::string& url)
```
- Accepts: A url string, url.
- Returns: `true` if url points to a valid, live website.
- Returns: `false` if url does not point to a valid website.
- Ex: `WebHelpers::isValidWebsite("https://www.sdfhjsdkfhsjdf.dfh")` will return `false`.
100 changes: 97 additions & 3 deletions docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,67 @@
This module contains objects for managing a system's network state.

## Table of Contents
- [CurlEasy](#curleasy)
- [NetworkMonitor](#networkmonitor)
- [NetworkState](#networkstate)
- [NetworkStateChangedEventArgs](#networkstatechangedeventargs)
- [WebClient](#webclient)

## CurlEasy
Description: An object for making easy curl requests.

Interface: [curleasy.h](/include/network/curleasy.h)

Type: `class`

Path: `Nickvision::Network::CurlEasy`

### Member Variables
- ```
std::string Url: get, set
```
- The url to make requests to.
- ```
bool NoBody: set
```
- Whether or not to include the body in the response.
- ```
std::vector<std::string> Headers: set
```
- The headers to include in the request.
- ```
std::string UserAgent: set
```
- The user agent to use in the request.
- ```
std::basic_ostream<char>* Stream: set
```
- The stream to write the response to.
- ```
std::function<int(curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)> Progress: set
```
- The progress function to use during the request.

### Methods
- ```cpp
WebClient(const std::string& url)
```
- Constructs a CurlEasy.
- Accepts: An optional url to make requests to, url.
- Throws: std::runtime_error if curl fails to initialize.
- ```cpp
~WebClient()
```
- Destructs a CurlEasy.
- ```cpp
void reset(const std::string& url)
```
- Accept: An optional url to make requests to on the reset handle, url.
- Throws: std::runtime_error if curl fails to initialize.
- ```cpp
CURLcode perform()
```
- Returns: The code returned by the performed the curl request.

## NetworkMonitor
Description: An object to monitor the state of the system's network connection.
Expand Down Expand Up @@ -100,15 +158,51 @@ Type: `class`
Path: `Nickvision::Network::NetworkStateChangedEventArgs`
## Member Variables
### Member Variables
- ```
NetworkState State: get
```
- The new state of the network.
## Methods
### Methods
- ```cpp
NetworkStateChangedEventArgs(NetworkState state)
```
- Constructs a NetworkStateChangedEventArgs.
- Accepts: The new NetworkState for the system, state.
- Accepts: The new NetworkState for the system, state.

## WebClient
Description: An object for interacting with the web.

Interface: [webclient.h](/include/network/webclient.h)

Type: `class`

Path: `Nickvision::Network::WebClient`

### Methods
- ```cpp
WebClient()
```
- Constructs a WebClient.
- ```cpp
~WebClient()
```
- Destructs a WebClient.
- ```cpp
bool getWebsiteExists(const std::string& url)
```
- Accepts: The url to check if it exists (i.e. points to a valid domain) or not, url.
- Returns: True if url exists.
- Returns: False if url does not exist.
- ```cpp
std::string fetchJson(const std::string& url)
```
- Accepts: The url to request a json string from, url.
- Returns: The fetched json string
- ```cpp
bool downloadFile(const std::string& url, const std::filesystem::path& path, const std::function<int(curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)>& progress, bool overwrite)
```
- Accepts: The url of the file to download, url, the path to save the downloaded file to on disk, path, an optional function to track download progress, progress, and whether or not to overwrite existing files on disk, overwrite.
- Returns: True if the file was downloaded and saved successfully.
- Returns: False if the file was not downloaded successfully.
1 change: 0 additions & 1 deletion include/app/aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace Nickvision::App
* @param englishShortName The application short name in English
* @param logLevel The application log level
* @throw std::runtime_error Thrown if unable to get the executable directory path
* @throw std::runtime_error Thrown if libcurl fails to initialize
* @throw std::runtime_error Thrown if the gettext system fails to initialize
* @return True if initialized, else false
*/
Expand Down
2 changes: 1 addition & 1 deletion include/helpers/codehelpers.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CODEHELPERS_H
#define CODEHELPERS_H

namespace Nickvision::CodeHelpers
namespace Nickvision::Helpers::CodeHelpers
{
template<typename T>
const T& unmove(T&& t)
Expand Down
2 changes: 1 addition & 1 deletion include/helpers/stringhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <type_traits>
#include <vector>

namespace Nickvision::StringHelpers
namespace Nickvision::Helpers::StringHelpers
{
template<typename T>
concept StringImplicitlyConstructible = std::is_constructible_v<T, std::string> && std::is_convertible_v<std::string, T>;
Expand Down
40 changes: 0 additions & 40 deletions include/helpers/webhelpers.h

This file was deleted.

Loading

0 comments on commit 7fcc304

Please sign in to comment.