Git Product home page Git Product logo

Comments (11)

ChuanqiXu9 avatar ChuanqiXu9 commented on June 14, 2024

具体的例子可以参考 https://github.com/alibaba/yalantinglibs

要 reduced 后的 example 的话,看看 @qicosmos@poor-circle 有没有时间吧

from async_simple.

qicosmos avatar qicosmos commented on June 14, 2024

@bethebest0622 用yalantinglibs的coro_http_client 可以比较容易的实现你的需求

#include "ylt/coro_http/coro_http_client.hpp"
#include "ylt/struct_json/json_reader.h"

//定义json对应的结构体
struct trading_decimal_t {
  std::string_view name;
  std::string_view min_amount;
  std::string_view maker_fee_rate;
  std::string_view taker_fee_rate;
  std::string_view pricing_name;
  int pricing_decimal;
  std::string_view trading_name;
  int trading_decimal;
};
REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate, pricing_name, pricing_decimal, trading_name, trading_decimal);

struct market_result{
  int code;
  trading_decimal_t data;
  std::string_view message;
};
REFLECTION(market_result, code, data, message);

// http请求
async_simple::coro::Lazy<void> test_async_ssl_client(
    coro_http::coro_http_client &client) {
  client.init_ssl("api.coinex.com"); 
  std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT";
  auto data = co_await client.async_get(uri);
  if(data.status==200){
      market_result result;
      struct_json::from_json(result, data.resp_body); // 解析json
  }
}

int main() {
   coro_http::coro_http_client client{};
   async_simple::coro::syncAwait(test_async_ssl_client(client));
}

from async_simple.

qicosmos avatar qicosmos commented on June 14, 2024

@bethebest0622 如果不是ssl的,则无需调用init ssl相关的代码。
test_async_ssl_client() 请求http 服务 整个过程是异步的。

from async_simple.

bethebest0622 avatar bethebest0622 commented on June 14, 2024

@qicosmos
谢谢回复,您提供的例子,init_ssl显示找不到函数定义
去掉init_ssl的话,返回404

是不是我需要安装指定版本的库

from async_simple.

poor-circle avatar poor-circle commented on June 14, 2024

@qicosmos 谢谢回复,您提供的例子,init_ssl显示找不到函数定义 去掉init_ssl的话,返回404

是不是我需要安装指定版本的库

你没安装openssl吧,换个http的网站看看

from async_simple.

poor-circle avatar poor-circle commented on June 14, 2024

@qicosmos 谢谢回复,您提供的例子,init_ssl显示找不到函数定义 去掉init_ssl的话,返回404

是不是我需要安装指定版本的库

还有记得#define YLT_ENABLE_SSL

from async_simple.

bethebest0622 avatar bethebest0622 commented on June 14, 2024

@poor-circle
#define #define YLT_ENABLE_SSL
这个放在哪个文件呢 放在cinatra 自动下载的时候好像会被覆盖

from async_simple.

poor-circle avatar poor-circle commented on June 14, 2024
#define YLT_ENABLE_SSL
#include "ylt/coro_http/coro_http_client.hpp"
#include "ylt/struct_json/json_reader.h"

//定义json对应的结构体
struct trading_decimal_t {
  std::string_view name;
  std::string_view min_amount;
  std::string_view maker_fee_rate;
  std::string_view taker_fee_rate;
  std::string_view pricing_name;
  int pricing_decimal;
  std::string_view trading_name;
  int trading_decimal;
};
REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate, pricing_name, pricing_decimal, trading_name, trading_decimal);

struct market_result{
  int code;
  trading_decimal_t data;
  std::string_view message;
};
REFLECTION(market_result, code, data, message);

// http请求
async_simple::coro::Lazy<void> test_async_ssl_client(
    coro_http::coro_http_client &client) {
  client.init_ssl("api.coinex.com"); 
  std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT";
  auto data = co_await client.async_get(uri);
  if(data.status==200){
      market_result result;
      struct_json::from_json(result, data.resp_body); // 解析json
  }
}

int main() {
   coro_http::coro_http_client client{};
   async_simple::coro::syncAwait(test_async_ssl_client(client));
}

from async_simple.

bethebest0622 avatar bethebest0622 commented on June 14, 2024

@poor-circle

