Compare commits

..

3 commits

Author SHA1 Message Date
Charlie Jacomme
6565b92f3b Revert "fix list function to follow pages"
Stupid Charlie, stupid
This reverts commit 61aba4812b.
2018-06-27 19:10:21 +02:00
Charlie Jacomme
61aba4812b fix list function to follow pages 2018-06-27 19:07:00 +02:00
Charlie Jacomme
b0885f7b84 Removing old import 2018-06-27 17:06:17 +02:00
3 changed files with 65 additions and 99 deletions

View file

@ -1,4 +1,4 @@
from .client import ApiSendMail, Re2oAPIClient
from .client import Re2oAPIClient
from . import exceptions
__all__ = ['Re2oAPIClient', 'ApiSendMail', 'exceptions']
__all__ = ['Re2oAPIClient', 'exceptions']

View file

@ -5,9 +5,6 @@ from pathlib import Path
import stat
import json
import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from requests.exceptions import HTTPError
from . import exceptions
@ -222,7 +219,6 @@ class Re2oAPIClient:
# Update headers to force the 'Authorization' field with the right token
self.log.debug("Forcing authentication token.")
self.log.debug("Token =" + str(self.get_token()))
headers.update({
'Authorization': 'Token {}'.format(self.get_token())
})
@ -235,19 +231,10 @@ class Re2oAPIClient:
# Perform the request
self.log.info("Performing request {} {}".format(method.upper(), url))
response = getattr(requests, method)(
url, headers=headers, params=params,
allow_redirects=False, *args, **kwargs
url, headers=headers, params=params, *args, **kwargs
)
self.log.debug("Response code: "+str(response.status_code))
if response.is_redirect:
self.log.debug("Redirection detected.")
response = getattr(requests, method)(
response.headers['Location'], headers=headers, params=params,
allow_redirects=False, *args, **kwargs
)
self.log.debug("Response code after redirection: "+str(response.status_code))
if response.status_code == requests.codes.unauthorized:
# Force re-login to the server (case of a wrong token but valid
# credentials) and then retry the request without catching errors.
@ -265,13 +252,13 @@ class Re2oAPIClient:
self.log.debug("Response code: "+str(response.status_code))
if response.status_code == requests.codes.forbidden:
e = exceptions.PermissionDenied(method, url, self._username, response.reason)
e = exceptions.PermissionDenied(method, url, self._username)
self.log.debug(e)
raise e
response.raise_for_status()
ret = response.json()
self.log.debug("Request {} {} successful.".format(method, response.url))
self.log.debug("Request {} {} successful.".format(method, url))
return ret
def delete(self, *args, **kwargs):
@ -568,24 +555,3 @@ class Re2oAPIClient:
self.log.debug("Viewing object under '{}' successful"
.format(endpoint))
return ret
class ApiSendMail:
"""Basic api for sending mails"""
def __init__(self, server, port, starttls=False):
"""Give here the server, the port and tls or not"""
self.connection = smtplib.SMTP(server, port)
if starttls:
self.connection.starttls()
def send_mail(self, email_from, email_to, subject, body, mode='html'):
"""Sending mail from from, to, subject and body"""
self.msg = MIMEMultipart()
self.msg['From'] = email_from
self.msg['To'] = email_to
self.msg['Subject'] = subject
self.msg.attach(MIMEText(body, mode))
self.connection.sendmail(email_from, email_to, self.msg.as_string())
def close(self):
self.connection.quit()

View file

@ -12,7 +12,7 @@ class InvalidCredentials(APIClientGenericError):
class PermissionDenied(APIClientGenericError):
template = "The {} request to '{}' was denied for {} (reason: {})."
template = "The {} request to '{}' was denied for {}."
class TokenFileNotFound(APIClientGenericError):