Class: BiliHttp::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bilibili_console/http/http.rb

Overview

bilibili http client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpClient

Returns a new instance of HttpClient.



22
23
24
25
26
27
# 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

Instance Attribute Details

#api_httpObject

Returns the value of attribute api_http.



20
21
22
# File 'lib/bilibili_console/http/http.rb', line 20

def api_http
  @api_http
end

#login_httpObject

Returns the value of attribute login_http.



20
21
22
# File 'lib/bilibili_console/http/http.rb', line 20

def 
  @login_http
end

#manga_httpObject

Returns the value of attribute manga_http.



20
21
22
# File 'lib/bilibili_console/http/http.rb', line 20

def manga_http
  @manga_http
end

Instance Method Details

#get(http, url) ⇒ Object



34
35
36
37
38
39
40
# 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) ⇒ Object

get method



30
31
32
# File 'lib/bilibili_console/http/http.rb', line 30

def get_json(http, url)
  json_data(get(http, url))
end

#json_data(data) ⇒ Object



66
67
68
69
70
71
72
73
# 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) ⇒ Object

post method with form data



43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object

post method with json body



55
56
57
58
59
60
61
62
63
64
# 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