Python 中的 CURL / Ruby 替代方案从 API 获取数据

发布于 2024-10-18 01:52:09 字数 2487 浏览 4 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-10-25 01:52:09

这不是一个端口,但看起来您正在发送 POST,然后获取授权令牌,然后使用它发送 GET 请求。
这是基于我的理解。

import urllib
import urllib2
import simplejson
import datetime

authURL = "http://api.sample.com/authenticate"
values = {"username" : "[email protected]",
          "password" : "sample"}

data = urllib.urlencode(values)

req = urllib2.Request(authURL, data)
response = urllib2.urlopen(req)

authToken = simplejson.load(response)["output"]

day = str(datetime.date.today())
dataURL = "http://api.sample.com/data/day/" + day + "/authToken/" + authToken

print simplejson.load(urllib2.urlopen(dataURL))

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.

import urllib
import urllib2
import simplejson
import datetime

authURL = "http://api.sample.com/authenticate"
values = {"username" : "[email protected]",
          "password" : "sample"}

data = urllib.urlencode(values)

req = urllib2.Request(authURL, data)
response = urllib2.urlopen(req)

authToken = simplejson.load(response)["output"]

day = str(datetime.date.today())
dataURL = "http://api.sample.com/data/day/" + day + "/authToken/" + authToken

print simplejson.load(urllib2.urlopen(dataURL))
余厌 2024-10-25 01:52:09

阅读 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文