module Jamf::Connection::JamfProAPI
This Module defines methods used for interacting with the Jamf
Pro API. This includes creating the Faraday connection, sending HTTP requests and handling responses
Public Instance Methods
create the faraday JPAPI connection object
# File lib/jamf/api/connection/jamf_pro_api.rb 38 def create_jp_connection(parse_json: true) 39 Faraday.new(@jp_base_url, ssl: ssl_options) do |cnx| 40 # use a proc for the token value, so its looked up on every request 41 # meaning we don't have to validate that the token is still valid before every request 42 # because the Token instance will (usually) refresh it automatically. 43 cnx.request :authorization, 'Bearer', -> { @token.token } 44 45 cnx.options[:timeout] = @timeout 46 cnx.options[:open_timeout] = @open_timeout 47 48 if parse_json 49 cnx.request :json 50 cnx.response :json, parser_options: { symbolize_names: true } 51 end 52 53 cnx.adapter :net_http 54 end 55 end
Delete an existing Jamf
Pro API resource
@param rsrc the API resource being deleted, the URL part after ‘api/’
@return [String] the response from the server.
# File lib/jamf/api/connection/jamf_pro_api.rb 156 def jp_delete(rsrc) 157 validate_connected 158 rsrc = rsrc.delete_prefix Jamf::Connection::SLASH 159 resp = @jp_cnx.delete rsrc 160 @last_http_response = resp 161 return resp.body if resp.success? 162 163 raise Jamf::Connection::JamfProAPIError, resp 164 end
GET a rsrc without doing any JSON parsing, using a temporary Faraday connection object
# File lib/jamf/api/connection/jamf_pro_api.rb 171 def jp_download(rsrc) 172 rsrc = rsrc.delete_prefix Jamf::Connection::SLASH 173 temp_cnx = create_jp_connection(parse_json: false) 174 resp = temp_cnx.get rsrc 175 @last_http_response = resp 176 return resp.body if resp.success? 177 178 raise Jamf::Connection::JamfProAPIError, resp 179 end
@return [Hash] the result of the get
# File lib/jamf/api/connection/jamf_pro_api.rb 62 def jp_get(rsrc) 63 validate_connected 64 rsrc = rsrc.delete_prefix Jamf::Connection::SLASH 65 resp = @jp_cnx.get(rsrc) do |req| 66 # Modify the request here if needed. 67 # puts "JPAPI Cookie is: #{req.headers['Cookie']}" 68 end 69 @last_http_response = resp 70 71 return resp.body if resp.success? 72 73 raise Jamf::Connection::JamfProAPIError, resp 74 end
Update an existing Jamf
Pro API resource
@param rsrc the API resource being changed, the URL part after ‘api/’
@param data the json specifying the changes.
@return [String] the response from the server.
# File lib/jamf/api/connection/jamf_pro_api.rb 133 def jp_patch(rsrc, data) 134 validate_connected 135 rsrc = rsrc.delete_prefix Jamf::Connection::SLASH 136 resp = @jp_cnx.patch(rsrc) do |req| 137 # Patch requests must use this content type! 138 req.headers['Content-Type'] = 'application/merge-patch+json' 139 req.body = data 140 end 141 @last_http_response = resp 142 return resp.body if resp.success? 143 144 raise Jamf::Connection::JamfProAPIError, resp 145 end
Create a JPAPI resource via POST
@param rsrc the resource to POST
(the part of the API url after the '/api/' )
@param data the JSON data to POST
@return [String] the response body
# File lib/jamf/api/connection/jamf_pro_api.rb 87 def jp_post(rsrc, data) 88 validate_connected 89 rsrc = rsrc.delete_prefix Jamf::Connection::SLASH 90 resp = @jp_cnx.post(rsrc) do |req| 91 req.body = data 92 end 93 @last_http_response = resp 94 return resp.body if resp.success? 95 96 raise Jamf::Connection::JamfProAPIError, resp 97 end
Replace an existing Jamf
Pro API resource
@param rsrc the API resource being changed, the URL part after ‘api/’
@param data the json specifying the changes.
@return [String] the response from the server.
# File lib/jamf/api/connection/jamf_pro_api.rb 110 def jp_put(rsrc, data) 111 validate_connected 112 rsrc = rsrc.delete_prefix Jamf::Connection::SLASH 113 resp = @jp_cnx.put(rsrc) do |req| 114 req.body = data 115 end 116 @last_http_response = resp 117 return resp.body if resp.success? 118 119 raise Jamf::Connection::JamfProAPIError, resp 120 end