Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextcloud versioning support added #432

Merged
merged 2 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 64 additions & 18 deletions apprise/plugins/NotifyNextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ class NotifyNextcloud(NotifyBase):
# A URL that takes you to the setup/help of the specific protocol
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_nextcloud'

# Nextcloud URL
notify_url = '{schema}://{host}/ocs/v2.php/apps/admin_notifications/' \
'api/v1/notifications/{target}'

# Nextcloud does not support a title
# Nextcloud title length
title_maxlen = 255

# Defines the maximum allowable characters per message.
Expand Down Expand Up @@ -101,6 +97,22 @@ class NotifyNextcloud(NotifyBase):
},
})

# Define our template arguments
template_args = dict(NotifyBase.template_args, **{
# Nextcloud uses different API end points depending on the version
# being used however the (API) payload remains the same. Allow users
# to specify the version they are using:
'version': {
'name': _('Version'),
'type': 'int',
'min': 1,
'default': 21,
},
'to': {
'alias_of': 'targets',
},
})

# Define any kwargs we're using
template_kwargs = {
'headers': {
Expand All @@ -109,7 +121,7 @@ class NotifyNextcloud(NotifyBase):
},
}

def __init__(self, targets=None, headers=None, **kwargs):
def __init__(self, targets=None, version=None, headers=None, **kwargs):
"""
Initialize Nextcloud Object
"""
Expand All @@ -121,6 +133,20 @@ def __init__(self, targets=None, headers=None, **kwargs):
self.logger.warning(msg)
raise TypeError(msg)

self.version = self.template_args['version']['default']
if version is not None:
try:
self.version = int(version)
if self.version < self.template_args['version']['min']:
# Let upper exception handle this
raise ValueError()

except (ValueError, TypeError):
msg = 'At invalid Nextcloud version ({}) was specified.'\
.format(version)
self.logger.warning(msg)
raise TypeError(msg)

self.headers = {}
if headers:
# Store our extra headers
Expand Down Expand Up @@ -163,17 +189,28 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
if self.user:
auth = (self.user, self.password)

notify_url = self.notify_url.format(
# Nextcloud URL based on version used
notify_url = '{schema}://{host}/ocs/v2.php/'\
'apps/admin_notifications/' \
'api/v1/notifications/{target}' \
if self.version < 21 else \
'{schema}://{host}/ocs/v2.php/'\
'apps/notifications/'\
'api/v2/admin_notifications/{target}'

notify_url = notify_url.format(
schema='https' if self.secure else 'http',
host=self.host if not isinstance(self.port, int)
else '{}:{}'.format(self.host, self.port),
target=target,
)

self.logger.debug('Nextcloud POST URL: %s (cert_verify=%r)' % (
notify_url, self.verify_certificate,
))
self.logger.debug('Nextcloud Payload: %s' % str(payload))
self.logger.debug(
'Nextcloud v%d POST URL: %s (cert_verify=%r)',
self.version, notify_url, self.verify_certificate)
self.logger.debug(
'Nextcloud v%d Payload: %s',
self.version, str(payload))

# Always call throttle before any remote server i/o is made
self.throttle()
Expand All @@ -194,8 +231,9 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
r.status_code)

self.logger.warning(
'Failed to send Nextcloud notification:'
'Failed to send Nextcloud v{} notification:'
'{}{}error={}.'.format(
self.version,
status_str,
', ' if status_str else '',
r.status_code))
Expand All @@ -207,13 +245,13 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
continue

else:
self.logger.info('Sent Nextcloud notification.')
self.logger.info(
'Sent Nextcloud %d notification.', self.version)

except requests.RequestException as e:
self.logger.warning(
'A Connection error occurred sending Nextcloud '
'notification.',
)
'A Connection error occurred sending Nextcloud v%d'
'notification.', self.version)
self.logger.debug('Socket Exception: %s' % str(e))

# track our failure
Expand All @@ -230,8 +268,11 @@ def url(self, privacy=False, *args, **kwargs):
# Create URL parameters from our headers
params = {'+{}'.format(k): v for k, v in self.headers.items()}

# Our URL parameters
params = self.url_parameters(privacy=privacy, *args, **kwargs)
# Set our version
params['version'] = str(self.version)

# Extend our parameters
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))

# Determine Authentication
auth = ''
Expand Down Expand Up @@ -285,6 +326,11 @@ def parse_url(url):
results['targets'] += \
NotifyNextcloud.parse_list(results['qsd']['to'])

# Allow users to over-ride the Nextcloud version being used
if 'version' in results['qsd'] and len(results['qsd']['version']):
results['version'] = \
NotifyNextcloud.unquote(results['qsd']['version'])

# Add our headers that the user can potentially over-ride if they
# wish to to our returned result set
results['headers'] = results['qsd+']
Expand Down
18 changes: 18 additions & 0 deletions test/test_rest_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,18 @@
# No user specified
'instance': TypeError,
}),
('ncloud://user@localhost?to=user1,user2&version=invalid', {
# An invalid version was specified
'instance': TypeError,
}),
('ncloud://user@localhost?to=user1,user2&version=0', {
# An invalid version was specified
'instance': TypeError,
}),
('ncloud://user@localhost?to=user1,user2&version=-23', {
# An invalid version was specified
'instance': TypeError,
}),
('ncloud://localhost/admin', {
'instance': plugins.NotifyNextcloud,
}),
Expand All @@ -2470,6 +2482,12 @@
('ncloud://user@localhost?to=user1,user2', {
'instance': plugins.NotifyNextcloud,
}),
('ncloud://user@localhost?to=user1,user2&version=20', {
'instance': plugins.NotifyNextcloud,
}),
('ncloud://user@localhost?to=user1,user2&version=21', {
'instance': plugins.NotifyNextcloud,
}),
('ncloud://user:pass@localhost/user1/user2', {
'instance': plugins.NotifyNextcloud,

Expand Down