fix & refactor: REST samples endpoints update + load_dotenv usage

This commit is contained in:
v-ghajam 2024-06-05 13:00:50 +03:00
Родитель 50233495d1
Коммит 08d30be945
12 изменённых файлов: 171 добавлений и 80 удалений

13
.env.example Normal file
Просмотреть файл

@ -0,0 +1,13 @@
# Subscription Keys
BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY
BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY
BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY
BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY
BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY
BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY
BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY
BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY
BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY
# Optional
#BING_CUSTOM_SEARCH_CONFIG

2
.gitignore поставляемый
Просмотреть файл

@ -1,3 +1,5 @@
.env
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##

Просмотреть файл

@ -12,3 +12,5 @@ msrest
azure-common
msrestazure
azure-core
requests
python-dotenv

Просмотреть файл

@ -7,31 +7,39 @@ import json
import os
import requests
from pprint import pprint
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample uses the Bing Autosuggest API to check the spelling of query words and then suggests corrections.
Bing Spell Check API: https://docs.microsoft.com/en-us/bing/search-apis/bing-spell-check/overview
'''
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY'
# Add your Bing Autosuggest subscription key and endpoint to your environment variables.
subscription_key = os.environ['BING_AUTOSUGGEST_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_AUTOSUGGEST_ENDPOINT'] + '/bing/v7.0/Suggestions/'
# Add your Bing Autosuggest subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
# Construct the request
mkt = 'en-US'
endpoint = 'https://api.bing.microsoft.com/v7.0/suggestions'
query = 'sail'
mkt = 'en-US'
params = { 'q': query, 'mkt': mkt }
headers = { 'Ocp-Apim-Subscription-Key': subscription_key }
headers = { AUTH_HEADER_NAME: subscription_key }
# Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex

Просмотреть файл

@ -9,23 +9,30 @@ import json
import os
from pprint import pprint
import requests
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample uses the Bing Custom Search API to search for a query topic and get back user-controlled web page results.
Bing Custom Search API: https://docs.microsoft.com/en-us/bing/search-apis/bing-custom-search/overview
'''
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY'
# Add your Bing Custom Search subscription key and endpoint to your environment variables.
subscriptionKey = os.environ['BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_CUSTOM_SEARCH_ENDPOINT']
customConfigId = os.environ["BING_CUSTOM_CONFIG"] # you can also use "1"
searchTerm = "microsoft"
# Add your Bing Custom Search subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
customConfigId = os.environ.get('BING_CUSTOM_SEARCH_CONFIG', '1')
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
searchTerm = 'Microsoft'
# </importsAndVars>
# <url>
# Add your Bing Custom Search endpoint to your environment variables.
url = endpoint + "/bingcustomsearch/v7.0/search?q=" + searchTerm + "&customconfig=" + customConfigId
url = 'https://api.bing.microsoft.com/v7.0/custom/search?q=' + searchTerm + '&customconfig=' + customConfigId
# </url>
# <request>
r = requests.get(url, headers={'Ocp-Apim-Subscription-Key': subscriptionKey})
r = requests.get(url, headers={AUTH_HEADER_NAME: subscription_key})
pprint(json.loads(r.text))
# </request>

Просмотреть файл

@ -3,34 +3,41 @@ import os
from pprint import pprint
import requests
import urllib.parse
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample uses the Bing Entity Search v7 to search for restaurants and return details about it.
Bing Entity Search API: https://docs.microsoft.com/en-us/bing/search-apis/bing-entity-search/overview
'''
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY'
# Add your Bing Entity Search subscription key and endpoint to your environment variables.
subscription_key = os.environ['BING_ENTITY_SEARCH_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_ENTITY_SEARCH_ENDPOINT'] + '/bing/v7.0/entities'
# Add your Bing Entity Search subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
# Entity you want to find
query = 'italian restaurants near me'
query = 'alija izetbegović'
# Construct the request
endpoint = 'https://api.bing.microsoft.com/v7.0/entities'
mkt = 'en-US'
params = 'mkt=' + mkt + '&q=' + urllib.parse.quote(query)
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
headers = {AUTH_HEADER_NAME: subscription_key}
# Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex
pprint(json.loads(response.read()))

Просмотреть файл

@ -7,33 +7,42 @@ import json
import os
from pprint import pprint
import requests
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample makes a call to the Bing Image Search API with a text query and returns relevant images with data.
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-image-search/overview
'''
# Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_SEARCH_V7_ENDPOINT'] + "/bing/v7.0/images/search"
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY'
# Add your Bing Image Search V7 subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
# Query to search for
query = "puppies"
query = 'Arabian horse'
# Construct a request
endpoint = 'https://api.bing.microsoft.com/v7.0/images/search'
mkt = 'en-US'
params = {'q': query, 'mkt': mkt}
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
headers = {AUTH_HEADER_NAME: subscription_key}
# Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex

Просмотреть файл

