Example #1
0
        internal static async Task <Dictionary <string, IEnumerable <T> > > GetObjectCollectionBatchedAsync <T>(HttpClient httpClient, string accessToken, string[] lookupData, string urlTemplate)
        {
            Dictionary <string, IEnumerable <T> > returnValue = new Dictionary <string, IEnumerable <T> >();

            Dictionary <string, string> requests = new Dictionary <string, string>();
            var batch = new GraphBatch();
            int id    = 0;

            foreach (var item in lookupData)
            {
                id++;
                var url = string.Format(urlTemplate, item);
                batch.Requests.Add(new GraphBatchRequest()
                {
                    Id = id.ToString(), Method = "GET", Url = $"{url}"
                });
                requests.Add(id.ToString(), item);
            }
            var stringContent = new StringContent(JsonSerializer.Serialize(batch));

            stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            var result = await GraphHelper.PostAsync <GraphBatchResponse>(httpClient, "v1.0/$batch", stringContent, accessToken);

            if (result.Responses != null && result.Responses.Any())
            {
                var options = new JsonSerializerOptions()
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                };
                foreach (var response in result.Responses)
                {
                    var itemId = requests.First(r => r.Key == response.Id).Value;
                    if (response.Body.TryGetValue("value", out object resultObject))
                    {
                        var objectElement = (JsonElement)resultObject;
                        returnValue.Add(itemId, JsonSerializer.Deserialize <T[]>(objectElement.ToString(), options));
                    }
                }
            }
            return(returnValue);
        }
Example #2
0
        internal static async Task <Dictionary <string, string> > GetPropertyBatchedAsync(HttpClient httpClient, string accessToken, string[] lookupData, string urlTemplate, string property)
        {
            Dictionary <string, string> returnValue = new Dictionary <string, string>();

            Dictionary <string, string> requests = new Dictionary <string, string>();
            var batch = new GraphBatch();
            int id    = 0;

            foreach (var item in lookupData)
            {
                id++;
                var url = string.Format(urlTemplate, item);
                batch.Requests.Add(new GraphBatchRequest()
                {
                    Id = id.ToString(), Method = "GET", Url = $"{url}?$select={property}"
                });
                requests.Add(id.ToString(), item);
            }
            var stringContent = new StringContent(JsonSerializer.Serialize(batch));

            stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            var result = await GraphHelper.PostAsync <GraphBatchResponse>(httpClient, "v1.0/$batch", stringContent, accessToken);

            if (result.Responses != null && result.Responses.Any())
            {
                foreach (var response in result.Responses)
                {
                    var userId = requests.First(r => r.Key == response.Id).Value;
                    if (response.Body.TryGetValue(property, out object webUrlObject))
                    {
                        var element = (JsonElement)webUrlObject;
                        returnValue.Add(userId, element.GetString());
                    }
                }
            }
            return(returnValue);
        }