/// <summary> /// Post a single document with the possibility to specify a different type /// for the new document object returned in the response. /// </summary> /// <typeparam name="T">The type of the post object used to record a new document.</typeparam> /// <typeparam name="U">Type of the returned document, only applies when /// <see cref="PostDocumentsQuery.ReturnNew"/> or <see cref="PostDocumentsQuery.ReturnOld"/> /// are used.</typeparam> /// <param name="collectionName"></param> /// <param name="document"></param> /// <param name="query"></param> /// <param name="serializationOptions">The serialization options. When the value is null the /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param> /// <returns></returns> public virtual async Task <PostDocumentResponse <U> > PostDocumentAsync <T, U>( string collectionName, T document, PostDocumentsQuery query = null, ApiClientSerializationOptions serializationOptions = null) { string uriString = _docApiPath + "/" + WebUtility.UrlEncode(collectionName); if (query != null) { uriString += "?" + query.ToQueryString(); } var content = GetContent(document, serializationOptions); using (var response = await _client.PostAsync(uriString, content)) { if (response.IsSuccessStatusCode) { var stream = await response.Content.ReadAsStreamAsync(); return(DeserializeJsonFromStream <PostDocumentResponse <U> >(stream)); } throw await GetApiErrorException(response); } }
/// <summary> /// Partially updates documents, the documents to update are specified /// by the _key attributes in the body objects. All attributes from the /// patch documents will be added to the existing documents if they do /// not yet exist, and overwritten in the existing documents if they do /// exist there. /// Setting an attribute value to null in the patch documents will cause a /// value of null to be saved for the attribute by default. /// If ignoreRevs is false and there is a _rev attribute in a /// document in the body and its value does not match the revision of /// the corresponding document in the database, the precondition is /// violated. /// PATCH /_api/document/{collection} /// </summary> /// <typeparam name="T">Type of the patch object used to partially update documents.</typeparam> /// <typeparam name="U">Type of the returned documents, only applies when /// <see cref="PatchDocumentsQuery.ReturnNew"/> or <see cref="PatchDocumentsQuery.ReturnOld"/> /// are used.</typeparam> /// <param name="collectionName"></param> /// <param name="patches"></param> /// <param name="query"></param> /// <param name="serializationOptions">The serialization options. When the value is null the /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param> /// <returns></returns> public virtual async Task <PatchDocumentsResponse <U> > PatchDocumentsAsync <T, U>( string collectionName, IList <T> patches, PatchDocumentsQuery query = null, ApiClientSerializationOptions serializationOptions = null) { string uri = _docApiPath + "/" + WebUtility.UrlEncode(collectionName); if (query != null) { uri += "?" + query.ToQueryString(); } var content = GetContent(patches, serializationOptions); using (var response = await _client.PatchAsync(uri, content)) { if (response.IsSuccessStatusCode) { if (query != null && query.Silent.HasValue && query.Silent.Value) { return(PatchDocumentsResponse <U> .Empty()); } else { var stream = await response.Content.ReadAsStreamAsync(); return(DeserializeJsonFromStream <PatchDocumentsResponse <U> >(stream)); } } throw await GetApiErrorException(response); } }
/// <summary> /// Partially updates the document identified by document-handle. /// The body of the request must contain a JSON document with the /// attributes to patch(the patch document). All attributes from the /// patch document will be added to the existing document if they do not /// yet exist, and overwritten in the existing document if they do exist /// there. /// PATCH/_api/document/{document-handle} /// </summary> /// <typeparam name="T">Type of the patch object used to partially update a document.</typeparam> /// <typeparam name="U">Type of the returned document, only applies when /// <see cref="PatchDocumentQuery.ReturnNew"/> or <see cref="PatchDocumentQuery.ReturnOld"/> /// are used.</typeparam> /// <param name="documentId"></param> /// <param name="body"></param> /// <param name="query"></param> /// <param name="serializationOptions">The serialization options. When the value is null the /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param> /// <returns></returns> public virtual async Task <PatchDocumentResponse <U> > PatchDocumentAsync <T, U>( string documentId, T body, PatchDocumentQuery query = null, ApiClientSerializationOptions serializationOptions = null) { ValidateDocumentId(documentId); string uriString = _docApiPath + "/" + documentId; if (query != null) { uriString += "?" + query.ToQueryString(); } var content = GetContent(body, serializationOptions); using (var response = await _client.PatchAsync(uriString, content)) { if (response.IsSuccessStatusCode) { var stream = await response.Content.ReadAsStreamAsync(); return(DeserializeJsonFromStream <PatchDocumentResponse <U> >(stream)); } throw await GetApiErrorException(response); } }
/// <summary> /// Replaces the document with the provided document ID with the one in /// the body, provided there is such a document and no precondition is /// violated. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="documentId"></param> /// <param name="doc"></param> /// <param name="serializationOptions">The serialization options. When the value is null the /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param> /// <returns></returns> public virtual async Task <PutDocumentResponse <T> > PutDocumentAsync <T>( string documentId, T doc, PutDocumentQuery opts = null, ApiClientSerializationOptions serializationOptions = null) { ValidateDocumentId(documentId); string uri = _docApiPath + "/" + documentId; if (opts != null) { uri += "?" + opts.ToQueryString(); } var content = GetContent(doc, serializationOptions); using (var response = await _client.PutAsync(uri, content)) { if (response.IsSuccessStatusCode) { var stream = await response.Content.ReadAsStreamAsync(); return(DeserializeJsonFromStream <PutDocumentResponse <T> >(stream)); } throw await GetApiErrorException(response); } }
/// <summary> /// Creates an edge in an existing graph. /// The edge must contain a _from and _to value /// referencing valid vertices in the graph. /// The edge has to conform to the definition of the edge collection it is added to. /// POST /_api/gharial/{graph}/edge/{collection} /// </summary> /// <typeparam name="T">The type of the edge to create. /// Must contain valid _from and _to properties once serialized. /// <c>null</c> properties are preserved during serialization.</typeparam> /// <param name="graphName">The name of the graph.</param> /// <param name="collectionName">The name of the edge collection the edge belongs to.</param> /// <param name="edge">The edge to create.</param> /// <param name="query">Optional query parameters of the request.</param> /// <param name="serializationOptions">The serialization options. When the value is null the /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param> /// <returns></returns> public virtual async Task <PostEdgeResponse <T> > PostEdgeAsync <T>( string graphName, string collectionName, T edge, PostEdgeQuery query = null, ApiClientSerializationOptions serializationOptions = null) { var content = GetContent(edge, serializationOptions); string uri = _graphApiPath + "/" + WebUtility.UrlEncode(graphName) + "/edge/" + WebUtility.UrlEncode(collectionName); if (query != null) { uri += "?" + query.ToQueryString(); } using (var response = await _transport.PostAsync(uri, content)) { if (response.IsSuccessStatusCode) { var stream = await response.Content.ReadAsStreamAsync(); return(DeserializeJsonFromStream <PostEdgeResponse <T> >(stream)); } throw await GetApiErrorException(response); } }
protected byte[] GetContent <T>(T item, ApiClientSerializationOptions serializationOptions) { try { return(_serialization.Serialize(item, serializationOptions)); } catch (Exception e) { throw new SerializationException($"A serialization error occured while preparing a request for Arango. See InnerException for more details.", e); } }
/// <summary> /// Post a single document. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collectionName"></param> /// <param name="document"></param> /// <param name="query"></param> /// <param name="serializationOptions">The serialization options. When the value is null the /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param> /// <returns></returns> public virtual Task <PostDocumentResponse <T> > PostDocumentAsync <T>( string collectionName, T document, PostDocumentsQuery query = null, ApiClientSerializationOptions serializationOptions = null) { return(PostDocumentAsync <T, T>( collectionName, document, query, serializationOptions)); }
/// <summary> /// Replace multiple documents. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collectionName"></param> /// <param name="documents"></param> /// <param name="query"></param> /// <param name="serializationOptions">The serialization options. When the value is null the /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param> /// <param name="headers">The <see cref="DocumentHeaderProperties"/> values.</param> /// <returns></returns> public virtual async Task <PutDocumentsResponse <T> > PutDocumentsAsync <T>( string collectionName, IList <T> documents, PutDocumentsQuery query = null, ApiClientSerializationOptions serializationOptions = null, DocumentHeaderProperties headers = null) { string uri = _docApiPath + "/" + WebUtility.UrlEncode(collectionName); if (query != null) { uri += "?" + query.ToQueryString(); } var content = GetContent(documents, serializationOptions); var headerCollection = GetHeaderCollection(headers); using (var response = await _client.PutAsync(uri, content, headerCollection).ConfigureAwait(false)) { if (response.IsSuccessStatusCode) { if (query != null && query.Silent.HasValue && query.Silent.Value) { return(PutDocumentsResponse <T> .Empty()); } else { var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); return(DeserializeJsonFromStream <PutDocumentsResponse <T> >(stream)); } } throw await GetApiErrorException(response).ConfigureAwait(false); } }