I am trying to make an API call to Yelp's Fusion API. My calls work when hard coded. I am trying to get a list of businesses and then get a list of reviews for those businesses which requires two GETs. I'd like to step through the list of businesses and get their associated reviews. The following code results in a Send a complete request to the server
message when using the variable form. Hard coding a business ID value works fine. Not sure what the challenge is. (Newbie question so my code is probably not the best either)
import http.client
import json
conn = http.client.HTTPSConnection("api.yelp.com")
headers = {
'authorization': "Bearer <access token value>",
'cache-control': "no-cache",
'postman-token': "<token value>"
}
#This request works fine
conn.request("GET", "/v3/businesses/search?latitude=40.8059518&longitude=-73.9657435&limit=10&radius=200&term=restaurant", headers=headers)
res = conn.getresponse()
data = res.read()
yelp_result = json.loads(data.decode("utf-8"))
all_businesses = []
for business in yelp_result['businesses']:
b_name = business['name']
b_id = business['id']
rurl = "/v3/businesses/" + b_id + "/reviews"
#This is the request resulting in error given earlier
conn.request("GET",rurl,headers=headers)
all_businesses.append((b_id, b_name))
Challenge with the
conn.request
call seems to be that it also needs correspondingconn.getresponse()
andres.read()
calls. Doesn't like to open a connection if nothing is going to be done with it. (Would love it if someone had more insightful reason for this). Here's how to make a call to get limited number of businesses within a radius of a location (lat/long) and then also incorporate review snippets as part of that businesses info (not returned in the new Fusion API)