{"id":186,"date":"2022-01-13T11:18:38","date_gmt":"2022-01-13T03:18:38","guid":{"rendered":"https:\/\/idez.biz\/?p=186"},"modified":"2022-01-13T11:18:39","modified_gmt":"2022-01-13T03:18:39","slug":"batching-azure-graph-api-requests-in-python","status":"publish","type":"post","link":"https:\/\/idez.biz\/index.php\/batching-azure-graph-api-requests-in-python\/","title":{"rendered":"Batching Azure Graph API requests in Python"},"content":{"rendered":"\n<p class=\"has-text-align-justify\">For advanced users of Azure Graph API, the volume of requests can be huge. And calling the API once for every single request incurs network overheads and will slow down the process if there are huge amount of data to download. Microsoft introduces <a href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/json-batching\">combining the requests via JSON requests<\/a> to help speed up the process. <\/p>\n\n\n\n<p>To use this in python, you can use the sample code below, the access_token is the token that you gotten from authenticating :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import urllib3\r\n\nurl_new = f\"https:\/\/graph.microsoft.com\/v1.0\/$batch\"\r\nheaders = {\r\n        'Content-type': 'application\/json',\r\n        'Authorization': 'Bearer ' + access_token }\r\n\r\nresponse = requests.request(\"POST\", url_new, headers=headers, data=request)\r\n<\/code><\/pre>\n\n\n\n<p>For the data, it will be the requests in JSON format like below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>request = '''{\n    \"requests\": &#91;\n        {\n            \"url\": \"\/me?$select=displayName,jobTitle,userPrincipalName\",\n            \"method\": \"GET\",\n            \"id\": \"1\"\n        },\n        {\n            \"url\": \"\/me\/messages?$filter=importance eq 'high'&amp;$select=from,subject,receivedDateTime,bodyPreview\",\n            \"method\": \"GET\",\n            \"id\": \"2\"\n        },\n        {\n            \"url\": \"\/me\/events\",\n            \"method\": \"GET\",\n            \"id\": \"3\"\n        }\n    ]\n}'''<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">As you will notice that the url in the requests are relative url, that means that it will call the API based on the version specified in the batch URL. In the above example, it will be referring to the v1.0 API calls. If you need the beta version of the API, simply change the v1.0 in the url_new to beta.<\/p>\n\n\n\n<p>The response obtained will contain a list of responses, with individual status code and their responses. You may parse them accordingly.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For advanced users of Azure Graph API, the volume of requests can be huge. And calling the API once for every single request incurs network overheads and will slow down the process if there are huge amount of data to download. Microsoft introduces combining the requests via JSON requests to help speed up the process. &#8230; <a title=\"Batching Azure Graph API requests in Python\" class=\"read-more\" href=\"https:\/\/idez.biz\/index.php\/batching-azure-graph-api-requests-in-python\/\" aria-label=\"Read more about Batching Azure Graph API requests in Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,64],"tags":[16,66,65,41],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-azure","category-graph-api","tag-azure","tag-batching","tag-graph-api","tag-python"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcj45d-30","_links":{"self":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":1,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":187,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions\/187"}],"wp:attachment":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}