class Jess::HttpClient

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.

Constants

BadCredentials

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

ConnectionError

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

NotFound

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

ServerError

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

Attributes

logger[R]
net_http_options[R]
password[R]
url[R]
username[R]

Public Class Methods

new(url, username:, password:, logger: default_logger, net_http_options: nil) click to toggle source
# 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

Public Instance Methods

get(path, accept: "application/json") click to toggle source

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.

# 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
inspect() click to toggle source
# File lib/jess/http_client.rb, line 50
def inspect
  url = resource_uri.to_s.sub(%r{://}, "://#{username}@")
  "Jess::HttpClient<#{url}>"
end
resource_uri() click to toggle source

The canonical JSSResource URI used to issue requests.

# 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

Private Instance Methods

apply_net_http_options(http) click to toggle source
# File lib/jess/http_client.rb, line 74
def apply_net_http_options(http)
  net_http_options.each do |attr, value|
    http.public_send("#{attr}=", value)
  end
end
default_logger() click to toggle source
# File lib/jess/http_client.rb, line 59
def default_logger
  defined?(Rails) ? Rails.logger : Logger.new($stderr)
end
http() click to toggle source
# File lib/jess/http_client.rb, line 63
def http
  @http ||= begin
    http = Net::HTTP.new(resource_uri.host, resource_uri.port)
    http.read_timeout = 15
    http.open_timeout = 15
    http.use_ssl = true if resource_uri.scheme == "https"
    apply_net_http_options(http)
    LoggingDecorator.new(logger, ErrorDecorator.new(http))
  end
end