Skip to content

Commit 7512c7f

Browse files
authored
Updated library
1 parent e2f752c commit 7512c7f

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import http.client
21
import json
32
import os
43
from pprint import pprint
@@ -11,21 +10,28 @@
1110
'''
1211

1312
# Add your Bing Entity Search subscription key and endpoint to your environment variables.
14-
subscriptionKey = os.environ['BING_ENTITY_SEARCH_SUBSCRIPTION_KEY']
15-
host = os.environ['BING_ENTITY_SEARCH_ENDPOINT']
16-
host = host.replace('https://', '')
17-
path = '/bing/v7.0/entities'
13+
subscription_key = os.environ['BING_ENTITY_SEARCH_SUBSCRIPTION_KEY']
14+
endpoint = os.environ['BING_ENTITY_SEARCH_ENDPOINT'] + '/bing/v7.0/entities'
1815

1916
# Entity you want to find
2017
query = 'italian restaurants near me'
2118

22-
# Construct a request
19+
# Construct the request
2320
mkt = 'en-US'
24-
params = '?mkt=' + mkt + '&q=' + urllib.parse.quote(query)
25-
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
26-
conn = http.client.HTTPSConnection(host)
27-
conn.request("GET", path + params, None, headers)
21+
query = 'sail'
22+
params = 'mkt=' + mkt + '&q=' + urllib.parse.quote(query)
23+
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
2824

29-
# Print response
30-
response = conn.getresponse()
25+
# Call the API
26+
try:
27+
response = requests.get(endpoint, headers=headers, params=params)
28+
response.raise_for_status()
29+
30+
print("\nHeaders:\n")
31+
print(response.headers)
32+
33+
print("\nJSON Response:\n")
34+
pprint(response.json())
35+
except Exception as ex:
36+
raise ex
3137
pprint(json.loads(response.read()))

0 commit comments

Comments
 (0)