public IEnumerable <T> Scan <T>(IEnumerable <IEntityField> fields) where T : IEntityScannable, new() { if (fields == null) { throw new ArgumentNullException(nameof(fields)); } if (fields.Any() == false) { throw new ArgumentException("fields must contain at least one field to retrieve", nameof(fields)); } if (fields.Any(entry => entry.BelongsTo != typeof(T))) { throw new ArgumentException("All fields must be a member of the entity being scanned.", nameof(fields)); } if (ValidateScanTypeSupported <T>(ScanType.Fields) == false) { throw new InvalidOperationException($"Entity of type {typeof (T).Name} does not support this type of scan."); } var scanRequest = Factory.Create <IScanRequest <T>, ScanRequest <T> >(); scanRequest.ApiKey = ApiKey; scanRequest.Fields = fields; var result = new ScanEnumerable <T>(scanRequest); return(result); }
public IEnumerable <T> Scan <T>() where T : IEntityScannable, new() { if (ValidateScanTypeSupported <T>(ScanType.Base) == false) { throw new InvalidOperationException($"Entity of type {typeof (T).Name} does not support this type of scan."); } var scanRequest = Factory.Create <IScanRequest <T>, ScanRequest <T> >(); scanRequest.ApiKey = ApiKey; var result = new ScanEnumerable <T>(scanRequest); return(result); }
public IEnumerable <T> Scan <T>(string searchQuery) where T : IEntityScannable, new() { if (string.IsNullOrWhiteSpace(searchQuery)) { throw new ArgumentException("searchQuery cannot be null, empty, or whitespace.", nameof(searchQuery)); } if (ValidateScanTypeSupported <T>(ScanType.Query) == false) { throw new InvalidOperationException($"Entity of type {typeof (T).Name} does not support this type of scan."); } var scanRequest = Factory.Create <IScanRequest <T>, ScanRequest <T> >(); scanRequest.ApiKey = ApiKey; scanRequest.SearchQuery = searchQuery; var result = new ScanEnumerable <T>(scanRequest); return(result); }
public void TestEnumerationWhenNoPagination() { var mockRestClient = A.Fake <IRestClient>(); IRestResponse <ScanResponse <Lead> >[] mockRestClientResults = { new RestResponse <ScanResponse <Lead> > { Data = new ScanResponse <Lead>() { HasMore = false, TotalResults = 1, Data = new[] { new Lead() { Id = "1" } } }, StatusCode = HttpStatusCode.OK } }; A.CallTo(() => mockRestClient.Execute <ScanResponse <Lead> >(A <IRestRequest> .That.Matches(entry => entry.Parameters.Any(param => param.Name == "_skip" && string.Equals(param.Value, "0"))))) .ReturnsNextFromSequence(mockRestClientResults); A.CallTo(() => mockRestClient.Execute <ScanResponse <Lead> >(A <IRestRequest> .That.Not.Matches(entry => entry.Parameters.Any( param => param.Name == "_skip" && string.Equals(param.Value, "0"))))) .Throws(new AssertFailedException("IRestClient.Execute called with unexpected value.")); Factory.DispenseForType <IRestClient, RestClient>(mockRestClient); var unit = new ScanEnumerable <Lead>(new ScanRequest <Lead>("mockApiKey")); var result = unit.ToList(); Assert.AreEqual(1, result.Count); Assert.AreEqual("1", result[0].Id); }
public void TestEnumerationWhenNoResults() { var mockRestClient = A.Fake <IRestClient>(); A.CallTo(() => mockRestClient.Execute <ScanResponse <Lead> >(A <IRestRequest> .That.Matches(entry => entry.Parameters.Any(param => param.Name == "_skip" && Equals(param.Value, "0"))))) .Returns(new RestResponse <ScanResponse <Lead> >() { StatusCode = HttpStatusCode.OK, Data = new ScanResponse <Lead>() { Data = new List <Lead>(), HasMore = false, TotalResults = 0 } }); Factory.DispenseForType <IRestClient, RestClient>(mockRestClient); var unit = new ScanEnumerable <Lead>(new ScanRequest <Lead>("mockApiKey")); var results = unit.ToList(); Assert.IsNotNull(results); Assert.AreEqual(0, results.Count); }