Nextcloud versioning support added (#432)

pull/436/head
Chris Caron 2021-08-29 12:02:56 -04:00 committed by GitHub
parent a888b85f8e
commit 81d1ea72bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 18 deletions

View File

@ -51,11 +51,7 @@ class NotifyNextcloud(NotifyBase):
# A URL that takes you to the setup/help of the specific protocol # A URL that takes you to the setup/help of the specific protocol
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_nextcloud' setup_url = 'https://github.com/caronc/apprise/wiki/Notify_nextcloud'
# Nextcloud URL # Nextcloud title length
notify_url = '{schema}://{host}/ocs/v2.php/apps/admin_notifications/' \
'api/v1/notifications/{target}'
# Nextcloud does not support a title
title_maxlen = 255 title_maxlen = 255
# Defines the maximum allowable characters per message. # Defines the maximum allowable characters per message.
@ -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 # Define any kwargs we're using
template_kwargs = { template_kwargs = {
'headers': { 'headers': {
@ -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 Initialize Nextcloud Object
""" """
@ -121,6 +133,20 @@ class NotifyNextcloud(NotifyBase):
self.logger.warning(msg) self.logger.warning(msg)
raise TypeError(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 = {} self.headers = {}
if headers: if headers:
# Store our extra headers # Store our extra headers
@ -163,17 +189,28 @@ class NotifyNextcloud(NotifyBase):
if self.user: if self.user:
auth = (self.user, self.password) 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', schema='https' if self.secure else 'http',
host=self.host if not isinstance(self.port, int) host=self.host if not isinstance(self.port, int)
else '{}:{}'.format(self.host, self.port), else '{}:{}'.format(self.host, self.port),
target=target, target=target,
) )
self.logger.debug('Nextcloud POST URL: %s (cert_verify=%r)' % ( self.logger.debug(
notify_url, self.verify_certificate, 'Nextcloud v%d POST URL: %s (cert_verify=%r)',
)) self.version, notify_url, self.verify_certificate)
self.logger.debug('Nextcloud Payload: %s' % str(payload)) self.logger.debug(
'Nextcloud v%d Payload: %s',
self.version, str(payload))
# Always call throttle before any remote server i/o is made # Always call throttle before any remote server i/o is made
self.throttle() self.throttle()
@ -194,8 +231,9 @@ class NotifyNextcloud(NotifyBase):
r.status_code) r.status_code)
self.logger.warning( self.logger.warning(
'Failed to send Nextcloud notification:' 'Failed to send Nextcloud v{} notification:'
'{}{}error={}.'.format( '{}{}error={}.'.format(
self.version,
status_str, status_str,
', ' if status_str else '', ', ' if status_str else '',
r.status_code)) r.status_code))
@ -207,13 +245,13 @@ class NotifyNextcloud(NotifyBase):
continue continue
else: else:
self.logger.info('Sent Nextcloud notification.') self.logger.info(
'Sent Nextcloud %d notification.', self.version)
except requests.RequestException as e: except requests.RequestException as e:
self.logger.warning( self.logger.warning(
'A Connection error occurred sending Nextcloud ' 'A Connection error occurred sending Nextcloud v%d'
'notification.', 'notification.', self.version)
)
self.logger.debug('Socket Exception: %s' % str(e)) self.logger.debug('Socket Exception: %s' % str(e))
# track our failure # track our failure
@ -230,8 +268,11 @@ class NotifyNextcloud(NotifyBase):
# Create URL parameters from our headers # Create URL parameters from our headers
params = {'+{}'.format(k): v for k, v in self.headers.items()} params = {'+{}'.format(k): v for k, v in self.headers.items()}
# Our URL parameters # Set our version
params = self.url_parameters(privacy=privacy, *args, **kwargs) params['version'] = str(self.version)
# Extend our parameters
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))
# Determine Authentication # Determine Authentication
auth = '' auth = ''
@ -285,6 +326,11 @@ class NotifyNextcloud(NotifyBase):
results['targets'] += \ results['targets'] += \
NotifyNextcloud.parse_list(results['qsd']['to']) 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 # Add our headers that the user can potentially over-ride if they
# wish to to our returned result set # wish to to our returned result set
results['headers'] = results['qsd+'] results['headers'] = results['qsd+']

View File

@ -2461,6 +2461,18 @@ TEST_URLS = (
# No user specified # No user specified
'instance': TypeError, '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', { ('ncloud://localhost/admin', {
'instance': plugins.NotifyNextcloud, 'instance': plugins.NotifyNextcloud,
}), }),
@ -2470,6 +2482,12 @@ TEST_URLS = (
('ncloud://user@localhost?to=user1,user2', { ('ncloud://user@localhost?to=user1,user2', {
'instance': plugins.NotifyNextcloud, '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', { ('ncloud://user:pass@localhost/user1/user2', {
'instance': plugins.NotifyNextcloud, 'instance': plugins.NotifyNextcloud,