https请求 - C ++的Python代码

时间:2016-07-13 04:21:44

标签: python c++ api curl

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

/*This must be in JS not JQUERY, its in a web worker*/
function removeErroneousData(resp) {
    var removedDivResp = "";

    /*pseudo code for removing div*/ 
    if (resp.contains(div with id disclosures){
       removedDivResp = resp.removeData(div, 'disclosures');
    }

    return removedDivResp;
}

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

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)

1 个答案:

答案 0 :(得分:0)

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

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