Tuesday, July 18, 2017
Wednesday, July 12, 2017
Script to curl request and read data from curl response(API)
7/12/2017:
import csv
import json
import urllib, urllib2
from urllib2 import Request, urlopen, URLError, HTTPError
import subprocess
#E2 in the file name is due to, I just need to make use of test data created for E2
input_file=open('json_curl_request_6_30_E2.csv','r')
output_file=open('json_curl_response_6_30_E2.csv','wt')
Reader1=csv.reader(input_file)
Writer1=csv.writer(output_file)
for row in Reader1:
#Parameterizing
var1=str(row[0])
var2=row[1]
headers = {'a1-Type': 'a2/json','Accept': '*/*','a3': 'CRDA','a4': 'PHONE','a5': 'US','a6': 'USD', 'c': var1 }
jsonurl = "<url>"
jsonapi="{\"request_type_code\":"+" \""+var1+"\""+",\"requested_entities\":[{\"app_annual_revenue_amount\":"+ var2+",\"c2\":"+var1+"}],\"unique_id_type\":"+" \""+unique_id_type+"\""+"}"
#print(jsonapi)
# double quotes jsonapi(hardcoded) is not working
# single quote jsonapi(hardcoded) is working
row[0]=var1
row[1]=var2
curl1="curl -X POST --header 'c:" +var1' --header ......"
jsonapireq="'"+jsonapi+"'"
row[3]=curl1+jsonapireq+" 'https://s'"
try:
req= urllib2.Request(jsonurl,jsonapi,headers)
resp= urllib2.urlopen(req)
response = json.loads(resp.read())
row[20]=response
except urllib2.HTTPError as err:
print(err.code)
#Are we printing error in response.csv file, especially HTTPError
#As I do not see Internal server error related responses are not logged in response.csv file
Writer1.writerow(row)
input_file.close()
output_file.close()
-------------- working
Wednesday, July 5, 2017
Comments and considering special characters
\ ---- for multiple lines of string
\" --- for ignoring "
Thursday, May 18, 2017
Reading directly curl request and posting request
http://stackoverflow.com/questions/17301938/making-a-request-to-a-restful-api-using-python
Friday, May 12, 2017
Parametrization in json
https://en.wikibooks.org/wiki/Python_Programming/Variables_and_Strings ---- teaches how to use variables and strings in python with examples.
Sunday, April 23, 2017
Access and Read Excel using Python
https://automatetheboringstuff.com/chapter12/ ---- this website is really useful, it has theoretical information
http://stackoverflow.com/questions/17977584/how-to-read-data-from-excel-and-write-it-to-text-file-line-by-line ------ This has code written to open and read from excel
http://stackoverflow.com/questions/17977584/how-to-read-data-from-excel-and-write-it-to-text-file-line-by-line ------ This has code written to open and read from excel
Friday, April 21, 2017
Automation scenarios
1. Automation Scenario#1:
- Parameterizing hive querying using python or shell scripting
- Read data(multiple rows) from Excel and put it in fields in API request
- Run multiple API requests
- Read data in the fields of API response and put it in excel
- Read entire data from excel and use it in hive query
- Run hive queries
- Read data from hive query output and put it in excel
Connecting Hive from Python??
http://stackoverflow.com/questions/21370431/how-to-access-hive-via-python
Subscribe to:
Posts (Atom)