Python 中的 CURL / Ruby 替代方案从 API 获取数据
我想使用 Python 从 API 获取数据。 API 文档提供了 CURL 和 Ruby 中的示例。如果您能发布有关如何使用 Python 执行以下操作的代码片段,我将非常高兴。
获取身份验证令牌:
Curl 示例:
curl -X POST -d "{\"username\" : \"[email protected]\", \"password\":\"sample\"}" http://api.sample.com/authenticate
Ruby 示例:
require 'rubygems'
require 'rest_client'
require 'json'
class AuthorizationClient
attr_accessor :base_url
def initialize(base_url)
@base_url = base_url
end
def authenticate(username,password)
login_data = { 'username' => username, 'password' => password}.to_json
begin
JSON.parse(RestClient.post "#{@base_url}/authenticate", login_data, :content_type => :json, :accept => :json)['output']
rescue Exception => e
JSON.pretty_generate JSON.parse e.http_body
end
end
end
client = AuthorizationClient.new('http://api.sample.com/authenticate')
puts client.authenticate('[email protected]','sample')
身份验证后,获取数据:
CURL 示例:
curl http://api.sample.com/data/day/2011-02-10/authToken/80afa08-1254-46ee-9545-afasfa4565
还有 Ruby 代码:
require 'rubygems'
require 'rest_client'
require 'json'
class ReportingClient
attr_accessor :auth_token, :base_url
def initialize(base_url)
@base_url = base_url
end
def authenticate(username,password)
login_data = { 'username' => username, 'password' => password}.to_json
response = RestClient.post "#{@base_url}/authenticate", login_data, :content_type => :json, :accept => :json
@auth_token = JSON.parse(response)['output']
end
def get_report(start_date, end_date)
response = RestClient.get "#{@base_url}/data/day/#{day}/authToken/#{auth_token}"
JSON.parse(response)
end
end
client = ReportingClient.new('http://api.sample.com:20960')
client.authenticate('[email protected]','sample')
results = client.get_report('2011-02-10')
puts JSON.pretty_generate(results)
谢谢..
PS:我知道 pycurl。但我不确定我是否真的需要它。我很高兴使用 Python 本机库。 Pycurl 可能无法满足我的需求。
我是 Python 新手,在阅读“urllib2”文档并尝试示例后找不到正确的解决方案。
I want to get data from an API using Python. The API documentation give examples in CURL and Ruby. I would be very happy if you can post code snippets on how to do the following things with Python.
To get authentication token:
Curl example:
curl -X POST -d "{\"username\" : \"[email protected]\", \"password\":\"sample\"}" http://api.sample.com/authenticate
Ruby example:
require 'rubygems'
require 'rest_client'
require 'json'
class AuthorizationClient
attr_accessor :base_url
def initialize(base_url)
@base_url = base_url
end
def authenticate(username,password)
login_data = { 'username' => username, 'password' => password}.to_json
begin
JSON.parse(RestClient.post "#{@base_url}/authenticate", login_data, :content_type => :json, :accept => :json)['output']
rescue Exception => e
JSON.pretty_generate JSON.parse e.http_body
end
end
end
client = AuthorizationClient.new('http://api.sample.com/authenticate')
puts client.authenticate('[email protected]','sample')
After authentication, to get data:
CURL example:
curl http://api.sample.com/data/day/2011-02-10/authToken/80afa08-1254-46ee-9545-afasfa4565
And Ruby code:
require 'rubygems'
require 'rest_client'
require 'json'
class ReportingClient
attr_accessor :auth_token, :base_url
def initialize(base_url)
@base_url = base_url
end
def authenticate(username,password)
login_data = { 'username' => username, 'password' => password}.to_json
response = RestClient.post "#{@base_url}/authenticate", login_data, :content_type => :json, :accept => :json
@auth_token = JSON.parse(response)['output']
end
def get_report(start_date, end_date)
response = RestClient.get "#{@base_url}/data/day/#{day}/authToken/#{auth_token}"
JSON.parse(response)
end
end
client = ReportingClient.new('http://api.sample.com:20960')
client.authenticate('[email protected]','sample')
results = client.get_report('2011-02-10')
puts JSON.pretty_generate(results)
Thank you..
PS: I am aware of pycurl. But I am not sure if I really need it. I am happy with using Python native libraries. Pycurl might be over-kill for my needs.
I am new to Python and I couldn't find the right solution after reading 'urllib2' documentation and trying examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个端口,但看起来您正在发送 POST,然后获取授权令牌,然后使用它发送 GET 请求。
这是基于我的理解。
This is not a port but it looks like you are sending a POST and then obtaining the authorization token and then sending a GET request using this.
This is based on what I understood.
阅读 urllib2 文档后,您到底不明白哪一部分?
我不了解我的 Ruby,但从外观上看,它只涉及以 json 格式发送 GET 请求和 POST 请求并解析响应。这对于 simplejson 和 urllib2 来说非常简单。
Which part exactly did you not understand after reading the urllib2 documentation?
I do not know my Ruby, but from the looks of it just involves sending GET request and POST requests in json format and parsing the responses. This is quite trivial with simplejson and urllib2.