Module: Jamf::Connection::ClassicAPI
- Included in:
- Jamf::Connection
- Defined in:
- lib/jamf/api/connection/classic_api.rb
Overview
This module defines methods used for interacting with the Classic API. This includes creating the Faraday connection, sending HTTP requests and handling responses
Instance Method Summary collapse
-
#c_delete(rsrc) ⇒ String
(also: #delete_rsrc)
Delete a resource from the Classic API.
-
#c_get(rsrc, format = :json, raw_json: false) ⇒ Hash, String
(also: #get_rsrc)
Get a Classic API resource via GET.
-
#c_post(rsrc, xml) ⇒ String
(also: #post_rsrc)
Create a new Classic API resource via POST.
-
#c_put(rsrc, xml) ⇒ String
(also: #put_rsrc)
Update an existing Classic API resource.
-
#upload(rsrc, local_file) ⇒ String
Upload a file.
Instance Method Details
#c_delete(rsrc) ⇒ String Also known as: delete_rsrc
Delete a resource from the Classic API
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/jamf/api/connection/classic_api.rb', line 145 def c_delete(rsrc) validate_connected raise MissingDataError, 'Missing :rsrc' if rsrc.nil? rsrc = rsrc.delete_prefix Jamf::Connection::SLASH # delete the resource resp = @c_cnx.delete(rsrc) do |req| req.headers[Jamf::Connection::HTTP_CONTENT_TYPE_HEADER] = Jamf::Connection::MIME_XML req.headers[Jamf::Connection::HTTP_ACCEPT_HEADER] = Jamf::Connection::MIME_XML end @last_http_response = resp unless resp.success? handle_classic_http_error resp return end resp.body end |
#c_get(rsrc, format = :json, raw_json: false) ⇒ Hash, String Also known as: get_rsrc
Get a Classic API resource via GET
The first argument is the resource to get (the part of the API url after the ‘JSSResource/’ ) The resource must be properly URL escaped beforehand. Note: URL.encode is deprecated, use CGI.escape
By default we get the data in JSON, and parse it into a ruby Hash with symbolized Hash keys.
If the second parameter is :xml then the XML version is retrieved and returned as a String.
To get the raw JSON string as it comes from the API, pass raw_json: true
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jamf/api/connection/classic_api.rb', line 44 def c_get(rsrc, format = :json, raw_json: false) rsrc = rsrc.delete_prefix Jamf::Connection::SLASH validate_connected raise Jamf::InvalidDataError, 'format must be :json or :xml' unless Jamf::Connection::GET_FORMATS.include?(format) resp = @c_cnx.get(rsrc) do |req| req.headers[Jamf::Connection::HTTP_ACCEPT_HEADER] = format == :json ? Jamf::Connection::MIME_JSON : Jamf::Connection::MIME_XML # puts "Classic API Cookie is: #{req.headers['Cookie']}" end @last_http_response = resp unless resp.success? handle_classic_http_error resp return end return JSON.parse(resp.body, symbolize_names: true) if format == :json && !raw_json # the raw body, either json or xml resp.body end |
#c_post(rsrc, xml) ⇒ String Also known as: post_rsrc
Create a new Classic API resource via POST
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/jamf/api/connection/classic_api.rb', line 77 def c_post(rsrc, xml) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH # convert CRs & to xml&.gsub!(/\r/, ' ') # send the data resp = @c_cnx.post(rsrc) do |req| req.headers[Jamf::Connection::HTTP_CONTENT_TYPE_HEADER] = Jamf::Connection::MIME_XML req.headers[Jamf::Connection::HTTP_ACCEPT_HEADER] = Jamf::Connection::MIME_XML req.body = xml end @last_http_response = resp unless resp.success? handle_classic_http_error resp return end resp.body end |
#c_put(rsrc, xml) ⇒ String Also known as: put_rsrc
Update an existing Classic API resource
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/jamf/api/connection/classic_api.rb', line 112 def c_put(rsrc, xml) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH # convert CRs & to xml.gsub!(/\r/, ' ') # send the data resp = @c_cnx.put(rsrc) do |req| req.headers[Jamf::Connection::HTTP_CONTENT_TYPE_HEADER] = Jamf::Connection::MIME_XML req.headers[Jamf::Connection::HTTP_ACCEPT_HEADER] = Jamf::Connection::MIME_XML req.body = xml end @last_http_response = resp unless resp.success? handle_classic_http_error resp return end resp.body end |
#upload(rsrc, local_file) ⇒ String
Upload a file. This is really only used for the ‘fileuploads’ endpoint of the classic API, as implemented in the Uploadable mixin module, q.v.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/jamf/api/connection/classic_api.rb', line 180 def upload(rsrc, local_file) validate_connected rsrc = rsrc.delete_prefix Jamf::Connection::SLASH payload = {} payload[:name] = Faraday::Multipart::FilePart.new(local_file.to_s, 'application/octet-stream') resp = @c_cnx.post rsrc, payload @last_http_response = resp unless resp.success? handle_classic_http_error resp return false end true end |