Class: Wokku::ApiClient
- Inherits:
-
Object
show all
- Defined in:
- lib/wokku/api_client.rb
Defined Under Namespace
Classes: Error, NotAuthenticated, Timeout, Unreachable
Constant Summary
collapse
- OPEN_TIMEOUT =
10
- READ_TIMEOUT =
30
Instance Method Summary
collapse
-
#delete(path, body = nil) ⇒ Object
-
#get(path) ⇒ Object
-
#initialize(url: nil, token: nil, user_agent: "WokkuCLI") ⇒ ApiClient
constructor
A new instance of ApiClient.
-
#patch(path, body = nil) ⇒ Object
-
#post(path, body = nil) ⇒ Object
-
#put(path, body = nil) ⇒ Object
-
#request(method, path, body = nil) ⇒ Object
-
#stream(method, path, &block) ⇒ Object
Constructor Details
#initialize(url: nil, token: nil, user_agent: "WokkuCLI") ⇒ ApiClient
Returns a new instance of ApiClient.
18
19
20
21
22
|
# File 'lib/wokku/api_client.rb', line 18
def initialize(url: nil, token: nil, user_agent: "WokkuCLI")
@url = url
@token = token
@user_agent = user_agent
end
|
Instance Method Details
#delete(path, body = nil) ⇒ Object
28
|
# File 'lib/wokku/api_client.rb', line 28
def delete(path, body = nil) = request(:delete, path, body)
|
#get(path) ⇒ Object
24
|
# File 'lib/wokku/api_client.rb', line 24
def get(path) = request(:get, path)
|
#patch(path, body = nil) ⇒ Object
27
|
# File 'lib/wokku/api_client.rb', line 27
def patch(path, body = nil) = request(:patch, path, body)
|
#post(path, body = nil) ⇒ Object
25
|
# File 'lib/wokku/api_client.rb', line 25
def post(path, body = nil) = request(:post, path, body)
|
#put(path, body = nil) ⇒ Object
26
|
# File 'lib/wokku/api_client.rb', line 26
def put(path, body = nil) = request(:put, path, body)
|
#request(method, path, body = nil) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/wokku/api_client.rb', line 30
def request(method, path, body = nil)
@last_path = path
raise NotAuthenticated, "Not logged in. Run: wokku auth:login" unless token
uri = URI("#{url}#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
http.open_timeout = OPEN_TIMEOUT
http.read_timeout = READ_TIMEOUT
req = build_request(method, uri)
req["Authorization"] = "Bearer #{token}"
req["Content-Type"] = "application/json"
req["User-Agent"] = @user_agent
req.body = body.to_json if body
resp = perform(http, req, uri)
parse_response(resp)
end
|
#stream(method, path, &block) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/wokku/api_client.rb', line 50
def stream(method, path, &block)
@last_path = path
raise NotAuthenticated, "Not logged in. Run: wokku auth:login" unless token
uri = URI("#{url}#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
http.read_timeout = nil
req = build_request(method, uri)
req["Authorization"] = "Bearer #{token}"
req["Accept"] = "text/plain"
req["User-Agent"] = @user_agent
http.request(req) do |resp|
raise Error, "Stream failed: HTTP #{resp.code}" unless resp.is_a?(Net::HTTPSuccess)
resp.read_body { |chunk| block.call(chunk) }
end
rescue Interrupt
end
|