/// <summary> /// Deserializes a single directory object string based on the odata type. /// </summary> /// <typeparam name="T">Graph object to be deserialized into.</typeparam> /// <param name="dictionaryToken">JSON dictionary token.</param> /// <returns>Deserialized directory object.</returns> public static T DeserializeGraphObject <T>(JToken dictionaryToken, Type resultObjectType) where T : GraphObject { GraphObject graphObject = JsonConvert.DeserializeObject( dictionaryToken.ToString(), resultObjectType ?? typeof(T), new AadJsonConverter()) as GraphObject; // Make sure that every object returned is of type GraphObject. // The library does not understand any other type. Debug.Assert(graphObject != null); graphObject.TokenDictionary = dictionaryToken; graphObject.PropertiesMaterializedFromDeserialization = new List <string>(graphObject.ChangedProperties); // Clear all the properties being tracked for update. graphObject.ChangedProperties.Clear(); T returnObject = graphObject as T; if (returnObject == null) { string message = string.Format( CultureInfo.InvariantCulture, "Unexpected type {0} obtained in response. Only objects of type {1} are expected.", graphObject.GetType(), typeof(T)); throw new GraphException(message); } return(returnObject); }
public virtual PagedResults <GraphObject> GetLinkedObjects( GraphObject graphObject, LinkProperty linkProperty, string nextPageToken, int top) { Utils.ValidateGraphObject(graphObject, "graphObject"); Uri objectUri = Utils.GetRequestUri( this, graphObject.GetType(), graphObject.ObjectId, nextPageToken, top, Utils.GetLinkName(linkProperty)); if (this.returnBatchItem.Value) { this.batchRequestItems.Value.Add( new BatchRequestItem(HttpVerb.GET, true, objectUri, null, null)); return(null); } byte[] rawResponse = this.ClientConnection.DownloadData(objectUri, null); PagedResults <GraphObject> pagedResults = SerializationHelper.DeserializeJsonResponse <GraphObject>( Encoding.UTF8.GetString(rawResponse), objectUri); return(pagedResults); }
/// <summary> /// Gets the uri for listing the directory objects. /// </summary> /// <param name="parent">Parent object if the list is for containment type.</param> /// <param name="graphConnection">Call context.</param> /// <param name="nextLink">Link to the next set of results.</param> /// <param name="objectType">Directory object type.</param> /// <param name="filter">Filter expression generator.</param> /// <returns>Uri for listing the directory objects.</returns> /// <exception cref="UriFormatException">Invalid format for next link.</exception> /// <exception cref="ArgumentNullException">Invalid call context or object type.</exception> public static Uri GetListUri( GraphObject parent, Type objectType, GraphConnection graphConnection, string nextLink, FilterGenerator filter) { Utils.ThrowIfNullOrEmpty(graphConnection, "graphConnection"); Utils.ThrowIfNullOrEmpty(objectType, "objectType"); if (filter == null) { // Generate a dummy filter // Makes it easy to add the api-version parameter. filter = new FilterGenerator(); } // Get the entity attribute. EntityAttribute entityAttribute = Utils.GetCustomAttribute <EntityAttribute>(objectType, true); // Build a base uri for both paged and non paged queries. UriBuilder uriBuilder; string baseUri = graphConnection.AadGraphEndpoint; if (!String.IsNullOrEmpty(nextLink)) { string formattedNextLink = String.Format( CultureInfo.InvariantCulture, "{0}/{1}", baseUri, nextLink); uriBuilder = new UriBuilder(formattedNextLink); } else { if (parent != null) { baseUri = String.Format( "{0}/{1}/{2}", baseUri, Utils.GetCustomAttribute <EntityAttribute>(parent.GetType(), true).SetName, parent.ObjectId); } baseUri = String.Format( CultureInfo.InvariantCulture, "{0}/{1}", baseUri, entityAttribute.SetName); uriBuilder = new UriBuilder(baseUri); } // add the delta link to the uri uriBuilder.AddParameter(Constants.DeltaLinkQueryKeyName, graphConnection.DeltaToken); filter[Constants.QueryParameterNameApiVersion] = graphConnection.GraphApiVersion; Utils.BuildQueryFromFilter(uriBuilder, filter); return(uriBuilder.Uri); }
public virtual void DeleteLink( GraphObject sourceObject, GraphObject targetObject, LinkProperty linkProperty) { Utils.ValidateGraphObject(sourceObject, "sourceObject"); Uri deleteUri; bool isSingleValued = Utils.GetLinkAttribute(sourceObject.GetType(), linkProperty).IsSingleValued; if (isSingleValued) { // If the link is single valued, the target object id should not be part of the Uri. deleteUri = Utils.GetRequestUri( this, sourceObject.GetType(), sourceObject.ObjectId, Constants.LinksFragment, Utils.GetLinkName(linkProperty)); } else { Utils.ValidateGraphObject(targetObject, "targetObject"); deleteUri = Utils.GetRequestUri( this, sourceObject.GetType(), sourceObject.ObjectId, Constants.LinksFragment, Utils.GetLinkName(linkProperty), targetObject.ObjectId); } if (this.returnBatchItem.Value) { this.batchRequestItems.Value.Add(new BatchRequestItem(HttpVerb.DELETE, true, deleteUri, null, null)); return; } this.ClientConnection.DeleteRequest(deleteUri); }
public virtual void AddLink( GraphObject sourceObject, GraphObject targetObject, LinkProperty linkProperty) { Utils.ValidateGraphObject(sourceObject, "sourceObject"); Utils.ValidateGraphObject(targetObject, "targetObject"); Uri linkUri = Utils.GetRequestUri( this, sourceObject.GetType(), sourceObject.ObjectId, Constants.LinksFragment, Utils.GetLinkName(linkProperty)); Uri targetObjectUri = Utils.GetRequestUri( this, targetObject.GetType(), targetObject.ObjectId); Dictionary <string, string> postParameters = new Dictionary <string, string>() { { "url", targetObjectUri.ToString() } }; string requestJson = JsonConvert.SerializeObject(postParameters); bool isSingleValued = Utils.GetLinkAttribute(sourceObject.GetType(), linkProperty).IsSingleValued; string methodName = isSingleValued ? HttpVerb.PUT : HttpVerb.POST; if (this.returnBatchItem.Value) { this.batchRequestItems.Value.Add(new BatchRequestItem(methodName, true, linkUri, null, requestJson)); return; } this.ClientConnection.UploadString( linkUri, methodName, requestJson, null, null); }
public virtual void Delete(GraphObject graphObject) { Utils.ValidateGraphObject(graphObject, "graphObject"); Uri deleteUri = Utils.GetRequestUri( this, graphObject.GetType(), graphObject.ObjectId); if (this.returnBatchItem.Value) { this.batchRequestItems.Value.Add(new BatchRequestItem(HttpVerb.DELETE, true, deleteUri, null, null)); return; } this.ClientConnection.DeleteRequest(deleteUri); }
/// <summary> /// Set the deferred stream property. /// </summary> /// <param name="graphObject">Graph object.</param> /// <param name="graphProperty">Property name.</param> /// <param name="memoryStream">Memory stream.</param> /// <param name="contentType">Content type.</param> public virtual void SetStreamProperty( GraphObject graphObject, GraphProperty graphProperty, MemoryStream memoryStream, string contentType) { Utils.ValidateGraphObject(graphObject, "graphObject"); Utils.ThrowIfNullOrEmpty(memoryStream, "memoryStream"); Utils.ThrowIfNullOrEmpty(contentType, "contentType"); Uri requestUri = Utils.GetRequestUri( this, graphObject.GetType(), graphObject.ObjectId, Utils.GetPropertyName(graphProperty)); WebHeaderCollection additionalHeaders = new WebHeaderCollection(); additionalHeaders[HttpRequestHeader.ContentType] = contentType; this.ClientConnection.UploadData(requestUri, HttpVerb.PUT, memoryStream.ToArray(), additionalHeaders); }
/// <summary> /// Gets the object that needs to be sent to graph containing only updated properties. /// </summary> /// <param name="graphObject">Graph object to be enquired for changed properties.</param> /// <returns>List of key value pairs of changed property names and values.</returns> public static IDictionary <string, object> GetSerializableGraphObject(GraphObject graphObject) { Utils.ThrowIfNull(graphObject, "graphObject"); IDictionary <string, object> serializableGraphObject = new Dictionary <string, object>(graphObject.ChangedProperties.Count); foreach (string changedProperty in graphObject.ChangedProperties) { PropertyInfo propertyInfo = graphObject.GetType().GetProperty(changedProperty); JsonPropertyAttribute jsonPropertyName = Utils.GetCustomAttribute <JsonPropertyAttribute>(propertyInfo, true); serializableGraphObject.Add(jsonPropertyName.PropertyName, propertyInfo.GetValue(graphObject, null)); } return(serializableGraphObject); }
public virtual void DeleteContainment(GraphObject parent, GraphObject containment) { Utils.ThrowIfNullOrEmpty(parent, "parent"); Utils.ThrowIfNullOrEmpty(containment, "containment"); Utils.ThrowArgumentExceptionIfNullOrEmpty(parent.ObjectId, "parent.ObjectId"); Utils.ThrowArgumentExceptionIfNullOrEmpty(containment.ObjectId, "containment.ObjectId"); Uri deleteUri = Utils.GetRequestUri( this, parent.GetType(), parent.ObjectId, containment.GetType(), containment.ObjectId); if (this.returnBatchItem.Value) { this.batchRequestItems.Value.Add(new BatchRequestItem(HttpVerb.DELETE, true, deleteUri, null, null)); return; } this.ClientConnection.DeleteRequest(deleteUri); }
public virtual GraphObject GetContainment(GraphObject parent, Type containmentType, string containmentObjectId) { Uri listUri = Utils.GetRequestUri(this, parent.GetType(), parent.ObjectId, containmentType, containmentObjectId); if (this.returnBatchItem.Value) { this.batchRequestItems.Value.Add(new BatchRequestItem(HttpVerb.GET, false, listUri, null, null)); return(null); } Logger.Instance.Info("Retrieving {0}", listUri); byte[] rawResponse = this.ClientConnection.DownloadData(listUri, null); PagedResults <GraphObject> pagedResults = SerializationHelper.DeserializeJsonResponse <GraphObject>( Encoding.UTF8.GetString(rawResponse), listUri); return(pagedResults.Results.FirstOrDefault()); }
/// <summary> /// Add or update the directory object on the cloud. /// </summary> /// <param name="parent">Parent object type for containment type.</param> /// <param name="containment">Containment object.</param> /// <param name="isCreate">Is this create or update?</param> /// <returns>Created base entity object.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="parent"/> or <paramref name="containment"/> is <see langword="null" /> /// </exception> private GraphObject AddOrUpdateContainment(GraphObject parent, GraphObject containment, bool isCreate) { Utils.ThrowIfNullOrEmpty(parent, "parent"); Utils.ThrowIfNullOrEmpty(containment, "containment"); containment.ValidateProperties(isCreate); Uri createUri = Utils.GetRequestUri( this, parent.GetType(), parent.ObjectId, containment.GetType(), isCreate ? String.Empty : containment.ObjectId); string requestJson = JsonConvert.SerializeObject(Utils.GetSerializableGraphObject(containment)); string methodName = isCreate ? HttpVerb.POST : HttpVerb.PATCH; if (this.returnBatchItem.Value) { this.batchRequestItems.Value.Add(new BatchRequestItem(methodName, true, createUri, null, requestJson)); return(null); } string responseJson = this.ClientConnection.UploadString( createUri, methodName, requestJson, null, null); PagedResults <GraphObject> pagedResults = SerializationHelper.DeserializeJsonResponse <GraphObject>(responseJson, createUri); if (pagedResults == null || pagedResults.Results == null || pagedResults.Results.Count != 1) { throw new InvalidOperationException("Unable to deserialize the response"); } containment.ChangedProperties.Clear(); pagedResults.Results[0].ChangedProperties.Clear(); return(pagedResults.Results[0]); }
/// <summary> /// Get the deferred stream property. /// </summary> /// <param name="graphObject">Graph object.</param> /// <param name="graphProperty">Property name.</param> /// <returns>Memory stream for the byte buffer.</returns> /// <param name="acceptType">Accept type header value.</param> public virtual Stream GetStreamProperty( GraphObject graphObject, GraphProperty graphProperty, string acceptType) { Utils.ValidateGraphObject(graphObject, "graphObject"); Utils.ThrowIfNullOrEmpty(acceptType, "acceptType"); Uri requestUri = Utils.GetRequestUri( this, graphObject.GetType(), graphObject.ObjectId, Utils.GetPropertyName(graphProperty)); WebHeaderCollection additionalHeaders = new WebHeaderCollection(); additionalHeaders[HttpRequestHeader.ContentType] = acceptType; byte[] buffer = this.ClientConnection.DownloadData(requestUri, additionalHeaders); if (buffer != null) { return(new MemoryStream(buffer)); } return(new MemoryStream()); }