示例#1
0
        static void Main(string[] args)
        {
            var authClient = new OAuthClient("*****@*****.**", "password");
            var apiContext = new ApiContext(authClient.GetSecureToken())
            {
                AppName = "Application name",
            };

            var api = new ApiExplorer(apiContext);

            // get template for new contact
            var template = api.Contacts.Default();

            // cast to insert model
            var contact = (ContactCreate)template;

            // fill new contact
            contact.CompanyName = "New Company";

            // insert
            var addedContact = api.Contacts.Create(contact);

            // delete
            bool isDeleted = api.Contacts.Delete(addedContact.Id);


            List <ValidationMessage> errors;
            bool isValid = ApiValidator.ValidateObject(contact, out errors);

            if (isValid)
            {
                var result = api.Contacts.Create(new ContactCreate());
            }
        }
示例#2
0
        public void ApiValidator_OtherValidationConditionsNotMet_ReturnsFalse()
        {
            // Arrange
            var entity = new TestEntity
            {
                IdentificationNumber = "123456789012345678901",
                Name            = "something",
                Rating          = 0,
                NonNullableDate = DateTime.Now,
                NullableDate    = DateTime.Now
            };

            // Act
            var result = ApiValidator.ValidateObject(entity, out var errors);

            // Assert
            Assert.False(result);
            Assert.AreEqual(4, errors.Count);
            var memberNames = errors.SelectMany(error => error.MemberNames).ToList();

            Assert.Contains(nameof(TestEntity.IdentificationNumber), memberNames);
            Assert.Contains(nameof(TestEntity.Rating), memberNames);
            Assert.Contains(nameof(TestEntity.NonNullableDate), memberNames);
            Assert.Contains(nameof(TestEntity.NullableDate), memberNames);
        }
示例#3
0
        static void Main(string[] args)
        {
            //var authCredentials = new AuthorizationCodeAuth("client_id", "client_secret", "code", "http://localhost:3432");
            //var url = AuthorizationCodeAuth.GetClientAuthenticationUrl("client_id", "http://localhost:3432");

            // choose authorization flow
            var clientCred = new ClientCredentialAuth("client_id", "client_secret");

            // initialise context with configuration
            var apiContext = new ApiContext(clientCred)
            {
                AppName = "Application name"
            };

            // initialise api explorer
            var api = new ApiExplorer(apiContext);

            // get template for new contact
            var template = api.Contacts.Default();

            // cast to insert model
            var contact = (ContactCreate)template;

            // fill new contact
            contact.CompanyName = "New Company";

            // insert
            var addedContact = api.Contacts.Create(contact);

            // delete
            bool isDeleted = api.Contacts.Delete(addedContact.Id);

            // get all
            var allContacts = api.Contacts.Contacts(new ApiFilter().WithPaging(1, int.MaxValue));

            // prepare custom filter for querying
            var contactFilter = new ContactFilter();

            contactFilter.DateLastChange.IsEqualOrGreatherThan(new DateTime(2016, 1, 1));

            // query data using custom filter
            var contacts = api.Contacts.ContactsExpand(
                new ApiFilter(contactFilter, FilterType.And).AddOrderDesc("Id").WithPaging(1, 5));

            List <ValidationMessage> errors;
            bool isValid = ApiValidator.ValidateObject(contact, out errors);

            if (isValid)
            {
                var result = api.Contacts.Create(new ContactCreate());
            }
        }
示例#4
0
        public void ValidateObject_ObjectIsNotValid_ReturnsFals()
        {
            // Arrange
            var model = new FakeApiModel()
            {
                Firstname = "ReallyLongName",
                Lastname  = null
            };

            List <ValidationMessage> errors;

            // Act
            bool isValid = ApiValidator.ValidateObject(model, out errors);

            // Assert
            Assert.That(isValid, Is.False);
            Assert.That(errors.Any(), Is.True);
        }
示例#5
0
        public void ApiValidator_AllConditionsAreMet_ReturnsTrue()
        {
            // Arrange
            var entity = new TestEntity
            {
                IdentificationNumber = "12345678901234567890",
                Name            = "something",
                Rating          = 1,
                NonNullableDate = DateTime.Now.SetKindUtc(),
                NullableDate    = DateTime.Now.AddDays(1).SetKindUtc(),
            };

            // Act
            var result = ApiValidator.ValidateObject(entity, out var errors);

            // Assert
            Assert.True(result);
            Assert.Zero(errors.Count);
        }
示例#6
0
        public void ApiValidator_RequiredPropertiesNotSet_ReturnsFalse()
        {
            // Arrange
            var entity = new TestEntity
            {
                NonNullableDate = DateTime.Now.SetKindUtc(),
                NullableDate    = null
            };

            // Act
            var result = ApiValidator.ValidateObject(entity, out var errors);

            // Assert
            Assert.False(result);
            Assert.AreEqual(2, errors.Count);
            var memberNames = errors.SelectMany(error => error.MemberNames).ToList();

            Assert.Contains(nameof(TestEntity.IdentificationNumber), memberNames);
            Assert.Contains(nameof(TestEntity.Name), memberNames);
        }
示例#7
0
 protected bool IsValidObject(object obj, out List <ValidationMessage> results)
 {
     return(ApiValidator.ValidateObject(obj, out results));
 }