Skip to content

Commit 369d03a

Browse files
committed
Add the ability to send metrics to prometheus via http-client-lite header only library
1 parent 4f220b1 commit 369d03a

File tree

14 files changed

+874
-3
lines changed

14 files changed

+874
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2021... by Maxim Gusev
2+
#
3+
# https://github.com/John-Jasper-Doe/http-client-lite
4+
#
5+
# Distributed under the MIT License.
6+
# (See accompanying file LICENSE or copy at https://mit-license.org/)
7+
8+
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
9+
project(http-client-lite)
10+
11+
## Set output binary
12+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
13+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
14+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
15+
16+
## Set property
17+
set(CMAKE_CXX_STANDARD 11)
18+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
19+
add_compile_options(-Wall -Werror -Wextra -Wpedantic -g -O0)
20+
21+
22+
option(HTTP_CLIENT_LITE_OPT_BUILD_EXAMPLES "Build examples" OFF )
23+
24+
if(HTTP_CLIENT_LITE_OPT_BUILD_EXAMPLES)
25+
add_subdirectory(examples)
26+
endif()
27+
28+
# Interface library:
29+
add_library(${PROJECT_NAME} INTERFACE)
30+
target_sources(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/jdl/httpclientlite.h)
31+
add_custom_target(${PROJECT_NAME}.hdr SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/include/jdl/httpclientlite.h)
32+
target_include_directories(
33+
${PROJECT_NAME}
34+
INTERFACE
35+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
36+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

3rdpatry/http-client-lite/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License
2+
3+
Copyright (c) 2016 Christian C. Sachs
4+
Copyright (c) 2021 Maxim Gusev
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# HTTP Client lite: C++ Cross-platform library only from single-file header-only
2+
3+
This is a lite, C++ cross-platform header-only client library for http request based
4+
on [csachs/picohttpclient](https://github.com/csachs/picohttpclient) project.
5+
6+
A Lightweight HTTP 1.1 client where to quickly do very simple HTTP requests,
7+
without adding larger dependencies to a project.
8+
9+
10+
## License
11+
12+
http client lite is distributed under the [MIT License](https://github.com/john-jasper-doe/http-client-lite/blob/master/LICENSE).
13+
14+
15+
## Example usage
16+
17+
To see how this can be used see the examples folders.
18+
19+
20+
**Example:**
21+
```C++
22+
#include <jdl/httpclientlite.hpp>
23+
...
24+
using namespace jdl;
25+
...
26+
HTTPResponse response = HTTPClient::request(HTTPClient::GET, URI("http://example.com"));
27+
cout << response.body << endl;
28+
...
29+
```
30+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
2+
project(http-client-lite-examples)
3+
4+
add_executable(${PROJECT_NAME}_simple_request simple_request.cpp)
5+
target_link_libraries(${PROJECT_NAME}_simple_request PRIVATE http_client_lite)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* example for httpclientlite.hxx
3+
*/
4+
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
9+
#include <jdl/httpclientlite.h>
10+
11+
12+
using namespace jdl;
13+
14+
15+
int main(int argc, char *argv[]) {
16+
if (argc == 1) {
17+
std::cout << "Use " << argv[0] << " http://example.org" << std::endl;
18+
return EXIT_SUCCESS;
19+
}
20+
21+
HTTPResponse response = HTTPClient::request(HTTPClient::POST, URI(argv[1]));
22+
23+
if (!response.success) {
24+
std::cout << "Request failed!" << std::endl;
25+
return EXIT_FAILURE;
26+
}
27+
28+
std::cout << "Request success" << endl;
29+
30+
std::cout << "Server protocol: " << response.protocol << std::endl;
31+
std::cout << "Response code: " << response.response << std::endl;
32+
std::cout << "Response string: " << response.responseString << std::endl;
33+
34+
std::cout << "Headers:" << std::endl;
35+
36+
for (stringMap::iterator it = response.header.begin(); it != response.header.end(); ++it) {
37+
std::cout << "\t" << it->first << "=" << it->second << std::endl;
38+
}
39+
40+
std::cout << response.body << std::endl;
41+
42+
return EXIT_SUCCESS;
43+
}

0 commit comments

Comments
 (0)