GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 6f2c0c...86ad1d )
by
unknown
01:09
created

req()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
1
# -*- coding: utf-8 -*-
2
3
import requests
4
from django.conf import settings
5
6
7
def get_api_header():
8
    return {
9
        'X-Secure-Token': settings.MAILHANDLER_RU_KEY,
10
        'Accept': 'application/json',
11
        'Content-Type': 'application/json',
12
    }
13
14
15
def get_url(url):
16
    return "http://api.mailhandler.ru/{}".format(url)
17
18
19
def send_email(subject, html_body):
20
    """
21
    Send email with requests python library.
22
    """
23
    headers = get_api_header()
24
25
    emails = get_user_emails(settings.MAILHANDLER_RU_USER_LIST_ID)
26
    data = {
27
        'from': '[email protected]',
28
        'to': emails,
29
        'subject': subject,
30
        'html_body': html_body
31
    }
32
    response = requests.post(get_url('message/send/'), json=data,
33
                             headers=headers)
34
    return response.json()
35
36
37
def req(url, get=True, data=None):
38
    if get:
39
        func = requests.get
40
    else:
41
        func = requests.post
42
43
    return func(url, headers=get_api_header(), json=data)
44
45
46
def get_lists():
47
    response = req(get_url('sub/lists/'))
48
49
    items = []
50
    items.extend(response.json()['results'])
51
    while response.json()['next'] is not None:
52
        response = req(response.json()['next'])
53
        items.extend(response.json()['results'])
54
55
    return items
56
57
58
def get_id_list_by_name(lists, name):
59
    for item in lists:
60
        if item.get('name', '') == name:
61
            return item['id']
62
    else:
63
        raise NotImplemented
64
65
66
def get_user_emails(list_id):
67
    users = []
68
    response = req(get_url('sub/lists/{}/subscribers/'.format(list_id)))
69
    users.extend(response.json()['results'])
70
    while response.json()['next'] is not None:
71
        response = req(response.json()['next'])
72
        users.extend(response.json()['results'])
73
74
    return [x['email'] for x in users if x['is_active']]
75