gpt4 book ai didi

ruby - API 集成错误 HTTParty

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:59 25 4
gpt4 key购买 nike

我正在学习如何使用 HTTParty 和 API,但我的代码有问题。

Users/admin/.rbenv/versions/2.0.0-p481/lib/ruby/2.0.0/uri/generic.rb:214:in `initialize': the scheme http does not accept registry part: :80 (or bad hostname?) 

我试过使用 debug_output STDOUT 作为我的方法的参数,并在包含 HTTParty 之后获得线索但没有成功。什么都不显示:

     require 'httparty'

class LolObserver
include HTTParty
default_timeout(1) #timeout after 1 second

attr_reader :api_key, :playerid
attr_accessor :region
def initialize(region,playerid,apikey)
@region = region_server(region)
@playerid = playerid
@api_key = apikey
end

def region_server(region)
case region
when "euw"
self.class.base_uri "https://euw.api.pvp.net"
self.region = "EUW1"
when "na"
self.class.base_uri "https://na.api.pvp.net"
self.region = "NA1"
end
end

def handle_timeouts
begin
yield
#Timeout::Error, is raised if a chunk of the response cannot be read within the read_timeout.
#Timeout::Error, is raised if a connection cannot be created within the open_timeout.
rescue Net::OpenTimeout, Net::ReadTimeout
#todo
end
end

def base_path
"/observer-mode/rest/consumer/getSpectatorGameInfo"
end

def current_game_info
handle_timeouts do
url = "#{ base_path }/#{region}/#{playerid}?api_key=#{api_key}"
puts '------------------------------'
puts url
HTTParty.get(url,:debug_output => $stdout)
end
end

end

我验证了我的 URL,这没问题,所以我不知道问题出在哪里。

我用静态 base_uri 进行了测试,它没有改变任何东西。

奇怪的是当我这样做的时候:

 HTTParty.get("https://euw.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/EUW1/randomid?api_key=myapikey")

一切正常,我正在收到回复。

最佳答案

HTTPParty 似乎不喜欢您设置 base_uri 的方式。

除非您需要它,只需添加另一个名为 domainattr_reader 即可。

require 'httparty'

class LolObserver
include HTTParty
default_timeout(1) #timeout after 1 second

attr_reader :api_key, :playerid, :domain
attr_accessor :region
def initialize(region,playerid,apikey)
@region = region_server(region)
@playerid = playerid
@api_key = apikey
end

def region_server(region)
case region
when "euw"
@domain = "https://euw.api.pvp.net"
self.region = "EUW1"
when "na"
@domain = "https://na.api.pvp.net"
self.region = "NA1"
end
end

def handle_timeouts
begin
yield
#Timeout::Error, is raised if a chunk of the response cannot be read within the read_timeout.
#Timeout::Error, is raised if a connection cannot be created within the open_timeout.
rescue Net::OpenTimeout, Net::ReadTimeout
#todo
end
end

def base_path
"/observer-mode/rest/consumer/getSpectatorGameInfo"
end

def current_game_info
handle_timeouts do
url = "#{domain}/#{ base_path }/#{region}/#{playerid}?api_key=#{api_key}"
puts '------------------------------'
puts url
HTTParty.get(url,:debug_output => $stdout)
end
end

end

关于ruby - API 集成错误 HTTParty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com