Posts

Making an API call and unit testing in Python

 This post is about mocking functions which have api calls. Consider the following python function which makes an API call to the url www.testurl.com import requests def api_call_demo ( name , telephone ): # add queryParams (Replace this with your query params) queryParams = { 'name' : name , 'address' : telephone , } retry = 1 retry_counter = 3 # Replace this with the proper url url = "www.testurl.com" for retry in range ( 1 , retry_counter): try : # make a http get call response = requests.get(url , params =queryParams , timeout = 200.0 , ) # request error except ( ConnectionError , ConnectionResetError ) as cerr: print ( f"Received error { cerr } for { url } on try { retry } " , file =sys.stderr) if retry == retry_counter