/// <inheritdoc /> public virtual async Task <bool> RunPage(CancellationToken cancellationToken = default) { if (subqueryRunners == null) { subqueryRunners = new Stack <IQueryRunner>(); subqueryResultSinks = new Dictionary <ISubquery, List <Action <object> > >(); // This is the first run, so run the master page. var master = owner.MasterQuery; var data = await connection.Run(master.GetPayload(Variables), cancellationToken).ConfigureAwait(false); var json = deserializer.Deserialize(data); json.AddAnnotation(this); Result = deserializer.Deserialize(master.ResultBuilder, json); // Look through each subquery for any results that have a next page. foreach (var subquery in owner.Subqueries) { var pageInfos = subquery.ParentPageInfo(json).ToList(); var parentIds = subquery.ParentIds(json).ToList(); if (subqueryResultSinks.TryGetValue(subquery, out var sinks)) { for (var i = 0; i < pageInfos.Count; ++i) { var pageInfo = pageInfos[i]; if ((bool)pageInfo["hasNextPage"] == true) { var id = parentIds[i].ToString(); var after = (string)pageInfo["endCursor"]; var runner = subquery.Start(connection, id, after, Variables, sinks[i]); subqueryRunners.Push(runner); } } } } } else { // Get the next subquery runner. var runner = subqueryRunners.Peek(); // Run its next page and pop it from the active runners if finished. if (!await runner.RunPage(cancellationToken).ConfigureAwait(false)) { subqueryRunners.Pop(); } } return(subqueryRunners.Count > 0); }
/// <inheritdoc /> public async Task <bool> RunPage(CancellationToken cancellationToken = default) { var payload = owner.GetPayload(variables); var data = await connection.Run(payload, cancellationToken).ConfigureAwait(false); var json = deserializer.Deserialize(data); var pageInfo = owner.PageInfo(json); Result = owner.ResultBuilder(json); foreach (var i in (IList)Result) { addResult(i); } if ((bool)pageInfo["hasNextPage"] == true) { variables["__after"] = (string)pageInfo["endCursor"]; return(true); } else { return(false); } }
public async Task <bool> RunPage() { var payload = owner.GetPayload(variables); var data = await connection.Run(payload); var json = deserializer.Deserialize(data); var pageInfo = owner.PageInfo(json); Result = owner.ResultBuilder(json); foreach (var i in (IList)Result) { finalResult.Add(i); } if ((bool)pageInfo["hasNextPage"] == true) { variables["__after"] = (string)pageInfo["endCursor"]; return(true); } else { return(false); } }
public void ShouldCheckConstructorParameters() { ResponseDeserializer deserializer; deserializer = new ResponseDeserializer(); Assert.ThrowsException <ArgumentNullException>(() => deserializer.Deserialize <string>(null)); }
public async Task <bool> RunPage() { var deserializer = new ResponseDeserializer(); var data = await connection.Run(parent.GetPayload(variables)); Result = deserializer.Deserialize(parent.ResultBuilder, data); return(false); }
/// <inheritdoc /> public async Task <bool> RunPage(CancellationToken cancellationToken = default) { var deserializer = new ResponseDeserializer(); var data = await connection.Run(parent.GetPayload(variables), cancellationToken).ConfigureAwait(false); Result = deserializer.Deserialize(parent.ResultBuilder, data); return(false); }
public void Can_deserialize_object() { //success case with well formed response const string xml = "<?xml version=\"1.0\"?><response xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" status=\"ok\"><testObject id=\"1\"> <name>A big test object</name><listOfThings><string>one</string><string>two</string><string>three</string></listOfThings><listOfInnerObjects><InnerObject id=\"1\"><Name>Trevor</Name></InnerObject><InnerObject id=\"2\"><Name>Bill</Name></InnerObject></listOfInnerObjects></testObject></response>"; var stubResponse = new Response { StatusCode = HttpStatusCode.OK, Body = xml }; var xmlSerializer = new ResponseDeserializer<TestObject>(); Assert.DoesNotThrow(() => xmlSerializer.Deserialize(stubResponse)); TestObject testObject = xmlSerializer.Deserialize(stubResponse); Assert.That(testObject.Id, Is.EqualTo(1)); }
public async Task <IEnumerable <T> > Run <T>(IQueryable <T> queryable) { var query = builder.Build(queryable); var httpClient = CreateHttpClient(); var content = new StringContent(query.GetPayload()); var response = await httpClient.PostAsync(uri, content); var data = await response.Content.ReadAsStringAsync(); return(deserializer.Deserialize(query, data)); }
public void can_deseralize_card_types() { var xmlSerializer = new ResponseDeserializer<PaymentCardTypes>(); var result = xmlSerializer.Deserialize(stubResponse); Assert.That(result.CardTypes.Count(),Is.EqualTo(4)); var lastCard = result.CardTypes.Last(); Assert.That(lastCard.Type, Is.EqualTo("American Express")); Assert.That(lastCard.Id, Is.EqualTo("AMEX")); }
public void should_deserialize_emtpy_release_type_to_unknown() { var responseXml = File.ReadAllText("StubResponses/ArtistReleases.xml"); var response = new Response { StatusCode = HttpStatusCode.OK, Body = responseXml }; var xmlSerializer = new ResponseDeserializer<ArtistReleases>(); var release = xmlSerializer.Deserialize(response).Releases.First(); Assert.That(release.Type,Is.EqualTo(ReleaseType.Unknown)); }
public void Can_Deserialise_ok_response_without_body__as_DeleteCard() { const string ResponseXml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><response status=\"ok\" version=\"1.2\" />"; var response = new Response { StatusCode = HttpStatusCode.OK, Body = ResponseXml }; var xmlSerializer = new ResponseDeserializer<DeleteCard>(); var result = xmlSerializer.Deserialize(response); Assert.That(result, Is.Not.Null); }
public void Can_deserialize_well_formed_error() { const string errorXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><response status=\"error\" version=\"1.2\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://api.7digital.com/1.2/static/7digitalAPI.xsd\" ><error code=\"1001\"><errorMessage>Test error</errorMessage></error></response>"; var response = new Response { StatusCode = HttpStatusCode.OK, Body = errorXml }; var xmlSerializer = new ResponseDeserializer<TestObject>(); var ex = Assert.Throws<ApiXmlException>(() => xmlSerializer.Deserialize(response)); Assert.That(ex.StatusCode, Is.EqualTo(response.StatusCode)); Assert.That(ex.Message, Is.StringStarting("Error response")); Assert.That(ex.Message, Is.StringEnding(errorXml)); Assert.That(ex.Error.Code, Is.EqualTo(1001)); Assert.That(ex.Error.ErrorMessage, Is.EqualTo("Test error")); }
public void Error_captures_http_status_code_from_html() { const string badXml = "<html><head>Error</head><body>It did not work<br><hr></body></html>"; var response = new Response { StatusCode = HttpStatusCode.InternalServerError, Body = badXml }; var xmlSerializer = new ResponseDeserializer<TestObject>(); var ex = Assert.Throws<ApiXmlException>(() => xmlSerializer.Deserialize(response)); Assert.That(ex, Is.Not.Null); Assert.That(ex.Message, Is.StringStarting("Server error:")); Assert.That(ex.Message, Is.StringEnding(badXml)); Assert.That(ex.StatusCode, Is.EqualTo(response.StatusCode)); }
public void Should_not_fail_if_xml_is_missing_error_code() { const string validXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><response status=\"error\" version=\"1.2\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://api.7digital.com/1.2/static/7digitalAPI.xsd\" ><error><errorMessage>An error</errorMessage></error></response>"; var response = new Response { StatusCode = HttpStatusCode.OK, Body = validXml }; var xmlSerializer = new ResponseDeserializer<TestObject>(); var ex = Assert.Throws<ApiXmlException>(() => xmlSerializer.Deserialize(response)); Assert.That(ex.StatusCode, Is.EqualTo(response.StatusCode)); Assert.That(ex.Error.ErrorMessage, Is.StringStarting("XML error parse failed")); }
public void should_throw_exception_when_deserialize_with_missing_status() { const string MissingStatusXmlResponse = "<?xml version=\"1.0\"?><response version=\"1.2\"></response>"; var response = new Response { StatusCode = HttpStatusCode.OK, Body = MissingStatusXmlResponse }; var deserializer = new ResponseDeserializer<TestEmptyObject>(); var ex = Assert.Throws<ApiXmlException>(() => deserializer.Deserialize(response)); Assert.That(ex, Is.Not.Null); Assert.That(ex.Message, Is.StringStarting("No valid status found in response.")); Assert.That(ex.StatusCode, Is.EqualTo(response.StatusCode)); }
public void Should_handle_plaintext_OauthFail() { var response = new Response { StatusCode = HttpStatusCode.Unauthorized, Body = "OAuth authentication error: Not authorised - no user credentials provided" }; var xmlSerializer = new ResponseDeserializer<TestObject>(); var ex = Assert.Throws<ApiXmlException>(() => xmlSerializer.Deserialize(response)); Assert.That(ex, Is.Not.Null); Assert.That(ex.Message, Is.StringStarting("Error response")); Assert.That(ex.Message, Is.StringEnding(response.Body)); Assert.That(ex.StatusCode, Is.EqualTo(response.StatusCode)); Assert.That(ex.Error.Code, Is.EqualTo(9001)); Assert.That(ex.Error.ErrorMessage, Is.EqualTo(response.Body)); }
public void Should_throw_api_exception_with_null() { var apiXmlDeSerializer = new ResponseDeserializer<Status>(); var apiException = Assert.Throws<ApiXmlException>(() => apiXmlDeSerializer.Deserialize(null)); Assert.That(apiException.Message, Is.EqualTo("No response")); }
private static TItem DoRequest <TItem>(string url, string method, Dictionary <string, string> data, Dictionary <string, string> headers, ResponseDeserializer deserializer) where TItem : class { var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.AllowAutoRedirect = false; request.ContentLength = 0; if (data != null) { request.ContentType = "application/x-www-form-urlencoded"; var body = data.ToUrlEncoded(); request.ContentLength = body.Length; using (var sm = request.GetRequestStream()) { using (var sw = new StreamWriter(sm)) { sw.Write(body); } } log.DebugFormat("{2} {0} with body {1}", url, body, method); } if (headers != null) { request.Headers = new WebHeaderCollection(); foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } log.DebugFormat("{2} {0} with headers {1}", url, string.Join(";", headers.Select(x => x.Key + ":" + x.Value)), method); } using (var response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { throw new HttpException($"Response status is {response.StatusCode}"); } if (response.ContentLength == 0) { throw new HttpException("Response doesn't contain content"); } using (var rs = response.GetResponseStream()) { using (var sr = new StreamReader(rs)) { var content = sr.ReadToEnd(); log.DebugFormat("{1} {2} response body\n{0}", content, method, url); return((TItem)deserializer.Deserialize(typeof(TItem), content)); } } } }
public void turns_html_ok_response_into_error() { const string badXml = "<html><head>Error</head><body>Some random html page<br><hr></body></html>"; var response = new Response { StatusCode = HttpStatusCode.OK, Body = badXml }; var xmlSerializer = new ResponseDeserializer<TestObject>(); var ex = Assert.Throws<ApiXmlException>(() => xmlSerializer.Deserialize(response)); Assert.That(ex, Is.Not.Null); Assert.That(ex.Message, Is.StringStarting("Error trying to deserialize xml response")); Assert.That(ex.Message, Is.StringEnding(badXml)); Assert.That(ex.StatusCode, Is.EqualTo(response.StatusCode)); }