Class: Jess::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/jess/http_client.rb,
lib/jess/http_client/error.rb,
lib/jess/http_client/error_decorator.rb,
lib/jess/http_client/logging_decorator.rb

Overview

Provides low-level access to making authenticated GET requests to the JSS API, with basic logging and error handling. You will normally not need to use this class directly, unless you need to make HTTP requests that haven’t been wrapped by the higher level ‘Jess::Connection` API.

Defined Under Namespace

Classes: Error, ErrorDecorator, LoggingDecorator

Constant Summary collapse

ConnectionError =

Raised when Jess::HttpClient fails to open an HTTP connection.

Class.new(Error)
ServerError =

Raised when Jess::HttpClient receives a 500 error from the server.

Class.new(Error)
NotFound =

Raised when Jess::HttpClient receives a 404 error from the server.

Class.new(Error)
BadCredentials =

Raised when Jess::HttpClient receives a 401 error from the server, which happens when the username and/or password are incorrect.

Class.new(Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username:, password:, logger: default_logger, net_http_options: nil) ⇒ HttpClient

Returns a new instance of HttpClient.



21
22
23
24
25
26
27
28
# File 'lib/jess/http_client.rb', line 21

def initialize(url, username:, password:,
               logger: default_logger, net_http_options: nil)
  @url = url
  @username = username
  @password = password
  @net_http_options = net_http_options || {}
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/jess/http_client.rb', line 19

def logger
  @logger
end

#net_http_optionsObject (readonly)

Returns the value of attribute net_http_options.



19
20
21
# File 'lib/jess/http_client.rb', line 19

def net_http_options
  @net_http_options
end

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/jess/http_client.rb', line 19

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



19
20
21
# File 'lib/jess/http_client.rb', line 19

def username
  @username
end

Instance Method Details

#get(path, accept: "application/json") ⇒ Object

Makes a GET request for the given path. The result is the raw, unparsed body of the HTTP response. The path is resolved relative to the ‘resource_uri` and should not start with a slash.



33
34
35
36
37
38
39
# File 'lib/jess/http_client.rb', line 33

def get(path, accept: "application/json")
  req = Net::HTTP::Get.new(URI.join(resource_uri, path))
  req.basic_auth(username, password)
  req["Accept"] = accept
  response = http.request(req)
  response.body.to_s
end

#inspectObject



50
51
52
53
# File 'lib/jess/http_client.rb', line 50

def inspect
  url = resource_uri.to_s.sub(%r{://}, "://#{username}@")
  "Jess::HttpClient<#{url}>"
end

#resource_uriObject

The canonical JSSResource URI used to issue requests.



42
43
44
45
46
47
48
# File 'lib/jess/http_client.rb', line 42

def resource_uri
  @resource_uri ||= begin
    root_url = url.to_s.sub(%r{JSSResource/*$}, "")
    root_url << "/" unless root_url.end_with?("/")
    URI.join(root_url, "JSSResource/")
  end
end