感谢您,不过可否看下下面这个例子,我的意图是 主线程连续调用http get请求,希望前面的get网络延迟的时候,不要阻塞后面的get,并可以正确处理所有返回的json,希望能比阻塞调用获得更快的响应

#define YLT_ENABLE_SSL
#include "curl_util/curl_util.h"
#include "util/time_util.h"
#include "ylt/coro_http/coro_http_client.hpp"
#include "ylt/struct_json/json_reader.h"

//定义json对应的结构体
struct trading_decimal_t {
  std::string_view name;
  std::string_view min_amount;
  std::string_view maker_fee_rate;
  std::string_view taker_fee_rate;
  std::string_view pricing_name;
  int pricing_decimal;
  std::string_view trading_name;
  int trading_decimal;
};
REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate,
           pricing_name, pricing_decimal, trading_name, trading_decimal);

struct market_result {
  int code;
  trading_decimal_t data;
  std::string_view message;
};
REFLECTION(market_result, code, data, message);

// http请求
async_simple::coro::Lazy<void>
test_async_ssl_client(coro_http::coro_http_client &client, int i) {
  util::time_util::Timer t; t.StartTimer();
  std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT";
  auto data = co_await client.async_get(uri);
  if (data.status == 200) {
    market_result result;
    struct_json::from_json(result, data.resp_body); // 解析json
    std::cout << result.data.maker_fee_rate << std::endl;
    t.EndTimer(std::to_string(i) + " asd");
  } else {
    std::cout << "failed " << data.resp_body << " " << data.net_err << " "
              << " " << data.status << std::endl;
  }
}

int main() {
  coro_http::coro_http_client client{};
  auto a = client.init_ssl("api.coinex.com");
  std::cout << a << std::endl;
  for (size_t i = 0; i < 10; ++i)
    const auto & asd =test_async_ssl_client(client, i);
  // async_simple::coro::syncAwait(test_async_ssl_client(client, i));
}

from async_simple.

poor-circle avatar poor-circle commented on June 14, 2024

@poor-circle

感谢您,不过可否看下下面这个例子,我的意图是 主线程连续调用http get请求,希望前面的get网络延迟的时候,不要阻塞后面的get,并可以正确处理所有返回的json,希望能比阻塞调用获得更快的响应

#define YLT_ENABLE_SSL
#include "curl_util/curl_util.h"
#include "util/time_util.h"
#include "ylt/coro_http/coro_http_client.hpp"
#include "ylt/struct_json/json_reader.h"

//定义json对应的结构体
struct trading_decimal_t {
  std::string_view name;
  std::string_view min_amount;
  std::string_view maker_fee_rate;
  std::string_view taker_fee_rate;
  std::string_view pricing_name;
  int pricing_decimal;
  std::string_view trading_name;
  int trading_decimal;
};
REFLECTION(trading_decimal_t, name, min_amount, maker_fee_rate, taker_fee_rate,
           pricing_name, pricing_decimal, trading_name, trading_decimal);

struct market_result {
  int code;
  trading_decimal_t data;
  std::string_view message;
};
REFLECTION(market_result, code, data, message);

// http请求
async_simple::coro::Lazy<void>
test_async_ssl_client(coro_http::coro_http_client &client, int i) {
  util::time_util::Timer t; t.StartTimer();
  std::string uri = "https://api.coinex.com/v1/market/detail?market=BTCUSDT";
  auto data = co_await client.async_get(uri);
  if (data.status == 200) {
    market_result result;
    struct_json::from_json(result, data.resp_body); // 解析json
    std::cout << result.data.maker_fee_rate << std::endl;
    t.EndTimer(std::to_string(i) + " asd");
  } else {
    std::cout << "failed " << data.resp_body << " " << data.net_err << " "
              << " " << data.status << std::endl;
  }
}

int main() {
  coro_http::coro_http_client client{};
  auto a = client.init_ssl("api.coinex.com");
  std::cout << a << std::endl;
  for (size_t i = 0; i < 10; ++i)
    const auto & asd =test_async_ssl_client(client, i);
  // async_simple::coro::syncAwait(test_async_ssl_client(client, i));
}

你把这个issue移到yalantinglibs重新开一个吧

from async_simple.

bethebest0622 avatar bethebest0622 commented on June 14, 2024

好的

from async_simple.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.