Compare commits

...
Sign in to create a new pull request.

7 commits

Author SHA1 Message Date
nanoy
729cfcb7ac Merge branch 'better_403_error' into 'master'
Better 403 error message

See merge request re2o/re2oapi!2
2021-01-12 22:28:22 +01:00
6d930e37ec Better 403 error message 2021-01-12 21:37:59 +01:00
detraz
ffaed92103 Merge branch 'master' of https://gitlab.federez.net/re2o/re2oapi 2019-10-06 16:03:33 +02:00
Hugo Levy-Falk
1f8366055d Better handle of redirections. 2019-09-28 13:32:04 +02:00
Hugo Levy-Falk
b12df74fe7 Fix indentation 2019-03-12 22:05:32 +01:00
Gabriel Detraz
0dd459e3ec unindent error 2018-11-14 17:14:27 +01:00
Gabriel Detraz
b4906d8b25 Api for sending mail 2018-11-14 17:08:55 +01:00
3 changed files with 99 additions and 66 deletions

View file

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

View file

@ -5,9 +5,11 @@ 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 endpoints
from . import exceptions
# Number of seconds before expiration where renewing the token is done
@ -220,6 +222,7 @@ 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())
})
@ -232,10 +235,19 @@ class Re2oAPIClient:
# Perform the request
self.log.info("Performing request {} {}".format(method.upper(), url))
response = getattr(requests, method)(
url, headers=headers, params=params, *args, **kwargs
url, headers=headers, params=params,
allow_redirects=False, *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.
@ -253,13 +265,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)
e = exceptions.PermissionDenied(method, url, self._username, response.reason)
self.log.debug(e)
raise e
response.raise_for_status()
ret = response.json()
self.log.debug("Request {} {} successful.".format(method, url))
self.log.debug("Request {} {} successful.".format(method, response.url))
return ret
def delete(self, *args, **kwargs):
@ -556,3 +568,24 @@ 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 {}."
template = "The {} request to '{}' was denied for {} (reason: {})."
class TokenFileNotFound(APIClientGenericError):