public void ApiResponseCleanerFetchJsonArrayWithEmptyLinkedEntitiesSucceeds()
        {
            const string expected = @"[{""BankAccounts"":[]}]";
            string       clean    = ApiResponseCleaner.GetJsonArray(JsonFileReader.GetJsonFromFile("ApiResponse_Json_Array_WithEmptyLinkedEntities.txt"));

            Assert.AreEqual(expected, clean);
        }
示例#2
0
        /// <summary>
        /// Gets specific collection of entities and return a skip token if there are more than
        /// 60 entities to be returned.
        /// </summary>
        /// <param name="query">oData query</param>
        /// <param name="token">The skip token if there are more results than could be retrieved
        /// via the REST API or <code>null</code></param>
        /// <returns>List of entity Objects</returns>
        public List <T> Get(string query, out string token)
        {
            // Get the response and convert it to a list of entities of the specific type
            string response = _conn.Get(query);

            // Search for skip token in json response
            dynamic json = JsonConvert.DeserializeObject(response);

            // This contains the url to the API call for the remaining entities including a skiptoken.
            string next = json["d"].__next;

            // Skiptoken has format "$skiptoken=xyz" in the url and we want to extract xyz.
            var match = Regex.Match(next ?? "", @"\$skiptoken=([^&#]*)");

            // Extract the skip token
            token = match.Success ? match.Groups[1].Value : null;

            // TODO: ApiResponseCleaner should extract Guid
            response = ApiResponseCleaner.GetJsonArray(response);

            var rc       = new EntityConverter();
            var entities = rc.ConvertJsonArrayToObjectList <T>(response);

            // If the entity isn't managed already, register to managed entity collection
            foreach (var entity in entities)
            {
                AddEntityToManagedEntitiesCollection(entity);
            }

            // Convert list
            return(entities.ConvertAll(x => x));
        }
		/// <summary>
		/// Returns the current user data
		/// </summary>
		/// <returns>Me entity</returns>
		public Me CurrentMe()
		{
			var conn = new ApiConnection(_apiConnector, _exactOnlineApiUrl + "current/Me");
			string response = conn.Get("$select=CurrentDivision");
			response = ApiResponseCleaner.GetJsonArray(response);
			var converter = new EntityConverter();
			var currentMe = converter.ConvertJsonArrayToObjectList<Me>(response);
			return currentMe.FirstOrDefault();
        }
 /// <summary>
 /// Returns the current user data
 /// </summary>
 /// <returns>Me entity</returns>
 public async Task<Me> CurrentMeAsync()
 {
     var conn = new ApiConnection(_apiConnector, _exactOnlineApiUrl + "current/Me");
     string response = await conn.GetAsync("$select=CurrentDivision").ConfigureAwait(false);
     response = ApiResponseCleaner.GetJsonArray(response);
     var converter = new EntityConverter();
     var currentMe = converter.ConvertJsonArrayToObjectList<Me>(response);
     return currentMe.FirstOrDefault();
 }
        public void ApiResponseCleaner_FetchJsonObject_WithEscapeCharacter_Succeeds()
        {
            const string sampleJsonResponse = @"{ ""d"": { ""Remarks"": ""\\escape test"" }}";

            var cleanedJson = ApiResponseCleaner.GetJsonObject(sampleJsonResponse);

            const string expectedCleanedJson = @"{""Remarks"":""\\escape test""}";

            Assert.AreEqual(expectedCleanedJson, cleanedJson);
        }
示例#6
0
        /// <summary>
        /// Returns a list of dynamic objects
        /// </summary>
        /// <param name="query">oData query</param>
        /// <returns></returns>
        public List <dynamic> GetDynamic(string query)
        {
            string response = _conn.Get(query);

            response = ApiResponseCleaner.GetJsonArray(response);

            var            converter = new EntityConverter();
            List <dynamic> list      = converter.ConvertJsonToDynamicObjectList(response);

            return(list);
        }
        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>
        /// Returns a list of dynamic objects
        /// </summary>
        /// <param name="query">oData query</param>
        /// <returns></returns>
        public async Task <List <dynamic> > GetDynamicAsync(string query)
        {
            string response = await _conn.GetAsync(query).ConfigureAwait(false);

            response = ApiResponseCleaner.GetJsonArray(response);

            var            converter = new EntityConverter();
            List <dynamic> list      = converter.ConvertJsonToDynamicObjectList(response);

            return(list);
        }
        public void EntityConverter_ConvertLinkedEntityJsonArrayToObjects_Succeeds()
        {
            string json                  = ApiResponseCleaner.GetJsonArray(JsonFileReader.GetJsonFromFile("Response_Json_Array_SalesInvoice_WithLinkedEntities.txt"));
            var    converter             = new EntityConverter();
            List <SalesInvoice> invoices = converter.ConvertJsonArrayToObjectList <SalesInvoice>(json);

            foreach (var invoice in invoices)
            {
                var sil = (List <SalesInvoiceLine>)invoice.SalesInvoiceLines;
                Assert.IsTrue(sil.Count > 0);
            }
        }
        public void EntityConverter_ConvertJsonToDynamicObjectCollection_Succeeds()
        {
            string json = JsonFileReader.GetJsonFromFile("Response_Json_Array_GLAccount.txt");

            json = ApiResponseCleaner.GetJsonArray(json);

            List <dynamic> list = _entityConverter.ConvertJsonToDynamicObjectList(json);

            if (list.Count < 2)
            {
                throw new AssertFailedException("The list list doesn't contain two entities");
            }
        }
