|
1 | | -import http.client |
2 | 1 | import json |
3 | 2 | import os |
4 | 3 | from pprint import pprint |
|
11 | 10 | ''' |
12 | 11 |
|
13 | 12 | # 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' |
18 | 15 |
|
19 | 16 | # Entity you want to find |
20 | 17 | query = 'italian restaurants near me' |
21 | 18 |
|
22 | | -# Construct a request |
| 19 | +# Construct the request |
23 | 20 | 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} |
28 | 24 |
|
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 |
31 | 37 | pprint(json.loads(response.read())) |
0 commit comments