Merge pull request #70 from kphatak/Issue-69-headers

Adding HTTP Headers Support for JSON and XML; refs #69
pull/72/head
Chris Caron 2019-02-16 00:30:03 -05:00 committed by GitHub
commit ab306dc74b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 4 deletions

3
.gitignore vendored
View File

@ -61,3 +61,6 @@ target/
#Ipython Notebook #Ipython Notebook
.ipynb_checkpoints .ipynb_checkpoints
#PyCharm
.idea

View File

@ -162,6 +162,7 @@ class NotifyBase(object):
self.user = kwargs.get('user') self.user = kwargs.get('user')
self.password = kwargs.get('password') self.password = kwargs.get('password')
self.headers = kwargs.get('headers')
if 'format' in kwargs: if 'format' in kwargs:
# Store the specified format if specified # Store the specified format if specified
@ -424,4 +425,6 @@ class NotifyBase(object):
if 'user' in results['qsd']: if 'user' in results['qsd']:
results['user'] = results['qsd']['user'] results['user'] = results['qsd']['user']
results['headers'] = {k[1:]: v for k, v in results['qsd'].items()
if re.match(r'^-.', k)}
return results return results

View File

@ -181,7 +181,7 @@ class NotifyDiscord(NotifyBase):
else: else:
# not markdown # not markdown
payload['content'] = body if not title \ payload['content'] = body if not title \
else "{}\r\n{}".format(title, body) else "{}\r\n{}".format(title, body)
if self.avatar and image_url: if self.avatar and image_url:
payload['avatar_url'] = image_url payload['avatar_url'] = image_url
@ -298,7 +298,7 @@ class NotifyDiscord(NotifyBase):
""" """
regex = re.compile( regex = re.compile(
r'^\s*#+\s*(?P<name>[^#\n]+)([ \r\t\v#])?' r'^\s*#+\s*(?P<name>[^#\n]+)([ \r\t\v#])?'
r'(?P<value>([^ \r\t\v#].+?)(\n(?!\s#))|\s*$)', flags=re.S|re.M) r'(?P<value>([^ \r\t\v#].+?)(\n(?!\s#))|\s*$)', flags=re.S | re.M)
common = regex.finditer(markdown) common = regex.finditer(markdown)
fields = list() fields = list()

View File

@ -91,6 +91,9 @@ class NotifyJSON(NotifyBase):
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
if self.headers:
headers.update(self.headers)
auth = None auth = None
if self.user: if self.user:
auth = (self.user, self.password) auth = (self.user, self.password)

View File

@ -58,8 +58,8 @@ LIST_DELIM = re.compile(r'[ \t\r\n,\\/]+')
# region as a delimiter. This is a bit hacky; but it's much easier than having # region as a delimiter. This is a bit hacky; but it's much easier than having
# users of this product search though this Access Key Secret and escape all # users of this product search though this Access Key Secret and escape all
# of the forward slashes! # of the forward slashes!
IS_REGION = re.compile( IS_REGION = re.compile(r'^\s*(?P<country>[a-z]{2})-'
r'^\s*(?P<country>[a-z]{2})-(?P<area>[a-z]+)-(?P<no>[0-9]+)\s*$', re.I) r'(?P<area>[a-z]+)-(?P<no>[0-9]+)\s*$', re.I)
# Extend HTTP Error Messages # Extend HTTP Error Messages
AWS_HTTP_ERROR_MAP = HTTP_ERROR_MAP.copy() AWS_HTTP_ERROR_MAP = HTTP_ERROR_MAP.copy()

View File

@ -96,6 +96,9 @@ class NotifyXML(NotifyBase):
'Content-Type': 'application/xml' 'Content-Type': 'application/xml'
} }
if self.headers:
headers.update(self.headers)
re_map = { re_map = {
'{MESSAGE_TYPE}': NotifyBase.quote(notify_type), '{MESSAGE_TYPE}': NotifyBase.quote(notify_type),
'{SUBJECT}': NotifyBase.quote(title), '{SUBJECT}': NotifyBase.quote(title),

View File

@ -166,6 +166,12 @@ def test_notify_base_urls():
assert 'password' in results assert 'password' in results
assert results['password'] == "newpassword" assert results['password'] == "newpassword"
# pass headers
results = NotifyBase.parse_url(
'https://localhost:8080?-HeaderKey=HeaderValue')
assert 'headerkey' in results['headers']
assert results['headers']['headerkey'] == 'HeaderValue'
# User Handling # User Handling
# user keyword over-rides default password # user keyword over-rides default password

View File

@ -431,6 +431,10 @@ TEST_URLS = (
# is set and tests that we gracfully handle them # is set and tests that we gracfully handle them
'test_requests_exceptions': True, 'test_requests_exceptions': True,
}), }),
('json://localhost:8080/path?-HeaderKey=HeaderValue', {
'instance': plugins.NotifyJSON,
}),
################################## ##################################
# NotifyKODI # NotifyKODI
@ -1470,6 +1474,9 @@ TEST_URLS = (
# is set and tests that we gracfully handle them # is set and tests that we gracfully handle them
'test_requests_exceptions': True, 'test_requests_exceptions': True,
}), }),
('xml://localhost:8080/path?-HeaderKey=HeaderValue', {
'instance': plugins.NotifyXML,
}),
) )