def test_get_key(self):
     yelp_key = authentication.get_token('https://www.apitutor.org/yelp/key')
     self.assertEqual(len(yelp_key), 128)
     time.sleep(1.0)
     spotify_key = authentication.get_token('https://www.apitutor.org/spotify/key')
     self.assertEqual(len(spotify_key), 144)
     time.sleep(1.0)
 def test_malformed_query_yields_errors(self):
     with self.assertRaises(Exception) as cm:
         authentication.get_token('https://www.apitutor.org/yelp/ke')
     self.assertIn(
         'This URL is invalid: https://www.apitutor.org/yelp/ke', str(cm.exception)
     )
     time.sleep(1.0)
     with self.assertRaises(Exception) as cm:
         authentication.get_token('https://www.apitutor.org/spotify/ke')
     self.assertIn(
         'This URL is invalid: https://www.apitutor.org/spotify/ke', str(cm.exception)
     )
Beispiel #3
0
def _issue_get_request(url):
    token = authentication.get_token('https://www.apitutor.org/spotify/key')
    request = urllib.request.Request(url, None,
                                     {'Authorization': 'Bearer ' + token})
    with urllib.request.urlopen(request) as response:
        data = json.loads(response.read().decode())
        return data
Beispiel #4
0
def send_mail(from_email:str, to_emails:tuple, subject:str, html_content:str):
    '''
    Uses the SendGrid API to send an email. 
        * from_email(str):      Required. The sender's email
        * to_emails(tuple):     Required: A tuple of recipient emails
        * subject(str):         Required: The subject of the email 
        * html_content(str):    Required: Text or HTML to be included in the body of the email.
    Returns True if the email was successfully sent, False otherwise.
    '''
    message = Mail(
        from_email=from_email,
        to_emails=to_emails,
        subject=subject,
        html_content=html_content
    )
    try:
        token = authentication.get_token('https://www.apitutor.org/sendgrid/key')
        sg = SendGridAPIClient(token)
        sg.send(message)
        print('Email sent.')
        return True
        # response = sg.send(message)
        # print(response.status_code)
        # print(response.body)
        # print(response.headers)
    except Exception as e:
        print(e)
        return False
Beispiel #5
0
def email_with_html_attachment(from_email: str, to_email: str, subject: str,
                               body: str, filename: str, file_path: str):
    import base64
    from apis import authentication
    from sendgrid.helpers.mail import (Mail, Attachment, FileContent, FileName,
                                       FileType, Disposition)
    from sendgrid import SendGridAPIClient

    message = Mail(from_email=from_email,
                   to_emails=to_email,
                   subject=subject,
                   html_content=body)
    with open(file_path, 'rb') as f:
        data = f.read()
        f.close()
    encoded = base64.b64encode(data).decode()
    attachment = Attachment()
    attachment.file_content = FileContent(encoded)
    attachment.file_type = FileType('text/html')
    attachment.file_name = FileName(filename)
    attachment.disposition = Disposition('attachment')
    message.attachment = attachment
    try:
        sendgrid_client = SendGridAPIClient(
            authentication.get_token('https://www.apitutor.org/sendgrid/key'))
        sendgrid_client.send(message)
        return True
    except:
        return False
Beispiel #6
0
def _send_get_request(url: str):
    token = authentication.get_token('https://www.apitutor.org/yelp/key')
    request = urllib.request.Request(url, None,
                                     {'Authorization': 'Bearer ' + token})
    try:
        with urllib.request.urlopen(request) as response:
            data = json.loads(response.read().decode())
            return data
    except urllib.error.HTTPError as e:
        # give a good error message:
        error = utilities.get_error_message(e, url)
    raise Exception(error)
Beispiel #7
0
def _issue_get_request(url: str):
    token = authentication.get_token('https://www.apitutor.org/yelp/key')
    request = urllib.request.Request(url, None,
                                     {'Authorization': 'Bearer ' + token})
    try:
        with urllib.request.urlopen(request) as response:
            data = json.loads(response.read().decode())
            return data
    except urllib.error.HTTPError as e:
        error_message = e.read()
        error_message = '\n' + str(e) + '\n' + str(
            json.loads(error_message)) + '\n'
        raise Exception(error_message)