示例#11
0
        private void DeleteData()
        {
            // Get GUID and set it in property for PUT and Delete functions
            string response = _conn.Get("$filter=Code+eq+'SDKTest123456789'");

            response = ApiResponseCleaner.GetJsonArray(response);
            var converter = new EntityConverter();

            dynamic dresponse = converter.ConvertJsonToDynamicObjectList(response);
            string  id        = dresponse[0].ID;

            _conn.Delete("ID", id);
        }
示例#12
0
        private void ParseObjectList()
        {
            var converter = new EntityConverter();

            for (int i = 0; i < 100; i++)
            {
                string         json     = ApiResponseCleaner.GetJsonArray(JsonArray);
                List <Account> accounts = converter.ConvertJsonArrayToObjectList <Account>(json);
                if (accounts.Count != 2)
                {
                    throw new Exception("The count of the list isn't equal to the actual list");
                }
            }
        }
示例#13
0
        public void ParseObjectList_LinkedEntities()
        {
            var converter = new EntityConverter();

            for (int i = 0; i < 100; i++)
            {
                string json = ApiResponseCleaner.GetJsonArray(LinkedEntities);
                List <SalesInvoice> invoices = converter.ConvertJsonArrayToObjectList <SalesInvoice>(json);

                foreach (var invoice in invoices)
                {
                    var sil = (List <SalesInvoiceLine>)invoice.SalesInvoiceLines;
                    Assert.IsTrue(sil.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);
        }
        public void EntityConverter_ConvertJsonToDynamicObject_WithCorrectJson_Succeeds()
        {
            string jsonsresponse = JsonFileReader.GetJsonFromFile("Response_Json_Object_GLAccount.txt");
            string json          = ApiResponseCleaner.GetJsonObject(jsonsresponse);

            dynamic glaccountObject = _entityConverter.ConvertJsonToDynamicObject(json);

            Assert.AreEqual("D", (string)glaccountObject.BalanceSide);
            Assert.AreEqual("W", (string)glaccountObject.BalanceType);
            Assert.AreEqual("4406", (string)glaccountObject.Code);
            Assert.AreEqual(false, (Boolean)glaccountObject.Compress);
            Assert.AreEqual(null, (string)glaccountObject.Costcenter);
            Assert.AreEqual(null, (string)glaccountObject.CostcenterDescription);
            Assert.AreEqual("10/25/2013 02:24:29", (string)glaccountObject.Created);
            Assert.AreEqual("99d87844-e4ef-4ac5-968c-fb863eaced16", (string)glaccountObject.Creator);
            Assert.AreEqual(null, (string)glaccountObject.CreatorFullName);
            Assert.AreEqual("Test", (string)glaccountObject.Description);
            Assert.AreEqual("499156", (string)glaccountObject.Division);
            Assert.AreEqual("0", (string)glaccountObject.ExcludeVATListing);
            Assert.AreEqual("0", (string)glaccountObject.ExpenseNonDeductiblePercentage);
            Assert.AreEqual("3c534e79-c4fe-44d2-9765-00b30573c2de", (string)glaccountObject.ID);
            Assert.AreEqual(false, (Boolean)glaccountObject.IsBlocked);
            Assert.AreEqual(false, (Boolean)glaccountObject.Matching);
            Assert.AreEqual("12/02/2013 13:44:27", (string)glaccountObject.Modified);
            Assert.AreEqual("10091f1b-4661-4854-9fd0-cf5f5f668cbd", (string)glaccountObject.Modifier);
            Assert.AreEqual("Edward Jackson", (string)glaccountObject.ModifierFullName);
            Assert.AreEqual(null, (string)glaccountObject.PrivateGLAccount);
            Assert.AreEqual("0", (string)glaccountObject.PrivatePercentage);
            Assert.AreEqual(null, (string)glaccountObject.ReportingCode);
            Assert.AreEqual(false, (Boolean)glaccountObject.RevalueCurrency);
            Assert.AreEqual("RENTE    ", (string)glaccountObject.SearchCode);
            Assert.AreEqual("120", (string)glaccountObject.Type);
            Assert.AreEqual("Other costs", (string)glaccountObject.TypeDescription);
            Assert.AreEqual("0", (string)glaccountObject.UseCostcenter);
            Assert.AreEqual("0", (string)glaccountObject.UseCostunit);
            Assert.AreEqual(null, (string)glaccountObject.VATCode);
            Assert.AreEqual(null, (string)glaccountObject.VATDescription);
            Assert.AreEqual(null, (string)glaccountObject.VATGLAccountType);
            Assert.AreEqual(null, (string)glaccountObject.VATNonDeductibleGLAccount);
            Assert.AreEqual("0", (string)glaccountObject.VATNonDeductiblePercentage);
            Assert.AreEqual(null, (string)glaccountObject.YearEndCostGLAccount);
            Assert.AreEqual(null, (string)glaccountObject.YearEndReflectionGLAccount);
        }
        /// <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>
        /// Gets specific collection of entities and return a skipToken if there are more than
        /// 60 entities to be returned.
        /// </summary>
        /// <param name="query">oData query</param>
        /// <param name="skipToken">The skip token to be used to get the next page of data.</param>
        /// <returns>List of entity Objects</returns>
        public List <T> Get(string query, ref string skipToken)
        {
            // Get the response and convert it to a list of entities of the specific type
            string response = _conn.Get(query);

            skipToken = ApiResponseCleaner.GetSkipToken(response);
            response  = ApiResponseCleaner.GetJsonArray(response);

            var rc       = new EntityConverter();
            var entities = rc.ConvertJsonArrayToObjectList <T>(response);

            // If the entity isn't managed already, register to managed entity collection
            foreach (var entity in entities)
            {
                AddEntityToManagedEntitiesCollection(entity);
            }

            // Convert list
            return(entities.ConvertAll(x => x));
        }
        /// <summary>
        /// Gets specific collection of entities and return a skipToken if there are more than
        /// 60 entities to be returned.
        /// </summary>
        /// <param name="query">oData query</param>
        /// <param name="skipToken">The skip token to be used to get the next page of data.</param>
        /// <returns>List of entity Objects</returns>
        public async Task <Tuple <string, List <T> > > GetAsync(string query, string skipToken)
        {
            // Get the response and convert it to a list of entities of the specific type
            string response = await _conn.GetAsync(query);

            skipToken = ApiResponseCleaner.GetSkipToken(response);
            response  = ApiResponseCleaner.GetJsonArray(response);

            var rc       = new EntityConverter();
            var entities = await rc.ConvertJsonArrayToObjectListAsync <T>(response);

            // If the entity isn't managed already, register to managed entity collection
            foreach (var entity in entities)
            {
                AddEntityToManagedEntitiesCollection(entity);
            }

            // Convert list
            return(new Tuple <string, List <T> >(skipToken, entities));
        }
示例#19
0
        /// <summary>
        /// Gets specific collection of entities and return a skipToken if there are more than
        /// 60 entities to be returned.
        /// </summary>
        /// <param name="query">oData query</param>
        /// <returns>List of entity Objects</returns>
        public async Task <Models.ApiList <T> > GetAsync(string query)
        {
            // Get the response and convert it to a list of entities of the specific type
            string response = await _conn.GetAsync(query).ConfigureAwait(false);

            string skipToken = ApiResponseCleaner.GetSkipToken(response);

            response = ApiResponseCleaner.GetJsonArray(response);

            var rc       = new EntityConverter();
            var entities = rc.ConvertJsonArrayToObjectList <T>(response);

            // If the entity isn't managed already, register to managed entity collection
            foreach (var entity in entities)
            {
                AddEntityToManagedEntitiesCollection(entity);
            }

            // Convert list
            return(new Models.ApiList <T>(entities.ConvertAll(x => x), skipToken));
        }
        /// <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 ApiResponseCleaner_FetchJsonArray_WithOutResultsKeyValuePair_Fails()
 {
     ApiResponseCleaner.GetJsonArray(JsonFileReader.GetJsonFromFile("ApiResponse_Json_Array_WithoutResultsTag.txt"));
 }
 public void ApiResponseCleaner_FetchJsonObject_WithCorrectValues_Succeeds()
 {
     ApiResponseCleaner.GetJsonObject(JsonFileReader.GetJsonFromFile("ApiResponse_Json_Object.txt"));
 }
 public void ApiResponseCleaner_FetchJsonObject_WithoutDKeyValuePair_Fails()
 {
     ApiResponseCleaner.GetJsonObject(JsonFileReader.GetJsonFromFile("ApiResponse_Json_Object_WithoutD.txt"));
 }
 public void ApiResponseCleaner_FetchJsonArray_WithCorrectValues_Succeeds()
 {
     string jsonarray = ApiResponseCleaner.GetJsonArray(JsonFileReader.GetJsonFromFile("ApiResponse_Json_Array.txt"));
 }