class BiliHttp::HttpClient

bilibili http client

Attributes

api_http[RW]
login_http[RW]
manga_http[RW]

Public Class Methods

new() click to toggle source
# File lib/bilibili_console/http/http.rb, line 22
def initialize
  BiliHttp.headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64',
    'Referer': 'https://www.bilibili.com'
  }
end

Public Instance Methods

get(http, url) click to toggle source
# File lib/bilibili_console/http/http.rb, line 34
def get(http, url)
  request = {
    headers: BiliHttp.headers,
    path: URI(url).request_uri
  }
  http.get(request).data
end
get_json(http, url) click to toggle source

get method

# File lib/bilibili_console/http/http.rb, line 30
def get_json(http, url)
  json_data(get(http, url))
end
json_data(data) click to toggle source
# File lib/bilibili_console/http/http.rb, line 66
def json_data(data)
  body = BiliHttp::ResponseBody.new(data.json)
  if body.data.nil?
    body
  else
    body.data
  end
end
post_form_json(http, url, params) click to toggle source

post method with form data

# File lib/bilibili_console/http/http.rb, line 43
def post_form_json(http, url, params)
  custom_headers = BiliHttp.headers.clone
  custom_headers['Content-Type'] = 'application/x-www-form-urlencoded'
  request = {
    headers: custom_headers,
    path: URI(url).request_uri,
    data: params
  }
  json_data(http.post(request).data)
end
post_json(http, url, headers, req_body) click to toggle source

post method with json body

# File lib/bilibili_console/http/http.rb, line 55
def post_json(http, url, headers, req_body)
  headers = BiliHttp.headers.clone if headers.nil? || headers.empty?
  headers['Content-Type'] = 'application/json' unless req_body.nil? || req_body.empty?
  request = {
    headers: headers,
    path: URI(url).request_uri
  }
  request.merge!({ data: req_body }) unless req_body.nil? || req_body.empty?
  json_data(http.post(request).data)
end