@ -7,32 +7,39 @@ import json
import os
from pprint import pprint
import requests
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample makes a call to the Bing News Search API with a text query and returns relevant news webpages.
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-news-search/overview
'''
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY'
# Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_SEARCH_V7_ENDPOINT'] + "/bing/v7.0/news/search"
query = "Microsoft"
# Add your Bing News Search V7 subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
# Construct a request
endpoint = 'https://api.bing.microsoft.com/v7.0/news/search'
query = 'Microsoft'
mkt = 'en-US'
params = {'q': query, 'mkt': mkt}
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
headers = {AUTH_HEADER_NAME: subscription_key}
# Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex

Просмотреть файл

@ -6,23 +6,31 @@ import os
from pprint import pprint
import requests
import urllib.parse
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample uses the Bing Spell Check API to check the spelling of query words
and then suggests corrections with a scored confidence.
Bing Spell Check API: https://docs.microsoft.com/en-us/bing/search-apis/bing-spell-check/overview
'''
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY'
# Add your Bing Spell Check subscription key and endpoint to your environment variables.
key = os.environ['BING_SPELL_CHECK_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_SPELL_CHECK_ENDPOINT'] + '/bing/v7.0/spellcheck'
# Add your Bing Spell Check subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
# Query you want spell-checked.
query = 'Hollo, wrld!'
query = '''when i went two the houze i heared they'r'e voice and they're srcreams. I walk their and told: "helo fren"'''
# Construct request
params = urllib.parse.urlencode( { 'mkt': 'en-US', 'mode': 'proof', 'text': query } )
headers = { 'Ocp-Apim-Subscription-Key': key,
endpoint = 'https://api.bing.microsoft.com/v7.0/spellcheck'
params = { 'mkt': 'en-US', 'mode': 'proof', 'text': query }
headers = { AUTH_HEADER_NAME: subscription_key,
'Content-Type': 'application/x-www-form-urlencoded' }
# Optional headers
@ -33,13 +41,13 @@ headers = { 'Ocp-Apim-Subscription-Key': key,
# Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response = requests.post(endpoint, headers=headers, params=params)
response.raise_for_status()
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex

Просмотреть файл

@ -7,25 +7,34 @@ import json
import os
from pprint import pprint
import requests
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample makes a call to the Bing Video Search API with a topic query and returns relevant video with data.
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-video-search/overview
'''
# Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_SEARCH_V7_ENDPOINT'] + "/bing/v7.0/videos/search"
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY'
# Add your Bing Videos Search V7 subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
# Search query
query = "kittens"
query = 'kentucky derby'
# Construct a request
endpoint = 'https://api.bing.microsoft.com/v7.0/videos/search'
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscriptionKey
AUTH_HEADER_NAME: subscription_key
}
params = { "q": query }
params = { 'q': query }
# Call the API
try:
@ -33,10 +42,10 @@ try:
response.raise_for_status()
# Print results
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex

Просмотреть файл

@ -5,32 +5,42 @@ import json
import os
from pprint import pprint
import requests
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample uses the Bing Visual Search API with a local, query image and returns several web links
and data of the exact image and/or similar images.
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-visual-search/overview
'''
AUTH_HEADER_NAME='Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME='BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY'
# Add your Bing Search V7 subscriptionKey and endpoint to your environment variables.
endpoint = os.environ['BING_SEARCH_V7_ENDPOINT'] + '/bing/v7.0/images/visualsearch'
subscription_key = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
# Add your Bing Visual Search V7 subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise(RuntimeError(f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
image_path = 'MY-IMAGE' # for example: my_image.jpg
image_path = './my_image.jpg'
# Construct the request
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
file = {'image' : ('MY-IMAGE', open(image_path, 'rb'))} # MY-IMAGE is the name of the image file (no extention)
endpoint = 'https://api.bing.microsoft.com/v7.0/images/visualsearch'
headers = {AUTH_HEADER_NAME: subscription_key,
'Content-Type': 'multipart/form-data' }
file = {'image' : ('Image_name', open(image_path, 'rb'))} # Image_name is the name of the image
# Call the API
try:
response = requests.post(endpoint, headers=headers, files=file)
response.raise_for_status()
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex

Просмотреть файл

@ -7,33 +7,42 @@ import json
import os
from pprint import pprint
import requests
from dotenv import load_dotenv
# Load the environment variables from .env file
load_dotenv()
'''
This sample makes a call to the Bing Web Search API with a query and returns relevant web search.
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-web-search/overview
'''
AUTH_HEADER_NAME = 'Ocp-Apim-Subscription-Key'
SUBSCRIPTION_KEY_ENV_VAR_NAME = 'BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY'
# Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscription_key = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
endpoint = os.environ['BING_SEARCH_V7_ENDPOINT'] + "/v7.0/search"
# Add your Bing Web Search V7 subscription key to your environment variables / .env file
subscription_key = os.environ.get(SUBSCRIPTION_KEY_ENV_VAR_NAME)
if subscription_key is None:
raise (RuntimeError(
f'Please define the {SUBSCRIPTION_KEY_ENV_VAR_NAME} environment variable'))
# Query term(s) to search for.
query = "Microsoft"
query = 'Microsoft'
# Construct a request
endpoint = 'https://api.bing.microsoft.com/v7.0/search'
mkt = 'en-US'
params = { 'q': query, 'mkt': mkt }
headers = { 'Ocp-Apim-Subscription-Key': subscription_key }
params = {'q': query, 'mkt': mkt}
headers = {AUTH_HEADER_NAME: subscription_key}
# Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
print("\nHeaders:\n")
print('\nHeaders:\n')
print(response.headers)
print("\nJSON Response:\n")
print('\nJSON Response:\n')
pprint(response.json())
except Exception as ex:
raise ex