nncli

NextCloud Notes Command Line Interface
git clone git://git.danielmoch.com/nncli.git
Log | Files | Refs | LICENSE

commit 3c6f785dd7335c107e3edc35a7c068d239e85efa
parent 79e88bfae9ff9c216cb7a207f492dff8ce842847
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Mon, 27 Aug 2018 17:14:30 -0400

Do basic HTTP auth correctly

Fixes #2

Diffstat:
Mnnotes_cli/nextcloud_note.py | 41+++++++++++++++++++----------------------
1 file changed, 19 insertions(+), 22 deletions(-)

diff --git a/nnotes_cli/nextcloud_note.py b/nnotes_cli/nextcloud_note.py @@ -65,14 +65,11 @@ class NextcloudNote(object): def __init__(self, username, password, host): """ object constructor """ - self.username = urllib.parse.quote(username) - self.password = urllib.parse.quote(password) - self.api_url = \ - 'https://{}:{}@{}/index.php/apps/notes/api/v0.2/notes'. \ - format(username, password, host) - self.sanitized_url = \ - 'https://{}:****@{}/index.php/apps/notes/api/v0.2/notes'. \ - format(username, host) + self.username = username + self.password = password + self.url = \ + 'https://{}/index.php/apps/notes/api/v0.2/notes'. \ + format(host) self.status = 'offline' def get_note(self, noteid): @@ -89,10 +86,10 @@ def get_note(self, noteid): """ # request note - url = '{}/{}'.format(self.api_url, str(noteid)) - #logging.debug('REQUEST: ' + self.sanitized_url+params) + url = '{}/{}'.format(self.url, str(noteid)) + #logging.debug('REQUEST: ' + self.url+params) try: - res = requests.get(url) + res = requests.get(url, auth=(self.username, self.password)) res.raise_for_status() note = res.json() self.status = 'online' @@ -136,18 +133,19 @@ def update_note(self, note): if 'modified' not in note: note["modified"] = int(time.time()) - url = '{}/{}'.format(self.api_url, note["id"]) + url = '{}/{}'.format(self.url, note["id"]) del note["id"] else: - url = self.api_url + url = self.url #logging.debug('REQUEST: ' + url + ' - ' + str(note)) try: logging.debug('NOTE: ' + str(note)) - if url != self.api_url: - res = requests.put(url, data=note) + if url != self.url: + res = requests.put(url, auth=(self.username, self.password), data=note) else: - res = requests.post(url, data=note) + res = requests.post(url, auth=(self.username, + self.password), data=note) note = res.json() res.raise_for_status() logging.debug('NOTE (from response): ' + str(res.json())) @@ -219,9 +217,9 @@ def get_note_list(self, category=None): # perform initial HTTP request try: - logging.debug('REQUEST: ' + self.sanitized_url + \ + logging.debug('REQUEST: ' + self.url + \ '?exclude=content') - res = requests.get(self.api_url, params=params) + res = requests.get(self.url, auth=(self.username, self.password), params=params) res.raise_for_status() #logging.debug('RESPONSE OK: ' + str(res)) note_list = res.json() @@ -261,12 +259,11 @@ def delete_note(self, note): - status (int): 0 on sucesss and -1 otherwise """ - url = '{}/{}'.format(self.api_url, str(note['id'])) - logurl = '{}/{}'.format(self.sanitized_url, str(note['id'])) + url = '{}/{}'.format(self.url, str(note['id'])) try: - logging.debug('REQUEST DELETE: ' + logurl) - res = requests.delete(url) + logging.debug('REQUEST DELETE: ' + url) + res = requests.delete(url, auth=(self.username, self.password)) res.raise_for_status() self.status = 'online' except ConnectionError as e: