· 9 years ago · May 25, 2016, 08:57 AM
1DEFAULT_XML_ACCOUNT = '<?xml version="1.0" encoding="UTF-8"?> ' \
2 '<account sysid="%(api_key)s" password="%(api_secret)s" max-binds="2" ' \
3 ' max-mt-per-second="100" max-mo-per-second="2" smpp-enabled="false" ' \
4 'use-http="true" ' \
5 ' http-mo-base-url="" http-dn-base-url="" http-post-username="" ' \
6 'http-post-password="" ' \
7 ' http-post-method="get" encoding="latin9"> <mo-queue enabled="true" ' \
8 'mo-required="true" ' \
9 ' delivery-receipts-required="true" queue-size="50" ' \
10 'discard-when-queue-full="false" /> ' \
11 '<quota enabled="%(quota)s" pricing-group-id="" mo-quota-enabled="%(' \
12 'mo_quota)s" /> ' \
13 '<banned banned="false" reason="" /> ' \
14 '<routing group-id="" /> ' \
15 '<dlr-format message-id-is-in-hex="false" /> ' \
16 '<special-capabilities> ' \
17 ' <internal can-specify-explicit-message-id="false" /> ' \
18 ' <automatically-ack-mo-and-dlr enabled="false" /> ' \
19 ' <custom-so-timeout value="0" /> ' \
20 ' <custom-mo-window-size value="0" /> ' \
21 ' <tlv can-specify-explicit-network-code="false" /> ' \
22 ' <smpp-nack include-message-id="true" /> ' \
23 ' <capabilities>%(capabilities)s</capabilities> ' \
24 '</special-capabilities> ' \
25 '<restrictions>%(restrictions)s</restrictions> ' \
26 '<security sign-mo-and-dlr-http-requests="false" ' \
27 ' require-signed-http-submissions="false" secret-key="" /> ' \
28 '<capacity-thresholds>%(thresholds)s</capacity-thresholds>' \
29 '<time-created>%(creation_date)s</time-created> ' \
30 '<time-of-last-activity>%(creation_date)s</time-of-last-activity> ' \
31 '<time-last-modified>%(creation_date)s</time-last-modified> ' \
32 '</account>'
33
34def create_d_user(api_key,
35 api_host=DEFAULT_API_HOST,
36 fraud_score="GOOD",
37 email_password=None,
38 dry_run=False,
39 verbose=True,
40 registration_phone_country="US",
41 registration_country="US",
42 number=nexmo_random.random_number(),
43 start_index=1,
44 first_name=None,
45 last_name=None,
46 mail_domain=DEFAULT_MAIL_DOMAIN,
47 credit=2,
48 quota="true",
49 mo_quota="true",
50 capabilities='',
51 restrictions='',
52 thresholds='<threshold id="max-concurrent-calls" max="1000" />'):
53 creation_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
54 uid = uuid.uuid4()
55 api_key = "{}{}{}".format(api_key, uid.hex[:8], nexmo_config.User.POSTFIX)
56 print('Creating user %s' % api_key)
57 api_secret = api_key
58
59 email = '%s@%s' % (api_key, mail_domain)
60 email_password = email_password if email_password is not None else "123456A"
61
62 m = hashlib.md5()
63 m.update(email_password)
64 email_password_md5 = m.hexdigest()
65
66 fn = first_name if first_name is not None else api_key
67 ln = last_name if last_name is not None else DEFAULT_LASTNAME
68
69 # Make provisioning call
70 xml = DEFAULT_XML_ACCOUNT % locals()
71 if verbose:
72 print('HTTP request: %s' % (DEFAULT_PROV_URL % locals()))
73 if not dry_run:
74 prov = requests.get(DEFAULT_PROV_URL % locals())
75 if 'OK' not in prov.text:
76 print('Error calling provisioning API\nReq: %s\nResp: %s' %
77 (DEFAULT_PROV_URL % locals(), str(prov.text)))
78 return False
79
80 # Make quota call
81 if verbose:
82 print('HTTP request: %s' % (DEFAULT_QUOTA_URL % locals()))
83 if not dry_run:
84 quota = requests.get(DEFAULT_QUOTA_URL % locals())
85 quota_json = quota.json()
86 if quota_json.get('result-code', -1) != 0:
87 print('Error calling quota API\nReq: %s\nResp: %s' %
88 (DEFAULT_QUOTA_URL % locals(), str(quota_json)))
89 return False
90
91 # Make mysql call
92 mysql_conn = connector.connect(
93 user=DEFAULT_MYSQL_USER,
94 password=DEFAULT_MYSQL_PASSWORD,
95 host=DEFAULT_MYSQL_HOST,
96 database=DEFAULT_MYSQL_DATABASE)
97 if verbose:
98 print('Mysql query: %s' % (DEFAULT_MYSQL_QUERY % locals()))
99 if not dry_run:
100 cursor = mysql_conn.cursor()
101 cursor.execute(DEFAULT_MYSQL_QUERY % locals())
102 mysql_conn.commit()
103 cursor.close()
104
105
106 print json.dumps({"api_key": api_key,
107 "api_secret": api_secret,
108 "number": number,
109 "fraud_score": fraud_score,
110 "email": email,
111 "email_password": email_password,
112 "restrictions": restrictions,
113 }, sort_keys=True, indent=4, separators=(',', ': '))
114 return {"api_key": api_key,
115 "api_secret": api_secret,
116 "number": number,
117 "fraud_score": fraud_score,
118 "email": email,
119 "email_password": email_password,
120 "restrictions": restrictions,
121 }