Created
July 17, 2017 16:08
-
-
Save 4lberto/16df76021f3ceca27d33f8df59011190 to your computer and use it in GitHub Desktop.
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
#!/usr/local/bin/python | |
# coding: latin-1 | |
import requests | |
import json | |
import time | |
import random | |
#******************** DISPO (AUTH) ******************************************** | |
print ("*") * 40 + " Authorization " + ("*"*40) | |
def getToken(basic_auth, username, password): | |
start = time.time() | |
url_k = 'https://CENSORED/api/auth/realms/commercial_platform/protocol/openid-connect/token' | |
headers_k = {'Authorization': 'Basic ' + basic_auth} | |
payload_k = {'grant_type': 'client_credentials', 'username': username, 'password':password, 'login_hint':'CENSORED'} | |
responseToken = requests.post(url_k, headers = headers_k, data=payload_k) | |
result = json.loads(responseToken.text).get('access_token') | |
print'Elapsed Time in this step %s' % (time.time() - start) | |
return result | |
basicAuth = 'CENSORED' | |
token = getToken(basicAuth,'CENSORED','CENSORED') | |
#print "Token from function: %s" % token | |
#******************** DISPO (AVM) ******************************************** | |
print ("*") * 40 + " DISPO (AVM) " + ("*"*40) | |
def getDispo(origin, destination, date, paxnum, token): | |
print "Asking for flights from %s to %s at %s for %s passengers..." % (origin, destination, date, paxnum) | |
start = time.time() | |
avm_url = 'https://CENSORED/api/sse-avm/rs/v2/availability' | |
headers_avm = {'Authorization': 'Bearer ' + token, 'Accept-Language':'es-ES'} | |
payload_avm = {"slices":[{"origin":origin.upper(),"destination":destination.upper(),"date":date}],"passengers":[{"passengerType":"ADULT","count":paxnum}],"maxStopNumber":0,"marketCode": "ES"} | |
response_avm = requests.post(avm_url, headers = headers_avm, json=payload_avm) | |
print 'Response AVM %r' % response_avm | |
result = json.loads(response_avm.text) | |
print'Elapsed Time in this step %s' % (time.time() - start) | |
return result | |
origins = ['AGP', 'MAD','BIO','BCN'] | |
destinations = ['PMI','LIS','NYC','LON'] | |
for x in xrange(0,10): | |
origin = random.choice(origins) | |
destination = random.choice(destinations) | |
dispo=getDispo(origin, destination, '2017-10-22', 2, token) | |
fileName = "AVM" + str(time.time()) +".txt" | |
text_file = open(fileName, "w") | |
text_file.write(json.dumps(dispo)) | |
text_file.close() | |
print "Guardadas %s ofertas en %s" % (len(dispo["offers"]), fileName) | |
#********************* FARE ********************** | |
print ("*") * 40 + " FARE (AVM) " + ("*"*40) | |
respondeId_fare = dispo["responseId"] | |
offerItemId_fare = dispo["offers"][0]["offerItems"][0]["offerItemId"] | |
fare_url = 'https://CENSORED/api/sse-avm/rs/v2/fare' | |
headers_fare = {'Authorization': 'Bearer ' + token, 'Accept-Language':'es-ES'} | |
payload_fare = {"offerItemIds":[offerItemId_fare],"originDestinations":[{"originDestinationId":"OD1","selectedSliceId":0}],"responseId":respondeId_fare} | |
response_fare = requests.post(fare_url, headers = headers_fare, json=payload_fare) | |
fare_avm = json.loads(response_fare.text) | |
print "Fare: %r" % fare_avm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test e2e python a API Rest