May-30-2018, 02:37 PM
Hi everyone, I am very new to coding let alone python...!!! Any help is appreciated.
I have managed to piece together the code below to test the response times of our API. I am having issues extracting data from the xml response.
I am trying to extract the string ResponseTime="337" from the xml response, the number in the "" changes but the rest is the same. When I run the code I see the following.
c:\unscan>python dir-uapitest.py
End point : uAPI Direct APAC VIP
Request 0 took 1.305 seconds
uAPI Time [(u'"452"', u'2')]
Request 1 took 0.795 seconds
uAPI Time [(u'"349"', u'9')]
Request 2 took 0.819 seconds
uAPI Time [(u'"366"', u'6')]
End point : uAPI Direct APAC VIP
Sending request WITH connection pooling ...
Request 0 took 0.719 seconds
uAPI Time [(u'"281"', u'1')]
Request 1 took 0.473 seconds
uAPI Time [(u'"352"', u'2')]
Request 2 took 0.395 seconds
uAPI Time [(u'"268"', u'8')]
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<air:LowFareSearchRsp TraceId="roventrace" TransactionId="AB53869A0A0759CF79A986CC276D3293" ResponseTime="337" DistanceUnits="MI" CurrencyType="INR" xmlns:air="http://www.travelport.com/schema/air_v45_0" xmlns:common_v45_0="http://www.travelport.com/schema/common_v45_0">
<common_v45_0:ResponseMessage Code="4039" Type="Warning" ProviderCode="1G">"Result size exceeded the maximum allowable and some results were discarded. It may be necessary to narrow your search using search modifiers."</common_v45_0:ResponseMessage>
Full code
I have managed to piece together the code below to test the response times of our API. I am having issues extracting data from the xml response.
I am trying to extract the string ResponseTime="337" from the xml response, the number in the "" changes but the rest is the same. When I run the code I see the following.
c:\unscan>python dir-uapitest.py
End point : uAPI Direct APAC VIP
Request 0 took 1.305 seconds
uAPI Time [(u'"452"', u'2')]
Request 1 took 0.795 seconds
uAPI Time [(u'"349"', u'9')]
Request 2 took 0.819 seconds
uAPI Time [(u'"366"', u'6')]
End point : uAPI Direct APAC VIP
Sending request WITH connection pooling ...
Request 0 took 0.719 seconds
uAPI Time [(u'"281"', u'1')]
Request 1 took 0.473 seconds
uAPI Time [(u'"352"', u'2')]
Request 2 took 0.395 seconds
uAPI Time [(u'"268"', u'8')]
uapitime = re.findall(r'ResponseTime=("([^"])*")', r.text) print "uAPI Time %s" % (uapitime)XML Response
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<air:LowFareSearchRsp TraceId="roventrace" TransactionId="AB53869A0A0759CF79A986CC276D3293" ResponseTime="337" DistanceUnits="MI" CurrencyType="INR" xmlns:air="http://www.travelport.com/schema/air_v45_0" xmlns:common_v45_0="http://www.travelport.com/schema/common_v45_0">
<common_v45_0:ResponseMessage Code="4039" Type="Warning" ProviderCode="1G">"Result size exceeded the maximum allowable and some results were discarded. It may be necessary to narrow your search using search modifiers."</common_v45_0:ResponseMessage>
Full code
import xml.dom.minidom, time, requests, re uapikey = "xxxxxxxxxxxxxxxxxxxx" #Base 64 encoded Username and Password url = "https://apac.universal-api.travelport.com/B2BGateway/connect/uAPI/AirService" data = """ <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <air:LowFareSearchReq xmlns:air="http://www.travelport.com/schema/air_v45_0" AuthorizedBy="user" TargetBranch="xxxxxx" TraceId="paultrace" SolutionResult="true" xmlns:com="http://www.travelport.com/schema/common_v45_0"> <com:BillingPointOfSaleInfo OriginApplication="uAPI"/> <air:SearchAirLeg> <air:SearchOrigin> <com:CityOrAirport Code="BLR"/> </air:SearchOrigin> <air:SearchDestination> <com:CityOrAirport Code="DEL"/> </air:SearchDestination> <air:SearchDepTime PreferredTime="2018-07-17"> </air:SearchDepTime> </air:SearchAirLeg> <air:AirSearchModifiers MaxSolutions="10"> <air:PreferredProviders> <com:Provider Code="1G"/> </air:PreferredProviders> <air:PermittedCarriers> <com:Carrier Code="AI"/> </air:PermittedCarriers> </air:AirSearchModifiers> <com:SearchPassenger Code="ADT"/> <air:AirPricingModifiers FaresIndicator="AllFares" ETicketability="Yes"/> </air:LowFareSearchReq> </soap:Body> </soap:Envelope> """ headers1 = {'User-Agent':'TravelportIndia','Content-type':'text/xml;charset=\"utf-8\"','Content-length': '%d' % len(data),'Authorization':'Basic %s'%(uapikey),'Accept-Encoding':'gzip'} headers = {'User-Agent':'TravelportIndia','Content-type':'text/xml;charset=\"utf-8\"','Content-length': '%d' % len(data),'Authorization':'Basic %s'%(uapikey), 'Accept-Encoding':'gzip','Connection':'Keep-Alive'} # Without Connection Pooling print "End point : uAPI Direct APAC VIP " #print "Sending request without connection pooling ..." for x in range(0,250): starttime = time.time() r = requests.post(url, data=data, headers=headers1) endtime = time.time() uapitime = re.findall(r'ResponseTime=("([^"])*")', r.text) print "Request %s took %.3f seconds" % (str(x), endtime - starttime) print "uAPI Time %s" % (uapitime) #print r.headers #print r.text # With Connection pooling session = requests.Session() print "End point : uAPI Direct APAC VIP " print "Sending request WITH connection pooling ..." for x in range(0,250): starttime = time.time() r = session.post(url, data=data, headers=headers) endtime = time.time() uapitime = re.findall(r'ResponseTime=("([^"])*")', r.text) print "Request %s took %.3f seconds" % (str(x), endtime - starttime) print "uAPI Time %s" % (uapitime) #print r.headers #print r.text