Created
August 10, 2020 14:26
-
-
Save UniversalSuperBox/3a6d5b93f8faf43579061302a0a1006d to your computer and use it in GitHub Desktop.
A small JPS API wrapper for getting mobile device details and setting mobile device extension attributes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2020 Dalton Durst | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import requests | |
from requests.auth import HTTPBasicAuth | |
class JPSSupport: | |
"""A limited API wrapper for the Jamf Pro Server | |
:param base_url: URL of the JPS. For example, https://jamf.example.com:8443 | |
:param api_username: Username to sign in to JPS with. | |
:param api_password: Password to sign in to JPS with. | |
""" | |
def __init__(self, base_url, api_username, api_password): | |
self._session = requests.Session() | |
self.base_url = base_url | |
r = self._session.post( | |
base_url + "/api/auth/tokens", | |
auth=HTTPBasicAuth(api_username, api_password), | |
) | |
r.raise_for_status() | |
token = r.json() | |
self._token = token["token"] | |
self._token_expires = token["expires"] | |
def _authorization(self): | |
return {"Authorization": "Bearer " + self._token} | |
def get_mobile_device_detail(self, mobile_device_id): | |
"""Returns a mobile device dictionary for the device with the given ID. | |
""" | |
r = self._session.get( | |
"{}/api/v2/mobile-devices/{}/detail".format( | |
self.base_url, mobile_device_id | |
), | |
headers=self._authorization(), | |
) | |
r.raise_for_status() | |
return r.json() | |
def set_mobile_device_extension_attribute( | |
self, mobile_device_id: int, extension_attribute_name: str, value: str | |
): | |
"""Sets an extension attribute on a mobile device. | |
:param mobile_device_id: JAMF API ID of the mobile device to modify. | |
:param extension_attribute_name: Name of the extension attribute to set. | |
:value: The text to set this extension attribute to. | |
:returns: Updated device dictionary | |
""" | |
new_json = { | |
"updatedExtensionAttributes": [ | |
{"name": extension_attribute_name, "value": [value]} | |
] | |
} | |
r = self._session.patch( | |
"{}/api/v2/mobile-devices/{}".format(self.base_url, mobile_device_id), | |
json=new_json, | |
headers=self._authorization(), | |
) | |
r.raise_for_status() | |
return r.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment