Module: Jamf::Connection::JamfProAPI
- Included in:
- Jamf::Connection
- Defined in:
- lib/jamf/api/connection/jamf_pro_api.rb
Overview
This Module defines methods used for interacting with the Jamf Pro API. This includes creating the Faraday connection, sending HTTP requests and handling responses
Instance Method Summary collapse
-
#create_jp_connection(parse_json: true, upload: false) ⇒ Object
create the faraday JPAPI connection object.
-
#jp_delete(rsrc, data = nil) ⇒ String
(also: #delete)
Delete an existing Jamf Pro API resource.
-
#jp_download(rsrc) ⇒ Object
GET a rsrc without doing any JSON parsing, using a temporary Faraday connection object.
-
#jp_get(rsrc, data = nil) ⇒ Hash
(also: #get)
The result of the get.
-
#jp_patch(rsrc, data = nil) ⇒ String
(also: #patch)
Update an existing Jamf Pro API resource.
-
#jp_post(rsrc, data = nil) ⇒ String
(also: #post)
Create a JPAPI resource via POST.
-
#jp_put(rsrc, data = nil) ⇒ String
(also: #put)
Replace an existing Jamf Pro API resource.
-
#jp_upload(rsrc, local_file) ⇒ String
The xml response from the server.
Instance Method Details
#create_jp_connection(parse_json: true, upload: false) ⇒ Object
create the faraday JPAPI connection object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 21 def create_jp_connection(parse_json: true, upload: false) Faraday.new(@jp_base_url, ssl: ) do |cnx| # use a proc for the token value, so its looked up on every request # meaning we don't have to validate that the token is still valid before every request # because the Token instance will (usually) refresh it automatically. cnx.request :authorization, 'Bearer', -> { @token.token } cnx.[:timeout] = @timeout cnx.[:open_timeout] = @open_timeout cnx.request :multipart if upload if parse_json cnx.request :json unless upload cnx.response :json, parser_options: { symbolize_names: true } end cnx.adapter :net_http end end |
#jp_delete(rsrc, data = nil) ⇒ String Also known as: delete
Delete an existing Jamf Pro API resource
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 149 def jp_delete(rsrc, data = nil) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH resp = @jp_cnx.delete(rsrc) do |req| req.body = data if data end @last_http_response = resp return resp.body if resp.success? raise Jamf::Connection::JamfProAPIError, resp end |
#jp_download(rsrc) ⇒ Object
GET a rsrc without doing any JSON parsing, using a temporary Faraday connection object
168 169 170 171 172 173 174 175 176 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 168 def jp_download(rsrc) rsrc = rsrc.delete_prefix Jamf::Connection::SLASH temp_cnx = create_jp_connection(parse_json: false) resp = temp_cnx.get rsrc @last_http_response = resp return resp.body if resp.success? raise Jamf::Connection::JamfProAPIError, resp end |
#jp_get(rsrc, data = nil) ⇒ Hash Also known as: get
Returns the result of the get.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 47 def jp_get(rsrc, data = nil) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH resp = @jp_cnx.get(rsrc) do |req| # Modify the request here if needed. # puts "JPAPI Cookie is: #{req.headers['Cookie']}" req.body = data if data end @last_http_response = resp return resp.body if resp.success? raise Jamf::Connection::JamfProAPIError, resp end |
#jp_patch(rsrc, data = nil) ⇒ String Also known as: patch
Update an existing Jamf Pro API resource
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 124 def jp_patch(rsrc, data = nil) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH resp = @jp_cnx.patch(rsrc) do |req| # Patch requests must use this content type! req.headers['Content-Type'] = 'application/merge-patch+json' req.body = data if data end @last_http_response = resp return resp.body if resp.success? raise Jamf::Connection::JamfProAPIError, resp end |
#jp_post(rsrc, data = nil) ⇒ String Also known as: post
Create a JPAPI resource via POST
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 74 def jp_post(rsrc, data = nil) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH resp = @jp_cnx.post(rsrc) do |req| req.body = data if data end @last_http_response = resp return resp.body if resp.success? raise Jamf::Connection::JamfProAPIError, resp end |
#jp_put(rsrc, data = nil) ⇒ String Also known as: put
Replace an existing Jamf Pro API resource
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 99 def jp_put(rsrc, data = nil) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH resp = @jp_cnx.put(rsrc) do |req| req.body = data if data end @last_http_response = resp return resp.body if resp.success? raise Jamf::Connection::JamfProAPIError, resp end |
#jp_upload(rsrc, local_file) ⇒ String
Returns the xml response from the server.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 185 def jp_upload(rsrc, local_file) upload_cnx = create_jp_connection upload: true rsrc = rsrc.delete_prefix Jamf::Connection::SLASH payload = {} payload[:file] = Faraday::Multipart::FilePart.new(local_file.to_s, 'application/octet-stream') resp = upload_cnx.post rsrc, payload @last_http_response = resp return resp.body if resp.success? raise Jamf::Connection::JamfProAPIError, resp end |