Class: KHL::HTTP::Base
- Inherits:
-
Object
- Object
- KHL::HTTP::Base
- Defined in:
- lib/khl/http/base.rb
Overview
接口基类
Direct Known Subclasses
Asset, Badge, Blacklist, Channel, ChannelRole, ChannelUser, DirectMessage, Game, Gateway, Guild, GuildEmoji, GuildMute, GuildRole, Intimacy, Invite, Message, User, UserChat
Constant Summary collapse
- BASE_URL =
"https://www.kaiheila.cn/api"
- API_VERSION =
"v3"
- END_POINT =
"#{BASE_URL}/#{API_VERSION}"
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#get(params = {}) ⇒ Object
GET 请求.
-
#initialize(config) ⇒ Base
constructor
A new instance of Base.
-
#post(params = {}) ⇒ Object
POST 请求.
-
#post_file(params = {}) ⇒ Object
POST 文件请求.
Constructor Details
#initialize(config) ⇒ Base
Returns a new instance of Base.
24 25 26 27 28 29 30 |
# File 'lib/khl/http/base.rb', line 24 def initialize(config) raise ArgumentError, "missing token" unless config[:token] config[:token_type] ||= "Bot" config[:language] ||= "zh-cn" @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
18 19 20 |
# File 'lib/khl/http/base.rb', line 18 def config @config end |
Instance Method Details
#get(params = {}) ⇒ Object
GET 请求
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/khl/http/base.rb', line 33 def get(params = {}) action = params.key?(:action) ? params.delete(:action) : action_name # 可在 params 中手动配置 action uri = URI("#{END_POINT}/#{resource_name}/#{action}") params = filter_params(params) uri.query = URI.encode_www_form(params) request = Net::HTTP::Get.new(uri) request["Authorization"] = "#{config[:token_type]} #{config[:token]}" raw_response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end Response.new(raw_response) end |
#post(params = {}) ⇒ Object
POST 请求
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/khl/http/base.rb', line 48 def post(params = {}) action = params.key?(:action) ? params.delete(:action) : action_name uri = URI("#{END_POINT}/#{resource_name}/#{action}") request = Net::HTTP::Post.new(uri) request["Authorization"] = "#{config[:token_type]} #{config[:token]}" request["Content-type"] = "application/json" params = filter_params(params) request.body = JSON.generate(params) raw_response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end Response.new(raw_response) end |
#post_file(params = {}) ⇒ Object
POST 文件请求
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/khl/http/base.rb', line 64 def post_file(params = {}) action = params.key?(:action) ? params.delete(:action) : action_name uri = URI("#{END_POINT}/#{resource_name}/#{action}") request = Net::HTTP::Post.new(uri) request["Authorization"] = "#{config[:token_type]} #{config[:token]}" request["Content-type"] = params.delete(:content_type) || "form-data" # 可在 params 中手动配置 content_type params = filter_params(params) request.set_form(params, "multipart/form-data") raw_response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end Response.new(raw_response) end |