Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
6565b92f3b | ||
![]() |
61aba4812b | ||
![]() |
b0885f7b84 |
3 changed files with 65 additions and 99 deletions
|
@ -1,4 +1,4 @@
|
|||
from .client import ApiSendMail, Re2oAPIClient
|
||||
from .client import Re2oAPIClient
|
||||
from . import exceptions
|
||||
|
||||
__all__ = ['Re2oAPIClient', 'ApiSendMail', 'exceptions']
|
||||
__all__ = ['Re2oAPIClient', 'exceptions']
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue