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

Instance Method Details

#create_jp_connection(parse_json: true) ⇒ Object

create the faraday JPAPI connection object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 38

def create_jp_connection(parse_json: true)
  Faraday.new(@jp_base_url, ssl: ssl_options) do |cnx|
    cnx.authorization :Bearer, @token.token

    cnx.options[:timeout] = @timeout
    cnx.options[:open_timeout] = @open_timeout

    if parse_json
      cnx.request :json
      cnx.response :json, parser_options: { symbolize_names: true }
    end

    cnx.adapter Faraday::Adapter::NetHttp
  end
end

#jp_delete(rsrc) ⇒ String Also known as: delete

Delete an existing Jamf Pro API resource

Parameters:

  • rsrc (String)

    the API resource being deleted, the URL part after ‘api/’

Returns:

  • (String)

    the response from the server.

Raises:



151
152
153
154
155
156
157
158
159
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 151

def jp_delete(rsrc)
  validate_connected @jp_cnx
  rsrc = rsrc.delete_prefix Jamf::Connection::SLASH
  resp = @jp_cnx.delete rsrc
  @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



166
167
168
169
170
171
172
173
174
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 166

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) ⇒ Hash Also known as: get

Returns the result of the get.

Returns:

  • (Hash)

    the result of the get

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 59

def jp_get(rsrc)
  validate_connected @jp_cnx
  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']}"
  end
  @last_http_response = resp

  return resp.body if resp.success?

  raise Jamf::Connection::JamfProAPIError, resp
end

#jp_patch(rsrc, data) ⇒ String Also known as: patch

Update an existing Jamf Pro API resource

Parameters:

  • rsrc (String)

    the API resource being changed, the URL part after ‘api/’

  • data (String)

    the json specifying the changes.

Returns:

  • (String)

    the response from the server.

Raises:



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 130

def jp_patch(rsrc, data)
  validate_connected @jp_cnx
  rsrc = rsrc.delete_prefix Jamf::Connection::SLASH
  resp = @jp_cnx.patch(rsrc) do |req|
    req.body = data
  end
  @last_http_response = resp
  return resp.body if resp.success?

  raise Jamf::Connection::JamfProAPIError, resp
end

#jp_post(rsrc, data) ⇒ String Also known as: post

Create a JPAPI resource via POST

Parameters:

  • rsrc (String)

    the resource to POST (the part of the API url after the ‘/api/’ )

  • data (String)

    the JSON data to POST

Returns:

  • (String)

    the response body

Raises:



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 84

def jp_post(rsrc, data)
  validate_connected @jp_cnx
  rsrc = rsrc.delete_prefix Jamf::Connection::SLASH
  resp = @jp_cnx.post(rsrc) do |req|
    req.body = data
  end
  @last_http_response = resp
  return resp.body if resp.success?

  raise Jamf::Connection::JamfProAPIError, resp
end

#jp_put(rsrc, data) ⇒ String Also known as: put

Replace an existing Jamf Pro API resource

Parameters:

  • rsrc (String)

    the API resource being changed, the URL part after ‘api/’

  • data (String)

    the json specifying the changes.

Returns:

  • (String)

    the response from the server.

Raises:



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jamf/api/connection/jamf_pro_api.rb', line 107

def jp_put(rsrc, data)
  validate_connected @jp_cnx
  rsrc = rsrc.delete_prefix Jamf::Connection::SLASH
  resp = @jp_cnx.put(rsrc) do |req|
    req.body = data
  end
  @last_http_response = resp
  return resp.body if resp.success?

  raise Jamf::Connection::JamfProAPIError, resp
end