Skip to content

Instantly share code, notes, and snippets.

@xbns
Last active November 18, 2020 15:04
Show Gist options
  • Save xbns/1dc496a00ea484783404711833443846 to your computer and use it in GitHub Desktop.
Save xbns/1dc496a00ea484783404711833443846 to your computer and use it in GitHub Desktop.
create random user and order via API
import requests
import names
import random
import json
token = ''
#program_id = ''
program_id = '2ba0d47c-dea6-11e8-b2b3-06f77de3a7f0' # Test Program
location_id = '09942cd5-f100-42c3-a175-cd620cd3bc7c' # Kilimani
def get_auth_token(token, program_id):
endpoint = 'https://testapi.tulaa.io/auth/login'
payload = {
'username': '<my-email>', # change this!
'password': '<my-password>', # your password
'grant_type': 'password',
'client_id': 'web_app'
}
headers = {
'Authorization': token,
'programId': program_id
}
response = requests.post(endpoint, headers=headers, data=payload)
# For successful API call, response code will be 200 (OK)
if(response.ok):
jData = json.loads(response.content)
#program_id = jData['payload']['user']['ownerProgramId']
for key in jData:
if key == 'access_token' in jData:
# print(str(jData[key])) # cast type object to string
token = 'Bearer'+' '+str(jData[key])
# print(token)
# print('\n')
# print(program_id)
return [token, program_id]
else:
# If response code is not ok (200), print the resulting http error code with description
print(response.text)
def random_N_digits(n):
number = str(''.join(random.sample("0123456789", n)))
#print(number)
def create_user_order(token, program_id):
token, program_id = get_auth_token(token, program_id)
user_order_endpoint = 'https://testapi.tulaa.io/order/orders'
# random first_name,last_name
first_name = names.get_first_name()
last_name = names.get_last_name()
# random age
low = 20
high = 50
random_age = random.uniform(low, high)
# random msisdn
random_msisdn = '+2547'+str(random_N_digits(8))
random_nationalId = str(random_N_digits(8))
# random input number
random_inputs = random.randint(1, 3)
# random gender
gender_list = ['F', 'M']
ran_gender = random.randint(0, 1)
payload = {
"programId": program_id,
"currencyId": "KES",
"loanApplied": "true",
"orderClientReference": "",
"inputPacks": [
{
"id": "8e454517-0190-40a0-9399-24fa7d44de0a",
"quantity": "1",
"inputs": [
{
"id": "51ba2868-cf62-443f-977c-f9b956712f15",
"quantity": random_inputs
},
{
"id": "03ea5788-26e7-459f-8685-f7553a204525",
"quantity": random_inputs
},
{
"id": "d167557c-d188-482d-84e3-661a228c84ab",
"quantity": random_inputs
}
]
}
],
"consignor": {
"firstName": first_name,
"lastName": last_name,
"age": random_age,
"msisdn": random_msisdn,
"ownerProgramId": program_id,
"locationId": location_id,
"languageId": "en",
"currency": "KES",
"alertLang": "en",
"nationalId": random_nationalId,
"nationalIdType": "NATIONAL_ID",
"landSize": 2,
"gender": gender_list[ran_gender],
"roleId": "b6167260-41f4-11e7-8ae4-0a8e71f5672c"
},
"consignee": {
"firstName": first_name,
"lastName": last_name,
"age": random_age,
"msisdn": random_msisdn,
"ownerProgramId": program_id,
"locationId": location_id,
"languageId": "en",
"currency": "KES",
"alertLang": "en",
"nationalId": random_nationalId,
"nationalIdType": "NATIONAL_ID",
"landSize": 2,
"gender": gender_list[ran_gender],
"roleId": "b6167260-41f4-11e7-8ae4-0a8e71f5672c"
}
}
# print(json.dumps(payload))
headers = {
'Authorization': token,
'programId': program_id
}
response = requests.post(
user_order_endpoint, headers=headers, data=json.dumps(payload))
if (response.ok):
print(response.text)
#print('Created User and Order Successful')
else:
print(response.text)
if __name__ == '__main__':
create_user_order(token, program_id)
#get_auth_token(token, program_id)
"""
# Call create_user_order 120 times to create 120 users and orders
for _ in range(120):
create_user_order(token,program_id)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment