Esempio n. 1
0
    def _send_request(
        self,
        mail: DecodedMail,
        imapc: imapclient.IMAPClient,
        method: str,
        endpoint: str,
        json_body_dict: dict,
    ):
        logger.debug(
            self,
            "Contact API on {endpoint} with method {method} with body {body}".format(
                endpoint=endpoint, method=method, body=str(json_body_dict)
            ),
        )
        if method == "POST":
            request_method = requests.post
        else:
            # TODO - G.M - 2018-08-24 - Better handling exception
            raise UnsupportedRequestMethod("Request method not supported")

        r = request_method(
            url=endpoint,
            json=json_body_dict,
            headers=self._get_auth_headers(mail.get_from_address()),
        )
        if r.status_code not in [200, 204]:
            details = r.json().get("message")
            msg = "bad status code {} (200 and 204 are valid) response when sending mail to tracim: {}"
            msg = msg.format(str(r.status_code), details)
            raise BadStatusCode(msg)
        # Flag all correctly checked mail
        if r.status_code in [200, 204]:
            imapc.add_flags((mail.uid,), IMAP_CHECKED_FLAG)
            imapc.add_flags((mail.uid,), IMAP_SEEN_FLAG)
Esempio n. 2
0
 def _get_content_info(self, content_id, user_email):
     endpoint = "{api_base_url}contents/{content_id}".format(
         api_base_url=self.api_base_url, content_id=content_id
     )
     result = requests.get(endpoint, headers=self._get_auth_headers(user_email))
     if result.status_code not in [200, 204]:
         details = str(result.content)
         msg = "bad status code {}(200 is valid) response when trying to get info about a content: {}"
         msg = msg.format(str(result.status_code), details)
         raise BadStatusCode(msg)
     return result.json()