Created
September 22, 2015 17:51
-
-
Save ariestiyansyah/0f8aa380b599cb9d49b0 to your computer and use it in GitHub Desktop.
Python script to demo all interaction functions of the API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import base64 | |
| import requests | |
| import json | |
| import random | |
| import pprint | |
| import urllib | |
| import base64 | |
| from dateutil.parser import parse | |
| from sys import argv | |
| script, username, password = argv | |
| base = 'http://api.kreditusaha.com' | |
| #username = 'username' | |
| #password = 'password' | |
| basicauth = requests.auth.HTTPBasicAuth(username, password) | |
| headers = {'Content-type': 'application/json'} | |
| pp = pprint.PrettyPrinter(indent=4) | |
| # get personal information | |
| print('>>>>>> Agent Data >>>>>>>>>>>>>>>>>>>>') | |
| agent = requests.get(base+'/api/users/'+username, auth=basicauth) | |
| agent_val = agent.json() | |
| print(agent_val) | |
| # get client roles | |
| print('>>>>>> Client Roles >>>>>>>>>>>>>>>>>>>>') | |
| cr = requests.get(base+'/api/clienttypes', params=[('for', 'CLIENTS' )], auth=basicauth) | |
| client_roles = cr.json() | |
| print(client_roles) | |
| # get clients for agents --> no request headers | |
| print('>>>>>> Clients of Agent >>>>>>>>>>>>>>>>>>>>') | |
| clients = requests.get(base+'/api/clients', params=[('creditOfficerUsername', username )], auth=basicauth) | |
| clients_val = clients.json() | |
| print(clients_val) | |
| # lookup for client per mobile phone number | |
| mobile = "7123686239" | |
| print('>>>>> Check for mobile >>>>>>>>>>>>>>>>>>>>>'+mobile) | |
| data = json.dumps( | |
| { | |
| "filterConstraints": [ | |
| { | |
| "filterSelection": "MOBILE_PHONE_NUMBER", | |
| "filterElement": "EQUALS", | |
| "value": mobile | |
| } | |
| ] | |
| }) | |
| mobuser = requests.post(base+'/api/clients/search', data, auth=basicauth, headers=headers) | |
| mob_user_val = mobuser.json() | |
| print(mob_user_val) | |
| # create client | |
| # not possible now -> validate if user already in Mambu (?) | |
| print('>>>>> Check for Non Jovi >>>>>>>>>>>>>>>>>>>>>') | |
| user = requests.get(base+'/api/clients', params=[('lastName', 'Non Jovi' )], auth=basicauth) | |
| user_val = user.json()[0] | |
| print(user_val) | |
| # create client | |
| #print('>>>>> Create new Client >>>>>>>>>>>>>>>>>>>>>') | |
| #out = urllib2.urlopen(base+'/api/clients/8a08c1984fd74ddf014fde8feb684ce0') | |
| #validate = out.read() | |
| #print(validate); | |
| # update client : copy of Non Jovi | |
| # TODO : check for more than one | |
| print('>>>>> Update Existing Client >>>>>>>>>>>>>>>>>>>>> '+user_val["lastName"]) | |
| data = json.dumps( | |
| { | |
| "client": | |
| { | |
| "encodedKey": user_val["encodedKey"], | |
| "firstName": user_val["firstName"], | |
| "lastName": user_val["lastName"], | |
| "birthDate": user_val["birthDate"], | |
| #Here is the update -> the rest above is mandatory | |
| "homePhone":str(random.randrange(1000000, 9900999, 2)) | |
| }, | |
| "customInformation":[ | |
| { | |
| "value":"Testscript", | |
| "customFieldID":"LEAD_SRC" | |
| } | |
| ] | |
| }) | |
| r = requests.post(base+'/api/clients/'+user_val["encodedKey"], data, auth=basicauth, headers=headers) | |
| print r.text.encode('utf8') | |
| # switch client role : lookup from earlier call | |
| # should only be possible for the person doing the credit assessment? | |
| print('>>>>> Update Existing Clienttype >>>>>>>>>>>>>>>>>>>>> '+user_val["lastName"]) | |
| user_role = ''; | |
| for role in client_roles: | |
| if role["name"] == "Client": | |
| user_role = role["encodedKey"] | |
| data = json.dumps( | |
| { | |
| "client": | |
| { | |
| "encodedKey": user_val["encodedKey"], | |
| "firstName": user_val["firstName"], | |
| "lastName": user_val["lastName"], | |
| "birthDate": user_val["birthDate"], | |
| #Here is the update -> the rest above is mandatory | |
| "clientRole": | |
| { | |
| "encodedKey": user_role | |
| } | |
| } | |
| }) | |
| print data | |
| r = requests.post(base+'/api/clients/'+user_val["encodedKey"], data, auth=basicauth, headers=headers) | |
| print r.text.encode('utf8') | |
| # get loan products | |
| print('>>>>> Get Loan Products >>>>>>>>>>>>>>>>>>>>>') | |
| products = requests.get(base+'/api/loanproducts/', auth=basicauth) | |
| products_val = products.json() | |
| product_loan = {} | |
| for product in products_val: | |
| if product["activated"] : | |
| print product["productName"]+":"+product["id"] | |
| if product["id"] == 'SME_WC_BE' : | |
| product_loan = product; | |
| # get loan schedule | |
| print('>>>>> Get Loan Schedule on >>>>>>>>>>>>>>>>>>>>> '+product_loan["productName"]) | |
| data = [ | |
| ("loanAmount", 21000000), | |
| ("firstRepaymentDate", "2015-10-30"), | |
| ("anticipatedDisbursement", "2015-10-23"), | |
| ("repaymentInstallments",6), | |
| ] | |
| schedules = requests.get(base+'/api/loanproducts/'+product_loan["id"]+'/schedule', params=data, auth=basicauth) | |
| schedules_val = schedules.json() | |
| print("date \t principal due \t interest due") | |
| for item in schedules_val["repayments"]: | |
| print parse(item["dueDate"]).strftime('%d/%m/%Y') + " \t " + item["principalDue"] + " \t " + item["interestDue"] | |
| # create loan contract (partial) | |
| print('>>>>> Create Loan Contract >>>>>>>>>>>>>>>>>>>>>') | |
| # lookup standard loan | |
| loan_product = requests.get(base+'/api/loanproducts/SME_WC_IND', auth=basicauth); | |
| loan_product_val = loan_product.json() | |
| data = json.dumps( | |
| { | |
| "loanAccount" : | |
| { | |
| "loanName" : "Testloan from testscript", | |
| "accountHolderKey" : user_val["encodedKey"], | |
| "accountHolderType" : "CLIENT", | |
| "productTypeKey" : loan_product_val["encodedKey"], | |
| "loanAmount" : loan_product_val["defaultLoanAmount"], | |
| "interestRate" : loan_product_val["defaultInterestRate"], | |
| "repaymentInstallments" : loan_product_val["defaultNumInstallments"] | |
| } | |
| }) | |
| #pp.pprint(data); | |
| #r = requests.post(base+'/api/loans/', data, auth=basicauth, headers=headers) | |
| #print r.text.encode('utf8') | |
| #loan_val = r.json() | |
| # upload attachments | |
| print('>>>>> Add Attachments to Loan contract >>>>>>>>>>>>>>>>>>>>>') | |
| link = "http://www.snee.com/xml/xslt/sample.doc" | |
| f = urllib.urlopen(link) | |
| attachment = f.read() | |
| filedata = base64.encodestring(attachment); | |
| print(' > File downloaded and encoded.') | |
| data = json.dumps( | |
| { | |
| "document":{ | |
| "documentHolderKey" : user_val["encodedKey"], | |
| "documentHolderType": "CLIENT", | |
| "name" : "sample_document", | |
| "type" : "doc" | |
| }, | |
| "documentContent" : filedata | |
| }) | |
| r = requests.post(base+'/api/documents', data, auth=basicauth, headers=headers) | |
| print r.text.encode('utf8') | |
| # set loan contract final for submission | |
| print('>>>>> Submit Loan Contract >>>>>>>>>>>>>>>>>>>>>') | |
| #r = requests.post(base+'/api/loans/'loan_id, data, auth=basicauth, headers=headers) | |
| #print r.text.encode('utf8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment