File: example_user.py

package info (click to toggle)
python-cloudflare 2.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 6,932; makefile: 138; sh: 76
file content (168 lines) | stat: -rwxr-xr-x 5,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""Cloudflare API code - example"""

import os
import sys

sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

def main():
    """Cloudflare API code - example"""

    cf = CloudFlare.CloudFlare()

    print('USER:')
    # grab the user info
    try:
        user = cf.user.get()
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/user.get %d %s - api call failed' % (e, e))
    except Exception as e:
        exit('/user.get - %s - api call failed' % (e))
    for kk in sorted(user.keys()):
        if isinstance(user[kk], list):
            if isinstance(user[kk][0], dict):
                print('\t%-40s =' % (kk))
                for ll in user[kk]:
                    for jj in sorted(ll.keys()):
                        if isinstance(ll[jj], list):
                            print('\t%-40s   %s = [ %s ]' % ('', jj, ', '.join(ll[jj])))
                        else:
                            print('\t%-40s   %s = %s' % ('', jj, ll[jj]))
            else:
                print('\t%-40s = [ %s ]' % (kk, ', '.join(user[kk])))
        elif isinstance(user[kk], dict):
            print('\t%-40s =' % (kk))
            for jj in sorted(user[kk].keys()):
                print('\t%-40s   %s = %s' % ('', jj, user[kk][jj]))
        else:
            print('\t%-40s = %s' % (kk, user[kk]))
    print('')

    print('ORGANIZATIONS:')
    # grab the user organizations info
    try:
        organizations = cf.user.organizations.get()
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/user.organizations.get %d %s - api call failed' % (e, e))
    if len(organizations) == 0:
        print('\tNo organization')
    for organization in organizations:
        organization_name = organization['name']
        organization_id = organization['id']
        organization_status = organization['status']
        print('\t%-40s %-10s %s' % (organization_id, organization_status, organization_name))
    print('')

    print('INVITES:')
    # grab the user invites info
    try:
        invites = cf.user.invites.get()
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/user.invites.get %d %s - api call failed' % (e, e))
    if len(invites) == 0:
        print('\tNo user invites')
    for invite in invites:
        invited_member_id = invite['invited_member_id']
        invited_member_email = invite['invited_member_email']
        organization_id = invite['organization_id']
        organization_name = invite['organization_name']
        invited_by = invite['invited_by']
        invited_on = invite['invited_on']
        expires_on = invite['expires_on']
        status = invite['status']
        print('\t %s %s %s %s %s %s %s %s' % (
            organization_id,
            status,
            invited_member_id,
            invited_member_email,
            organization_name,
            invited_by,
            invited_on,
            expires_on
        ))
    print('')

    print('BILLING:')
    # grab the user billing profile info
    try:
        profile = cf.user.billing.profile.get()
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/user.billing.profile.get %d %s - api call failed' % (e, e))
    profile_id = profile['id']
    profile_first = profile['first_name']
    profile_last = profile['last_name']
    profile_company = profile['company'] if 'company' in profile else ''
    if profile_company is None:
        profile_company = ''

    if profile['payment_email'] != '':
        payment_email = profile['payment_email']
        card_number = None
        card_expiry_year = None
        card_expiry_month = None
    else:
        payment_email = None
        card_number = profile['card_number']
        card_expiry_year = profile['card_expiry_year']
        card_expiry_month = profile['card_expiry_month']

    if payment_email is not None:
        print('\t %s %s %s %s PayPal: %s' % (
            profile_id,
            profile_first,
            profile_last,
            profile_company,
            payment_email
        ))
    else:
        if card_number is None:
            card_number = '---- ---- ----- ----'
        if card_expiry_year is not None and card_expiry_month is not None:
            card_expiry = card_expiry_month + '/' + card_expiry_year
        else:
            card_expiry = '--/--'
        print('\t %s %s %s %s CC: %s %s' % (
            profile_id,
            profile_first,
            profile_last,
            profile_company,
            card_number,
            card_expiry
        ))

    print('')

    print('BILLING HISTORY:')
    # grab the user billing history info
    try:
        history = cf.user.billing.history.get()
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/user.billing.history.get %d %s - api call failed' % (e, e))
    if len(history) == 0:
        print('\tNo billing history')
    for h in sorted(history, key=lambda v: v['occurred_at']):
        history_id = h['id']
        history_type = h['type']
        history_action = h['action']
        history_occurred_at = h['occurred_at']
        history_amount = h['amount']
        history_currency = h['currency']
        history_description = h['description']
        print('\t %s %s %s %s %s %s %s' % (
            history_id,
            history_type,
            history_action,
            history_occurred_at,
            history_amount,
            history_currency,
            history_description
        ))

    print('')

    exit(0)

if __name__ == '__main__':
    main()