Skip to content

Commit

Permalink
Implement FileUpload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shea Craig committed Aug 21, 2014
1 parent bf52491 commit ec3adc6
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 0.3.5 (August 21, 2014)

CHANGES:

- Implemented FileUpload. They are kind of unique in the way they operate, so check the docstring for more info.

### 0.3.4 (August 8, 2014)

NOTES:
Expand Down
2 changes: 1 addition & 1 deletion jss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from jss import *


__version__ = "0.3.4"
__version__ = "0.3.5"
116 changes: 105 additions & 11 deletions jss/jss.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class JSSUnsupportedSearchMethodError(Exception):
pass


class JSSFileUploadParameterError(Exception):
pass


class JSSPrefs(object):
"""Uses the OS X preferences system to store credentials and JSS URL."""
def __init__(self, preferences_file=None):
Expand Down Expand Up @@ -294,8 +298,8 @@ def DockItem(self, data=None):
def EBook(self, data=None):
return self.factory.get_object(EBook, data)

# def FileUpload(self, data=None):
# return self.factory.get_object(FileUpload, data)
# FileUploads' only function is to upload, so a method here is not
# provided.

def GSXConnection(self, data=None):
return self.factory.get_object(GSXConnection, data)
Expand Down Expand Up @@ -1006,16 +1010,106 @@ class EBook(JSSContainerObject):
_url = '/ebooks'


class FileUpload(JSSObject):
# Need to think about how to handle this.
def __init__(self):
raise NotImplementedError
class FileUpload(object):
"""FileUploads are a special case in the API. They allow you to add
file resources to a number of objects on the JSS.
To use, instantiate a new FileUpload object, then use the save() method to
upload.
Once the upload has been posted you may only interact with it through the
web interface. You cannot list/get it or delete it through the API.
# _url = '/fileuploads'
# can_put = False
# can_delete = False
# can_get = False
# can_list = False
However, you can reuse the FileUpload object if you wish, by changing the
parameters, and issuing another save().
"""
_url = 'fileuploads'

def __init__(self, j, resource_type, id_type, _id, resource):
"""Prepare a new FileUpload.
j: A JSS object to POST the upload to.
resource_type: String. Acceptable Values:
Attachments:
computers
mobiledevices
enrollmentprofiles
peripherals
Icons:
policies
ebooks
mobiledeviceapplicationsicon
Mobile Device Application:
mobiledeviceapplicationsipa
Disk Encryption
diskencryptionconfigurations
id_type: String of desired ID type:
id
name
_id Int or String referencing the identity value of
the resource to add the FileUpload to.
resource String path to the file to upload.
"""
resource_types = ['computers', 'mobiledevices', 'enrollmentprofiles',
'peripherals', 'policies', 'ebooks',
'mobiledeviceapplicationsicon',
'mobiledeviceapplicationsipa',
'diskencryptionconfigurations']
id_types = ['id', 'name']

self.jss = j

# Do some basic error checking on parameters.
if resource_type in resource_types:
self.resource_type = resource_type
else:
raise JSSFileUploadParameterError("resource_type must be one of: "
"%s" % resource_types)
if id_type in id_types:
self.id_type = id_type
else:
raise JSSFileUploadParameterError("id_type must be one of: "
"%s" % id_types)
self._id = str(_id)

self.resource = {'name': (resource, open(resource, 'rb'),
'multipart/form-data')}

self.set_upload_url()

def set_upload_url(self):
"""Use to generate the full URL to POST to."""
self._upload_url = "/".join([self.jss._url, self._url,
self.resource_type, self.id_type,
str(self._id)])

def save(self):
"""POST the object to the JSS."""
# Until I figure out how to do this using the already established
# session, I will just create a one-off post request directly.
try:
response = requests.post(self._upload_url,
auth=self.jss.session.auth,
files=self.resource)
except JSSPostError as e:
if e.status_code == 409:
raise JSSPostError("Object Conflict! If trying to post a "
"new object, look for name conflict and "
"delete.")
else:
raise JSSMethodNotAllowedError(self.__class__.__name__)

if response.status_code == 201:
if self.jss.verbose:
print("POST: Success")
print(response.text.encode('utf-8'))
elif response.status_code >= 400:
self.jss._error_handler(JSSPostError, response)


class GSXConnection(JSSFlatObject):
Expand Down

0 comments on commit ec3adc6

Please sign in to comment.