Skip to content

Instantly share code, notes, and snippets.

@vgrem
Last active November 7, 2018 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/868a6b50297003579fd79d5071692020 to your computer and use it in GitHub Desktop.
Save vgrem/868a6b50297003579fd79d5071692020 to your computer and use it in GitHub Desktop.
using Microsoft.Graph;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.Graph.GraphClient.JsonBatchSupport
{
public static class GraphServiceClientExtensions
{
public static async Task<List<object>> GetBatchAsync(this GraphServiceClient client, BatchRequest request)
{
var batchMessage = request.ToMessage(client);
await client.AuthenticationProvider.AuthenticateRequestAsync(batchMessage);
var response = await client.HttpProvider.SendAsync(batchMessage);
var content = await response.Content.ReadAsStringAsync();
var json = JObject.Parse(content);
var entities = json["responses"].Select(item =>
{
var queryId = (string)item["id"];
var entityPayload = JsonConvert.SerializeObject(item["body"]);
var subRequest = request[queryId];
var entity = JsonConvert.DeserializeObject(entityPayload, subRequest.Value);
return entity;
});
return entities.ToList();
}
}
public class BatchRequest
{
private Dictionary<string, IBaseRequest> _queriesTable = new Dictionary<string, IBaseRequest>();
private Dictionary<string,Type> _resultsTable = new Dictionary<string, Type>();
public KeyValuePair<IBaseRequest, Type> this[string queryId]
{
get
{
return new KeyValuePair<IBaseRequest,Type>(_queriesTable[queryId], _resultsTable[queryId]);
}
}
public void AddQuery(IBaseRequest query, Type entityType)
{
var queryId = Guid.NewGuid().ToString();
_resultsTable[queryId] = entityType;
_queriesTable[queryId] = query;
}
/// <summary>
/// Construct JSON batch request https://developer.microsoft.com/en-us/graph/docs/concepts/json_batching
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public HttpRequestMessage ToMessage(GraphServiceClient client)
{
var batchMessage = new HttpRequestMessage();
batchMessage.RequestUri = new Uri("https://graph.microsoft.com/v1.0/$batch");
batchMessage.Method = HttpMethod.Post;
batchMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
dynamic payload = new ExpandoObject();
payload.requests = _queriesTable.Select(kv =>
{
var message = kv.Value.GetHttpRequestMessage();
dynamic request = new ExpandoObject();
request.id = kv.Key;
request.method = message.Method.ToString();
request.url = message.RequestUri.AbsoluteUri.Replace(client.BaseUrl,string.Empty);
if(message.Content != null)
request.body = message.Content;
request.headers = message.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault());
return request;
});
var jsonPayload = client.HttpProvider.Serializer.SerializeObject(payload);
batchMessage.Content = new StringContent(jsonPayload,Encoding.UTF8,"application/json");
return batchMessage;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment