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

Instance Method Details

#c_delete(rsrc) ⇒ String Also known as: delete_rsrc

Delete a resource from the Classic API

Parameters:

  • rsrc (String)

    the resource to create, the URL part after 'JSSResource/'

Returns:

  • (String)

    the xml response from the server.

Raises:



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/jamf/api/connection/classic_api.rb', line 155

def c_delete(rsrc)
  validate_connected @c_cnx
  raise MissingDataError, 'Missing :rsrc' if rsrc.nil?

  # 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

Parameters:

  • rsrc (String)

    the resource to get (the part of the API url after the 'JSSResource/' )

  • format (Symbol) (defaults to: :json)

    either ;json or :xml If the second argument is :xml, the XML data is returned as a String.

  • raw_json (Boolean) (defaults to: false)

    When GETting JSON, return the raw unparsed string (the XML is always returned as a raw string)

Returns:

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jamf/api/connection/classic_api.rb', line 61

def c_get(rsrc, format = :json, raw_json: false)
  validate_connected @c_cnx
  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
    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

Parameters:

  • rsrc (String)

    the API resource being created, the URL part after 'JSSResource/'

  • xml (String)

    the xml specifying the new object.

Returns:

  • (String)

    the xml response from the server.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jamf/api/connection/classic_api.rb', line 91

def c_post(rsrc, xml)
  validate_connected @c_cnx

  # 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

Parameters:

  • rsrc (String)

    the API resource being changed, the URL part after 'JSSResource/'

  • xml (String)

    the xml specifying the changes.

Returns:

  • (String)

    the xml response from the server.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/jamf/api/connection/classic_api.rb', line 124

def c_put(rsrc, xml)
  validate_connected @c_cnx

  # 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.

Parameters:

  • rsrc (String)

    the API resource being uploadad-to, the URL part after 'JSSResource/'

  • local_file (String, Pathname)

    the local file to upload

Returns:

  • (String)

    the xml response from the server.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/jamf/api/connection/classic_api.rb', line 188

def upload(rsrc, local_file)
  validate_connected @c_cnx

  # the upload file object for faraday
  local_file = Pathname.new local_file
  upfile = Faraday::UploadIO.new(
    local_file.to_s,
    'application/octet-stream',
    local_file.basename.to_s
  )

  # send it and get the response
  resp =
    @c_cnx.post rsrc do |req|
      req.headers['Content-Type'] = 'multipart/form-data'
      req.body = { name: upfile }
    end
  @last_http_response = resp

  unless resp.success?
    handle_classic_http_error resp
    return false
  end

  true
end