Forum Discussion
Shubhorin1
1 year agoFrequent Visitor
Issue with 202 Status Code and Polling for Notebook Definition in Microsoft Fabric API
Hi Microsoft Fabric Team, I am working on a project where I need to fetch the definition of a notebook from a workspace using the Microsoft Fabric API. While making the API call to the endpoint http...
DaVinci-DB
1 year agoNew Member
Hi,
The code includes exponential backoff for polling and handles the 403 Forbidden response. Make sure to replace your_workspace_id, your_notebook_id, and your_access_token with your actual values.
import requests
import time
def get_notebook_definition(workspace_id, notebook_id, access_token):
url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/notebooks/{notebook_id}/getDefinition"
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
if response.status_code == 202:
location_url = response.headers.get("Location")
if not location_url:
raise Exception("Location-Header nicht im 202-Response gefunden")
# Polling mit exponentiellem Rückoff
wartezeit = 1 # Beginne mit 1 Sekunde
while True:
poll_response = requests.get(location_url, headers=headers)
if poll_response.status_code == 200:
return poll_response.json()
elif poll_response.status_code == 403:
raise Exception("403 Forbidden: Anfrage konnte nicht verarbeitet werden")
else:
time.sleep(wartezeit)
wartezeit = min(wartezeit * 2, 60) # Begrenze die Wartezeit auf 60 Sekunden
else:
response.raise_for_status()
# Beispielaufruf
workspace_id = "deine_workspace_id"
notebook_id = "deine_notebook_id"
access_token = "dein_access_token"
try:
notebook_definition = get_notebook_definition(workspace_id, notebook_id, access_token)
print(notebook_definition)
except Exception as e:
print(f"Fehler: {e}")