Class: Clash::Web::Http

Inherits:
RubyQt6::Bando::QObject
  • Object
show all
Defined in:
lib/clash-systray/lib/clash/web/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, options) ⇒ Http

Returns a new instance of Http.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/clash-systray/lib/clash/web/http.rb', line 8

def initialize(client, options)
  super($qApp)

  @client = client
  @host = options.fetch(:host)
  @port = options.fetch(:port)

  @on_reply_success = {}
  @manager = QNetworkAccessManager.new
  @manager.finished.connect(self, :_on_reply_finished)
end

Instance Method Details

#get(path, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/clash-systray/lib/clash/web/http.rb', line 20

def get(path, options = {})
  url = QUrl.new("http://#{@host}:#{@port}#{path}")
  set_url_query(url, options[:params])

  request = QNetworkRequest.new
  request.set_url(url)
  request.set_raw_header("Authorization", "Bearer #{@client.secret}")

  reply = @manager.get(request)
  @on_reply_success[reply._qobject_ptr] = options[:on_success]
end

#put(path, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/clash-systray/lib/clash/web/http.rb', line 32

def put(path, options = {})
  url = QUrl.new("http://#{@host}:#{@port}#{path}")

  request = QNetworkRequest.new
  request.set_url(url)
  request.set_raw_header("Authorization", "Bearer #{@client.secret}")

  data = QByteArray.new(options[:json].to_json)
  reply = @manager.put(request, data)
  @on_reply_success[reply._qobject_ptr] = options[:on_success]
end