项目作者: cddjr

项目描述 :
RxCpp + libcurl + VS2017 实现类似RxMoya库。并基于RxCpp实现类似Swift中的DispatchQueue
高级语言:
项目地址: git://github.com/cddjr/RxCurl.git
创建时间: 2018-06-24T13:23:53Z
项目社区:https://github.com/cddjr/RxCurl

开源协议:

下载


RxCurl

  1. //请求testGet这个api
  2. RxCurl::RxCurlProvider provider;
  3. disposeBag << provider.request(BaiduApi::testGet())
  4. .subscribe([](const std::string& body) {
  5. TRACE("BaiduApi::testGet OnNext: %s\n", body.substr(0, 32).c_str());
  6. }, [](std::exception_ptr ep) {
  7. try { std::rethrow_exception(ep); }
  8. catch (const std::exception& ex) {
  9. TRACE("BaiduApi::testGet OnError: %s\n", ex.what());
  10. }
  11. }, [] {
  12. TRACE("BaiduApi::testGet OnCompleted\n"); }
  13. );

  1. //400毫秒后在background线程中取消网络请求
  2. DispatchQueue.global(DispatchQoS::background).asyncAfterNow(std::chrono::milliseconds(400), [] {
  3. disposeBag.reset();
  4. //然后回到主线程打印log
  5. DispatchQueue.main.async([] {
  6. TRACE("disposeBag.reset!\n");
  7. });
  8. });

  1. //测试API的定义
  2. struct BaiduApi : RxCurl::TargetType {
  3. BaiduApi() {
  4. baseURL = "http://www.baidu.com";
  5. method = RxCurl::Method::get;
  6. }
  7. static auto testGet() {
  8. auto api = BaiduApi();
  9. api.task = std::make_shared<RxCurl::RquestTask::requestPlain>();
  10. return api;
  11. }
  12. static auto testPost(const std::string& id) {
  13. auto api = BaiduApi();
  14. RxCurl::RquestTask::Parameters params = { { "id", id } };
  15. api.task = std::make_shared<RxCurl::RquestTask::requestParameters>(params);
  16. api.path = "index.htm";
  17. api.method = RxCurl::Method::post;
  18. return api;
  19. }
  20. };