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:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/jamf/api/connection/classic_api.rb', line 162

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

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
80
81
82
# File 'lib/jamf/api/connection/classic_api.rb', line 61

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

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.



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

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

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.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jamf/api/connection/classic_api.rb', line 129

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.

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.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/jamf/api/connection/classic_api.rb', line 197

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