-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from rbw0/branch-0.7.0
Branch 0.7.0
- Loading branch information
Showing
12 changed files
with
321 additions
and
32 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Attachment | ||
========== | ||
|
||
.. automodule:: pysnow.attachment | ||
.. autoclass:: Attachment | ||
:members: | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
from .params_builder import ParamsBuilder | ||
|
||
__author__ = "Robert Wikman <[email protected]>" | ||
__version__ = "0.6.9" | ||
__version__ = "0.7.0" | ||
|
||
# Set default logging handler to avoid "No handler found" warnings. | ||
import logging | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
from pysnow.exceptions import InvalidUsage | ||
|
||
|
||
class Attachment(object): | ||
"""Attachment management | ||
:param resource: Table API resource to manage attachments for | ||
:param table_name: Name of the table to use in the attachment API | ||
""" | ||
|
||
def __init__(self, resource, table_name): | ||
self.resource = resource | ||
self.table_name = table_name | ||
|
||
def get(self, sys_id=None, limit=100): | ||
"""Returns a list of attachments | ||
:param sys_id: record sys_id to list attachments for | ||
:param limit: override the default limit of 100 | ||
:return: list of attachments | ||
""" | ||
|
||
if sys_id: | ||
return self.resource.get(query={'table_sys_id': sys_id, 'table_name': self.table_name}).all() | ||
|
||
return self.resource.get(query={'table_name': self.table_name}, limit=limit).all() | ||
|
||
def upload(self, sys_id, file_path, name=None, multipart=False): | ||
"""Attaches a new file to the provided record | ||
:param sys_id: the sys_id of the record to attach the file to | ||
:param file_path: local absolute path of the file to upload | ||
:param name: custom name for the uploaded file (instead of basename) | ||
:param multipart: whether or not to use multipart | ||
:return: the inserted record | ||
""" | ||
|
||
if not isinstance(multipart, bool): | ||
raise InvalidUsage('Multipart must be of type bool') | ||
|
||
resource = self.resource | ||
|
||
if name is None: | ||
name = os.path.basename(file_path) | ||
|
||
resource.parameters.add_custom({ | ||
'table_name': self.table_name, | ||
'table_sys_id': sys_id, | ||
'file_name': name | ||
}) | ||
|
||
data = open(file_path, 'rb').read() | ||
headers = {} | ||
|
||
if multipart: | ||
headers["Content-Type"] = "multipart/form-data" | ||
path_append = '/upload' | ||
else: | ||
headers["Content-Type"] = "text/plain" | ||
path_append = '/file' | ||
|
||
return resource.request(method='POST', data=data, headers=headers, path_append=path_append).one() | ||
|
||
def delete(self, sys_id): | ||
"""Deletes the provided attachment record | ||
:param sys_id: attachment sys_id | ||
:return: delete result | ||
""" | ||
|
||
return self.resource.delete(query={'sys_id': sys_id}) |
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
import json | ||
import httpretty | ||
import pysnow | ||
|
||
from pysnow.exceptions import InvalidUsage | ||
|
||
|
||
def get_serialized_result(dict_mock): | ||
return json.dumps({'result': dict_mock}) | ||
|
||
|
||
attachment_path = 'tests/data/attachment.txt' | ||
mock_sys_id = '98ace1a537ea2a00cf5c9c9953990e19' | ||
|
||
attachments_resource = [ | ||
{ | ||
'sys_id': mock_sys_id, | ||
'size_bytes': '512', | ||
'file_name': 'test1.txt' | ||
}, | ||
{ | ||
'sys_id': mock_sys_id, | ||
'size_bytes': '1024', | ||
'file_name': 'test2.txt' | ||
}, | ||
{ | ||
'sys_id': mock_sys_id, | ||
'size_bytes': '2048', | ||
'file_name': 'test3.txt' | ||
} | ||
] | ||
|
||
attachments_resource_sys_id = [ | ||
{ | ||
'sys_id': 'mock_sys_id1', | ||
'size_bytes': '512', | ||
'file_name': 'test1.txt' | ||
}, | ||
{ | ||
'sys_id': 'mock_sys_id2', | ||
'size_bytes': '1024', | ||
'file_name': 'test2.txt' | ||
} | ||
] | ||
|
||
attachment = { | ||
'sys_id': mock_sys_id, | ||
'size_bytes': '512', | ||
'file_name': 'test1.txt' | ||
} | ||
|
||
delete_status = {'status': 'record deleted'} | ||
|
||
|
||
class TestAttachment(unittest.TestCase): | ||
def setUp(self): | ||
client = pysnow.Client(instance='test', user='test', password='test') | ||
r = self.resource = client.resource(api_path='/table/incident') | ||
a = self.attachment_base_url = r._base_url + r._base_path + '/attachment' | ||
self.attachment_url_binary = a + '/file' | ||
self.attachment_url_multipart = a + '/upload' | ||
self.attachment_url_sys_id = a + '/' + mock_sys_id | ||
|
||
@httpretty.activate | ||
def test_get_resource_all(self): | ||
"""Getting metadata for multiple attachments within a resource should work""" | ||
|
||
httpretty.register_uri(httpretty.GET, | ||
self.attachment_base_url, | ||
body=get_serialized_result(attachments_resource), | ||
status=200, | ||
content_type="application/json") | ||
|
||
result = self.resource.attachments.get() | ||
self.assertEqual(attachments_resource, list(result)) | ||
|
||
@httpretty.activate | ||
def test_get_all_by_id(self): | ||
"""Getting attachment metadata for a specific record by sys_id should work""" | ||
|
||
httpretty.register_uri(httpretty.GET, | ||
self.attachment_base_url, | ||
body=get_serialized_result(attachments_resource_sys_id), | ||
status=200, | ||
content_type="application/json") | ||
|
||
result = self.resource.attachments.get(sys_id=mock_sys_id) | ||
self.assertEqual(attachments_resource_sys_id, list(result)) | ||
|
||
@httpretty.activate | ||
def test_upload_binary(self): | ||
"""Uploading with multipart should append /file to URL""" | ||
|
||
httpretty.register_uri(httpretty.POST, | ||
self.attachment_url_binary, | ||
body=get_serialized_result(attachment), | ||
status=201, | ||
content_type="application/json") | ||
|
||
result = self.resource.attachments.upload(sys_id=mock_sys_id, | ||
file_path=attachment_path) | ||
|
||
self.assertEqual(result, attachment) | ||
|
||
@httpretty.activate | ||
def test_upload_multipart(self): | ||
"""Uploading with multipart should append /upload to URL""" | ||
|
||
httpretty.register_uri(httpretty.POST, | ||
self.attachment_url_multipart, | ||
body=get_serialized_result(attachment), | ||
status=201, | ||
content_type="application/json") | ||
|
||
result = self.resource.attachments.upload(sys_id=mock_sys_id, | ||
file_path=attachment_path, | ||
multipart=True) | ||
|
||
self.assertEqual(result, attachment) | ||
|
||
@httpretty.activate | ||
def test_upload_delete(self): | ||
"""Deleting an attachment should trigger pysnow to perform a lookup followed by a delete""" | ||
|
||
httpretty.register_uri(httpretty.GET, | ||
self.attachment_base_url, | ||
body=get_serialized_result(attachment), | ||
status=200, | ||
content_type="application/json") | ||
|
||
httpretty.register_uri(httpretty.DELETE, | ||
self.attachment_url_sys_id, | ||
body=get_serialized_result(delete_status), | ||
status=204, | ||
content_type="application/json") | ||
|
||
result = self.resource.attachments.delete(mock_sys_id) | ||
|
||
self.assertEqual(result, delete_status) | ||
|
||
def test_upload_invalid_multipart_type(self): | ||
"""Passing a non-bool type as multipart argument should raise InvalidUsage""" | ||
|
||
a = self.resource.attachments | ||
self.assertRaises(InvalidUsage, a.upload, mock_sys_id, attachment_path, multipart=dict()) |
Oops, something went wrong.