• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

flaviotordini/http: Wrapper around Qt's HTTP APIs

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

flaviotordini/http

开源软件地址:

https://github.com/flaviotordini/http

开源编程语言:

C++ 96.8%

开源软件介绍:

A wrapper for the Qt Network Access API

This is just a wrapper around Qt's QNetworkAccessManager and friends. I use it in my apps at https://flavio.tordini.org . It has a simpler, higher-level API and some functionality not found in Qt:

  • Throttling (as required by many web APIs nowadays)
  • Automatic retries
  • User agent and request header defaults
  • Partial requests
  • Easier POST requests
  • Read timeouts (don't let your requests get stuck forever). (now supported by Qt >= 5.15)
  • Redirection support (now supported by Qt >= 5.6)
  • Disk-based cache implementation similar to Qt's but not strictly a HTTP cache, i.e. it ignores HTTP headers. This is good if want to cache successful requests irrespective of what the origin server says you should do. The cache also fallbacks to stale content when the server returns an error.

Design

This library uses the Decorator design pattern to modularize features and make it easy to add them and use them as needed. The main class is Http, which implements the base features of a HTTP client. More specialized classes are:

The constructor of these classes takes another Http instance for which they will act as a proxy. (See examples below). Following this design you can create your own Http subclass. For example, a different caching mechanism, an event dispatcher, custom request logging, etc.

Build Instructions

In order to build this library you can use either qmake or cmake.

qmake

mkdir build
cd build
qmake ..
make

CMake

mkdir build
cd build
cmake ..
make

Integration

You can use this library as a git submodule. For example, add it to your project inside a lib subdirectory:

git submodule add -b master https://github.com/flaviotordini/http lib/http

Then you can update your git submodules like this:

git submodule update --init --recursive --remote

To integrate the library in your qmake based project just add this to your .pro file:

include(lib/http/http.pri)

qmake builds all object files in the same directory. In order to avoid filename clashes use:

CONFIG += object_parallel_to_source

If you are using CMake you can integrate the library by adding the following lines to your CMakeLists.txt:

add_subdirectory(lib/http)
...
target_link_library(your_super_cool_project <other libraries> http)

or if you have installed http you can find it via:

find_library(http REQUIRED)
...
target_link_library(your_super_cool_project <other libraries> http)

Examples

A basic C++14 example:

#include "http.h"

auto reply = Http::instance().get("https://google.com/");
connect(reply, &HttpReply::finished, this, [](auto &reply) {
    if (reply.isSuccessful()) {
        qDebug() << "Feel the bytes!" << reply.body();
    } else {
        qDebug() << "Something's wrong here" << reply.statusCode() << reply.reasonPhrase();
    }
});

Or using two separate signals for success and failure:

#include "http.h"

auto reply = Http::instance().get("https://google.com/");
connect(reply, &HttpReply::data, this, [](auto &bytes) {
    qDebug() << "Feel the bytes!" << bytes;
});
connect(reply, &HttpReply::error, this, [](auto &msg) {
    qDebug() << "Something's wrong here" << msg;
});

This is a real-world example of building a Http object with more complex features. It throttles requests, uses a custom user agent and caches results:

#include "http.h"
#include "cachedhttp.h"
#include "throttledhttp.h"

Http &myHttp() {
    static Http *http = [] {
        Http *http = new Http;
        http->addRequestHeader("User-Agent", userAgent());

        ThrottledHttp *throttledHttp = new ThrottledHttp(*http);
        throttledHttp->setMilliseconds(1000);

        CachedHttp *cachedHttp = new CachedHttp(*throttledHttp, "mycache");
        cachedHttp->setMaxSeconds(86400 * 30);

        return cachedHttp;
    }();
    return *http;
}

If the full power (and complexity) of QNetworkReply is needed you can always fallback to it:

#include "http.h"

HttpRequest req;
req.url = "https://flavio.tordini.org/";
QNetworkReply *reply = Http::instance().networkReply(req);
// Use QNetworkReply as needed...

Note that features like redirection, retries and read timeouts won't work in this mode.

License

You can use this library under the MIT license and at your own risk. If you do, you're welcome contributing your changes and fixes.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
leev/ngx_http_geoip2_module: Nginx GeoIP2 module发布时间:2022-06-17
下一篇:
twitter/netty-http2: HTTP/2 for Netty发布时间:2022-06-17
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap