【问题标题】:https request--Python code to C++https 请求——Python 代码到 C++
【发布时间】:2016-07-13 04:21:43
【问题描述】:

我有一个用 Python 编写的函数,它调用 Robinhood(股票交易经纪人)API 来获取报价数据(以下代码截图中的“get_quote(self, symbol)”函数)。它工作正常。返回了正确的市场数据。

import requests
import urllib

class Robinhood(object):

# All known endpoints as of September 5th, 2015
endpoints = {

    "quotes": "https://api.robinhood.com/quotes/",
    "user": "https://api.robinhood.com/user/",
    "user/additional_info": "https://api.robinhood.com/user/additional_info/",
    "user/basic_info": "https://api.robinhood.com/user/basic_info/",
    "user/employment": "https://api.robinhood.com/user/employment/",
    "user/investment_profile": "https://api.robinhood.com/user/investment_profile/",
    "watchlists": "https://api.robinhood.com/watchlists/"
    }

def get_quote(self, symbol):
    ''' Returns a qoute object for a given symbol including all data returned by Robinhood's API'''
    data = { 'symbols' : symbol }
    res = self.session.get(self.endpoints['quotes'], params=data)
    if res.status_code == 200:
        return res.json()['results']
    else:
        raise Exception("Could not retrieve quote: " + res.text)

我尝试使用 Curl 库在 C++ 中实现此逻辑。但它不起作用。没有编译或运行时错误,但程序返回了一个不可读的字符,而不是股票的市场价格。在我看来,我的 URL 设置不正确,但我不知道如何修复它。有人有想法吗?谢谢!

std::string RobinhoodAPI::GetQuote(std::string ticker)
{
 struct response resStr;
init_string(&resStr);
std::string url = "https://api.robinhood.com/quotes/symbols=AVP/";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resStr);

 resCode = curl_easy_perform(curl);
 std::cout << std::string(resStr.ptr);

 return std::string(resStr.ptr);
}

【问题讨论】:

  • 你能发布一个可运行的例子吗?
  • 我可以。但在这两种情况下,它都需要您的 Robinhood 用户名和密码才能连接到 API。
  • @MK。不可读的符号看起来像数字零,里面有问号。

标签: python c++ api curl


【解决方案1】:

我为 robinhood api 的非官方文档创建了一个开放的 api 规范。有了它,您可以为大多数语言生成 http 客户端。

请访问此处https://github.com/sabareeshkkanan/robinhood 了解规范。请访问此 repo 了解如何使用此规范生成客户端 https://github.com/swagger-api/swagger-codegen

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多