Skip to content

Commit

Permalink
Use Magic to resolve content-type on upload (#121)
Browse files Browse the repository at this point in the history
Performs conditional import of magic with hinting about
python-magic-bin on Mac and Windows

Signed-off-by: Robert Wikman <[email protected]>
  • Loading branch information
rbw authored Aug 6, 2019
1 parent f7d8d35 commit 20e613f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ python:
- "3.6"
# command to install dependencies
install:
- pip install pysnow requests httpretty oauthlib requests_oauthlib coverage python-coveralls nose ijson six pytz
- pip install pysnow requests httpretty oauthlib requests_oauthlib coverage python-coveralls nose ijson six pytz python-magic
# command to run tests
script:
- nosetests --with-coverage --cover-package=pysnow --cover-erase
Expand Down
4 changes: 2 additions & 2 deletions pysnow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from .params_builder import ParamsBuilder

__author__ = "Robert Wikman <[email protected]>"
__version__ = "0.7.8"
__version__ = "0.7.9"

# Set default logging handler to avoid "No handler found" warnings.
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
except ImportError: # pragma: no cover
class NullHandler(logging.Handler):
def emit(self, record):
pass
Expand Down
12 changes: 11 additions & 1 deletion pysnow/attachment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# -*- coding: utf-8 -*-

import os
import warnings

try:
import magic
HAS_MAGIC = True
except ImportError: # pragma: no cover
HAS_MAGIC = False
warnings.warn("Missing the python-magic library; attachment content-type will be set to text/plain. "
"Try installing the python-magic-bin package if running on Mac or Windows")

from pysnow.exceptions import InvalidUsage


Expand Down Expand Up @@ -59,7 +69,7 @@ def upload(self, sys_id, file_path, name=None, multipart=False):
headers["Content-Type"] = "multipart/form-data"
path_append = '/upload'
else:
headers["Content-Type"] = "text/plain"
headers["Content-Type"] = magic.from_file(file_path, mime=True) if HAS_MAGIC else "text/plain"
path_append = '/file'

return resource.request(method='POST', data=data, headers=headers, path_append=path_append)
Expand Down
11 changes: 10 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ def get_version():
version=get_version(),
description='ServiceNow HTTP client library',
long_description=readme.read(),
install_requires=['requests', 'oauthlib', 'requests_oauthlib', 'httpretty', 'six', 'ijson', 'pytz'],
install_requires=[
'requests',
'oauthlib',
'python-magic',
'requests_oauthlib',
'httpretty',
'six',
'ijson',
'pytz'
],
author='Robert Wikman',
author_email='[email protected]',
maintainer='Robert Wikman',
Expand Down

0 comments on commit 20e613f

Please sign in to comment.