public void EntityConverter_ConvertLinkedEntityJsonToObject_Succeeds() { string json = ApiResponseCleaner.GetJsonObject(JsonFileReader.GetJsonFromFile("Response_Json_Object_SalesInvoice.txt")); var converter = new EntityConverter(); var invoice = converter.ConvertJsonToObject <SalesInvoice>(json); var lines = (List <SalesInvoiceLine>)invoice.SalesInvoiceLines; Assert.IsNotNull(invoice); Assert.IsTrue(lines.Count > 0); }
/// <summary> /// Get entity using specific GUID /// </summary> /// <param name="guid">Global Unique Identifier of the entity</param> /// <param name="parameters">parameters</param> /// <returns>Entity if exists. Null if entity not exists.</returns> public T GetEntity(string guid, string parameters) { if (guid.Contains('}') || guid.Contains('{')) { throw new Exception("Bad Guid: Guid cannot contain '}' or '{'"); } // Convert the resonse to an object of the specific type var response = _conn.GetEntity(_keyname, guid, parameters); response = ApiResponseCleaner.GetJsonObject(response); var ec = new EntityConverter(); var entity = ec.ConvertJsonToObject <T>(response); // If entity isn't managed already, add entity to EntityController AddEntityToManagedEntitiesCollection(entity); return(entity); }
/// <summary> /// Creates an entity in Exact Online /// </summary> /// <param name="entity">Entity to create</param> /// <returns>True if succeed</returns> public Boolean Create(ref T entity) { var supportedActions = GetSupportedActions(entity); if (!supportedActions.CanCreate) { throw new Exception("Cannot create entity. Entity does not support creation. Please see the Reference Documentation."); } // Get Json code var created = false; var converter = new EntityConverter(); var emptyEntity = Activator.CreateInstance <T>(); var json = converter.ConvertObjectToJson(emptyEntity, entity, _entityControllerDelegate); // Send to API var response = _conn.Post(json); if (!response.Contains("error")) { created = true; // Set values of API in account entity (to ensure GUID is set) response = ApiResponseCleaner.GetJsonObject(response); var ec = new EntityConverter(); entity = ec.ConvertJsonToObject <T>(response); // Try to add the entity to the managed entities collections if (!AddEntityToManagedEntitiesCollection(entity)) { throw new Exception("This entity already exists"); } // Check if the endpoint supports a read action. Some endpoints such as PrintQuotation only support create (POST). if (supportedActions.CanRead) { // Get entity with linked entities (API Response for creating does not return the linked entities) entity = GetEntity(GetIdentifierValue(entity), _expandfield); } } return(created); }
/// <summary> /// Creates an entity in Exact Online /// </summary> /// <param name="entity">Entity to create</param> /// <returns>True if succeed</returns> public Boolean Create(ref T entity) { if (!IsCreateable(entity)) { throw new Exception("Cannot create entity. Entity does not support creation. Please see the Reference Documentation."); } // Get Json code var created = false; var converter = new EntityConverter(); var emptyEntity = Activator.CreateInstance <T>(); var json = converter.ConvertObjectToJson(emptyEntity, entity, _entityControllerDelegate); // Send to API var response = _conn.Post(json); if (!response.Contains("error")) { created = true; // Set values of API in account entity (to ensure GUID is set) response = ApiResponseCleaner.GetJsonObject(response); var ec = new EntityConverter(); entity = ec.ConvertJsonToObject <T>(response); // Try to add the entity to the managed entities collections if (!AddEntityToManagedEntitiesCollection(entity)) { throw new Exception("This entity already exists"); } /* IGNORE the additional GET request until it is resolved properly by the API team (https://github.com/exactonline/exactonline-api-dotnet-client/issues/9) * // Get entity with linked entities (API Response for creating does not return the linked entities) * entity = GetEntity(GetIdentifierValue(entity), _expandfield); */ } return(created); }
public void EntityConverter_ConvertLinkedEntityJsonToObject_Succeeds() { string json = ApiResponseCleaner.GetJsonObject(JsonFileReader.GetJsonFromFile("Response_Json_Object_SalesInvoice.txt")); var converter = new EntityConverter(); var invoice = converter.ConvertJsonToObject<SalesInvoice>(json); var lines = (List<SalesInvoiceLine>)invoice.SalesInvoiceLines; Assert.IsNotNull(invoice); Assert.IsTrue(lines.Count > 0); }