public void DeserializationWorks_WhenPassedRightXML() { var xmlPath = Path.Combine(Environment.CurrentDirectory, "SavedSearchListData.xml"); var doc = XDocument.Load(xmlPath); var xml = new XmlDeserializer(); var output = xml.Deserialize<SavedSearchListResponseModel>(new RestResponse() { Content = doc.ToString() }); Assert.IsNotNull(output, "no deserialization worked"); Assert.IsNull(output.Errors, "deserialized errors wrong"); Assert.IsNotNull(output.SavedSearches, "SavedSearched did not Deserialize"); Assert.IsNotNull(output.SavedSearches.SavedSearchList, "SavedSearchList did not Deserialize"); //make a SavedSearchList to compare to //this will simplify the Asserts List<SavedSearch> searches = new List<SavedSearch>(); SavedSearch viewRetrieve = new SavedSearch(); viewRetrieve.SearchName = "viewRetrieve"; viewRetrieve.HostSite = "US"; viewRetrieve.ExternalID = "366f62b16ac491fed0ed848baa0fbc109128e67b30e75d9614d60ef1fe207d75"; searches.Add(viewRetrieve); SavedSearch lister = new SavedSearch(); lister.SearchName = "lister"; lister.HostSite = "US"; lister.ExternalID = "67f87348bf0da271924400d9197eeba7a291c76362a9398ab1699659c60c585f"; searches.Add(lister); Assert.AreEqual(searches[0].SearchName, output.SavedSearches.SavedSearchList[0].SearchName, "first in searchList searchname did not desearialize"); Assert.AreEqual(searches[0].HostSite, output.SavedSearches.SavedSearchList[0].HostSite, "first in searchList hostsite did not desearialize"); Assert.AreEqual(searches[0].ExternalID, output.SavedSearches.SavedSearchList[0].ExternalID, "first in searchList externalID did not desearialize"); Assert.AreEqual(searches[1].SearchName, output.SavedSearches.SavedSearchList[1].SearchName, "second in searchList searchName did not desearialize"); Assert.AreEqual(searches[1].HostSite, output.SavedSearches.SavedSearchList[1].HostSite, "second in searchList hostsite did not desearialize"); Assert.AreEqual(searches[1].ExternalID, output.SavedSearches.SavedSearchList[1].ExternalID, "second in searchList ExternalID did not desearialize"); }
public void DeserializeTest() { XmlDeserializer target = new XmlDeserializer(); DotSpatial.Controls.Map map = new DotSpatial.Controls.Map(); string path = Path.Combine("Data", "DeserializeTest.map.xml"); target.Deserialize(map, File.ReadAllText(path)); }
public void Can_Deserialize_Elements_With_Namespace() { var doc = CreateElementsXml(); var response = new RestResponse { Content = doc }; var d = new XmlDeserializer(); d.Namespace = "http://restsharp.org"; var p = d.Deserialize<PersonForXml>(response); Assert.Equal("John Sheehan", p.Name); Assert.Equal(new DateTime(2009, 9, 25, 0, 6, 1), p.StartDate); Assert.Equal(28, p.Age); Assert.Equal(long.MaxValue, p.BigNumber); Assert.Equal(99.9999m, p.Percent); Assert.Equal(false, p.IsCool); Assert.Equal(new Guid(GuidString), p.UniqueId); Assert.Equal(new Uri("http://example.com", UriKind.RelativeOrAbsolute), p.Url); Assert.Equal(new Uri("/foo/bar", UriKind.RelativeOrAbsolute), p.UrlPath); Assert.NotNull(p.Friends); Assert.Equal(10, p.Friends.Count); Assert.NotNull(p.BestFriend); Assert.Equal("The Fonz", p.BestFriend.Name); Assert.Equal(1952, p.BestFriend.Since); }
public void HasReceivedPlayListWith(ImageId imageId) { ProbeFor(5000, 500, () => { var request = new RestRequest { Resource = "playlists.xml", RootElement = "playlists", XmlNamespace = "", RequestFormat = DataFormat.Xml, Method = Method.GET }; var response = m_Client.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { throw new ApplicationException(string.Format("Unable to get playlists")); } var deserializer = new XmlDeserializer {Namespace = "", RootElement = ""}; var playLists = deserializer.Deserialize<RestArchive.PlayLists>(response); Assert.That(GetImageIdsOf(playLists), Contains.Item(imageId)); }); }
public void DeserializeTest() { XmlDeserializer target = new XmlDeserializer(); DotSpatial.Controls.Map map = new DotSpatial.Controls.Map(); string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", "DeserializeTest.map.xml"); target.Deserialize(map, File.ReadAllText(path)); }
public void Can_Deserialize_Attributes_Without_Matching_Case_On_Default_Root() { var doc = CreateLowercaseDashesXml(); var response = new RestResponse { Content = doc }; var d = new XmlDeserializer(); var p = d.Deserialize<PersonForXml>(response); Assert.Equal("John Sheehan", p.Name); Assert.Equal(new DateTime(2009, 9, 25, 0, 6, 1), p.StartDate); Assert.Equal(28, p.Age); Assert.Equal(long.MaxValue, p.BigNumber); Assert.Equal(99.9999m, p.Percent); Assert.Equal(false, p.IsCool); Assert.Equal(new Guid(GuidString), p.UniqueId); Assert.Equal(new Uri("http://example.com", UriKind.RelativeOrAbsolute), p.Url); Assert.Equal(new Uri("/foo/bar", UriKind.RelativeOrAbsolute), p.UrlPath); Assert.NotNull(p.Friends); Assert.Equal(10, p.Friends.Count); Assert.NotNull(p.BestFriend); Assert.Equal("The Fonz", p.BestFriend.Name); Assert.Equal(1952, p.BestFriend.Since); Assert.NotNull(p.Foes); Assert.Equal(5, p.Foes.Count); Assert.Equal("Yankees", p.Foes.Team); }
public DeserializationCompiler (XmlDeserializer xmlDeserializer, Type type) : base (type) { if (xmlDeserializer == null) throw new ArgumentNullException ("xmlDeserializer"); this.xml_deserializer = xmlDeserializer; }
public void TestGraphWithEnumNode() { Node rootNode = new Node(1); Node intNode = new Node(42); Node enumNode = new Node(TestEnum.Two); rootNode.Nodes.Add(intNode); rootNode.Nodes.Add(enumNode); intNode.Nodes.Add(rootNode); Graph g = new Graph(rootNode); XmlSerializer s = new XmlSerializer(); string result = s.Serialize(g); XmlDeserializer d = new XmlDeserializer(); Graph newGraph = d.Deserialize<Graph>(result); Assert.IsNotNull(newGraph); Assert.AreEqual(g.Root.Data, newGraph.Root.Data); Assert.AreEqual(g.Root.Nodes[0].Data, newGraph.Root.Nodes[0].Data); Assert.AreEqual(g.Root.Nodes[1].Data, newGraph.Root.Nodes[1].Data); Assert.AreEqual(g.Root.Nodes[0].Nodes[0].Data, newGraph.Root.Nodes[0].Nodes[0].Data); Assert.AreSame(newGraph.Root, newGraph.Root.Nodes[0].Nodes[0]); }
public void DeserializationWorks_WhenPassedRightXML() { //using the same XML as SavedSearchCreate since they output the same var xmlPath = Path.Combine(Environment.CurrentDirectory, "SaveSearchCreateData.xml"); var doc = XDocument.Load(xmlPath); var xml = new XmlDeserializer(); var output = xml.Deserialize<SavedSearchRetrieveResponseModel>(new RestResponse() { Content = doc.ToString() }); Assert.IsNotNull(output, "no deserialization worked"); Assert.IsNotNull(output.SavedSearch.SavedSearchParameters, "SavedSearchParameters deserialization Did not work"); Assert.AreEqual("lotsloc", output.SavedSearch.SearchName, "search name did not dezerialize"); Assert.AreEqual("none", output.SavedSearch.IsDailyEmail.ToLower(), "IsDailyEmail did not dezerialize"); Assert.AreEqual("Chicago, Il, Atlanta, Ga, New York, Ny", output.SavedSearch.SavedSearchParameters.Location, "Location did not dezerialize"); Assert.AreEqual(false, output.SavedSearch.SavedSearchParameters.ExcludeNational, "ExcludeNational did not dezerialize"); Assert.AreEqual("DRNS", output.SavedSearch.SavedSearchParameters.EducationCode, "educationCode did not dezerialize"); Assert.AreEqual("AND", output.SavedSearch.SavedSearchParameters.BooleanOperator, "booleanoperator did not dezerialize"); Assert.AreEqual("Pay", output.SavedSearch.SavedSearchParameters.OrderBy, "OrderBy did not dezerialize"); Assert.AreEqual(70, output.SavedSearch.SavedSearchParameters.PayHigh, "pay high did not dezerialize"); Assert.AreEqual(40, output.SavedSearch.SavedSearchParameters.PayLow, "Pay Low did not dezerialize"); Assert.AreEqual(30, output.SavedSearch.SavedSearchParameters.PostedWithin, "posted within did not dezerialize"); Assert.AreEqual(30, output.SavedSearch.SavedSearchParameters.Radius, "radius did not dezerialize"); Assert.AreEqual(false, output.SavedSearch.SavedSearchParameters.PayInfoOnly, "pay info only did not dezerialize"); Assert.AreEqual(false, output.SavedSearch.SavedSearchParameters.SpecificEducation, "specific education did not dezerialize"); Assert.AreEqual("ascending", output.SavedSearch.SavedSearchParameters.OrderDirection, "order direction did not dezerialize"); }
public void DeserializationWorks_WhenPassedRightXML() { var xmlpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testdata","ResponseBlankApplication.xml"); var doc = XDocument.Load(xmlpath); var xml = new XmlDeserializer(); var output = xml.Deserialize<BlankApplication>(new RestResponse() { Content = doc.ToString() }); Assert.IsNotNull(output.Questions); Assert.AreEqual(4,output.Questions.Count); Assert.AreEqual("ApplicantName", output.Questions[0].QuestionID); Assert.AreEqual("Basic", output.Questions[0].QuestionType); Assert.AreEqual(true, output.Questions[0].IsRequired); Assert.AreEqual("Text 50", output.Questions[0].ExpectedResponseFormat); Assert.AreEqual("Your name", output.Questions[0].QuestionText); Assert.AreEqual("ApplicantEmail", output.Questions[1].QuestionID); Assert.AreEqual("Basic", output.Questions[1].QuestionType); Assert.AreEqual(true, output.Questions[1].IsRequired); Assert.AreEqual("Text 50", output.Questions[1].ExpectedResponseFormat); Assert.AreEqual("Your email", output.Questions[1].QuestionText); Assert.AreEqual("Resume", output.Questions[2].QuestionID); Assert.AreEqual("Basic", output.Questions[2].QuestionType); Assert.AreEqual(true, output.Questions[2].IsRequired); Assert.AreEqual("Text 5000", output.Questions[2].ExpectedResponseFormat); Assert.AreEqual("Your resume", output.Questions[2].QuestionText); Assert.AreEqual("CoverLetter", output.Questions[3].QuestionID); Assert.AreEqual("Basic", output.Questions[3].QuestionType); Assert.AreEqual(false, output.Questions[3].IsRequired); Assert.AreEqual("Text 5000", output.Questions[3].ExpectedResponseFormat); Assert.AreEqual("Your cover letter", output.Questions[3].QuestionText); }
public void Can_Deserialize_Names_With_Underscores_On_Default_Root() { var doc = CreateUnderscoresXml(); var d = new XmlDeserializer(); var p = d.Deserialize<PersonForXml>(doc); Assert.Equal("John Sheehan", p.Name); Assert.Equal(new DateTime(2009, 9, 25, 0, 6, 1), p.StartDate); Assert.Equal(28, p.Age); Assert.Equal(long.MaxValue, p.BigNumber); Assert.Equal(99.9999m, p.Percent); Assert.Equal(false, p.IsCool); Assert.NotNull(p.Friends); Assert.Equal(10, p.Friends.Count); Assert.NotNull(p.BestFriend); Assert.Equal("The Fonz", p.BestFriend.Name); Assert.Equal(1952, p.BestFriend.Since); Assert.NotNull(p.Foes); Assert.Equal(5, p.Foes.Count); Assert.Equal("Yankees", p.Foes.Team); }
public void DeserializationWorks_WhenPassedRightXML() { var xmlpath = Path.Combine(Environment.CurrentDirectory,"ResponseJobSearch.xml"); var doc = XDocument.Load(xmlpath); var xml = new XmlDeserializer(); var output = xml.Deserialize<ResponseJobSearch>(new RestResponse() { Content = doc.ToString() }); Assert.IsNotNull(output.Results); Assert.AreEqual(25, output.Results.Count); Assert.AreEqual("Direct Sales Recruiting, LLC", output.Results[0].Company); Assert.AreEqual("c35x165rs82x1bnbbc", output.Results[0].CompanyDID); Assert.AreEqual("http://www.careerbuilder.com/Jobs/Company/C35X165RS82X1BNBBC/Direct-Sales-Recruiting-LLC/?sc_cmp1=13_JobRes_ComDet", output.Results[0].CompanyDetailsURL); Assert.AreEqual("JHM0LH6LPCGLF3FR3ZB", output.Results[0].DID); Assert.AreEqual("41-4011.00", output.Results[0].OnetCode); Assert.AreEqual("Full-Time", output.Results[0].EmploymentType); Assert.AreEqual("http://api.careerbuilder.com/v1/joblink?TrackingID=DLR0T71H&DID=JHM0LH6LPCGLF3FR3ZB", output.Results[0].JobDetailsURL); Assert.AreEqual("http://api.careerbuilder.com/v1/job?DID=JHM0LH6LPCGLF3FR3ZB&DeveloperKey=WXXXXXXXXXXXXXXXXXX", output.Results[0].JobServiceURL); Assert.AreEqual("TN - Chattanooga", output.Results[0].Location); Assert.AreEqual(35.04644f, output.Results[0].LocationLatitude); Assert.AreEqual(-85.30946f, output.Results[0].LocationLongitude); Assert.AreEqual("10/28/2012", output.Results[0].PostedDate); Assert.AreEqual("$70k - $80k/year", output.Results[0].Pay); Assert.AreEqual("http://www.careerbuilder.com/jobseeker/jobs/recommendedjobs.aspx?ipath=JELO&job_did=JHM0LH6LPCGLF3FR3ZB", output.Results[0].SimilarJobsURL); Assert.AreEqual("Medical Sales-Tissue Graft/Biologics-Wound Care", output.Results[0].JobTitle); Assert.AreEqual("http://emj.icbdr.com/MediaManagement/QS/I8F50C63D7B6SGPDXQS.gif", output.Results[0].CompanyImageURL); Assert.IsNotNull(output.Facets); Assert.AreEqual(0, output.Facets.Count); }
public static object ConvertSingle(XElement xml) { object returnObject = null; if (xml != null) { string partyType = String.Empty; var s = new XmlDeserializer(); var element = xml.Element("type"); if (element != null) partyType = element.Value; if (String.IsNullOrEmpty(partyType)) { var attribute = xml.Attribute("type"); if (attribute != null) partyType = attribute.Value; } switch (partyType.ToLower()) { case "person": returnObject = s.Deserialize<Person>(new RestResponse { Content = xml.ToString() }); break; case "company": returnObject = s.Deserialize<Company>(new RestResponse { Content = xml.ToString() }); break; } } return returnObject; }
public Deserializer (XmlDeserializer xmlDeserializer) { if (xmlDeserializer == null) { throw new ArgumentNullException ("xmlDeserializer"); } this.deserializer = xmlDeserializer; }
public void Deserialize_SerializationRepresentation_DeserializedObject() { var subject = new XmlDeserializer(); var deserialized = subject.Deserialize<Serializable>(Serializable.XmlString("s", 3m)); Assert.That(deserialized.D, Is.EqualTo(3m)); Assert.That(deserialized.S, Is.EqualTo("s")); }
public void Ignore_ReadOnly_Property_That_Exists_In_Data() { var doc = CreateElementsXml(); var d = new XmlDeserializer(); d.Namespace = "http://restsharp.org"; var p = d.Deserialize<PersonForXml>(doc); Assert.Null(p.ReadOnlyProxy); }
public void Can_Deserialize_AcoustID_Xml() { var xmlpath = PathFor("AcoustID.xml"); var doc = XDocument.Load(xmlpath); var response = new RestResponse { Content = doc.ToString() }; var d = new XmlDeserializer(); var output = d.Deserialize<MTB.Worker.AcoustID.Response>(response); }
public void Can_Use_DeserializeAs_Attribute_for_List() { string xmlpath = this.PathFor("deserialize_as_list.xml"); XDocument doc = XDocument.Load(xmlpath); XmlDeserializer xml = new XmlDeserializer(); List<Oddball> output = xml.Deserialize<List<Oddball>>(new RestResponse { Content = doc.ToString() }); Assert.NotNull(output); Assert.AreEqual("1", output[0].Sid); }
public void Can_Use_DeserializeAs_Attribute_for_List() { const string content = "<document><response><oddballRootName><sid>1</sid><friendlyName>Jackson</friendlyName><oddballPropertyName>oddball</oddballPropertyName></oddballRootName></response><response><oddballRootName><sid>1</sid><friendlyName>Jackson</friendlyName><oddballPropertyName>evenball</oddballPropertyName></oddballRootName></response></document>"; var xml = new XmlDeserializer(); List<Oddball> output = xml.Deserialize<List<Oddball>>(new RestResponse { Content = content }); Assert.NotNull(output); Assert.AreEqual("1", output[0].Sid); }
public void WhenSerializedDirectoryEntityIsPassedItWillBeDeserialized() { const string serializedFileString = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<FakeEntity xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n </FakeEntity>"; var deserializedDirectory = new XmlDeserializer().Deserialize<FakeEntity>(serializedFileString); Assert.IsNotNull(deserializedDirectory); Assert.IsInstanceOfType(deserializedDirectory, typeof(FakeEntity)); }
public void ZoomToMaxExtentTest() { XmlDeserializer target = new XmlDeserializer(); Map map = new Map(); string path = Path.Combine("TestFiles", "testproject1.dspx"); target.Deserialize(map, File.ReadAllText(path)); map.ZoomToMaxExtent(); }
public ControlServer (IMap<string, ServiceAction> actions, string serviceType, Uri url, XmlSerializer serializer) : base (url) { this.actions = actions; this.service_type = serviceType; this.serializer = serializer; this.deserializer = Helper.Get<XmlDeserializer> (static_deserializer); }
public void Can_Deserialize_To_Standalone_List_With_Matching_Class_Case() { var xmlpath = PathFor("InlineListSample.xml"); var doc = XDocument.Load(xmlpath); var xml = new XmlDeserializer(); var output = xml.Deserialize<List<image>>(new RestResponse { Content = doc.ToString() }); Assert.NotEmpty(output); Assert.Equal(4, output.Count); }
public void Can_Deserialize_Into_Struct() { const string content = "<root><one>oneOneOne</one><two>twoTwoTwo</two><three>3</three></root>"; XmlDeserializer xml = new XmlDeserializer(); SimpleStruct output = xml.Deserialize<SimpleStruct>(new RestResponse { Content = content }); Assert.NotNull(output); Assert.AreEqual("oneOneOne", output.One); Assert.AreEqual("twoTwoTwo", output.Two); Assert.AreEqual(3, output.Three); }
public void Can_Deserialize_Boolean_From_Number() { var xmlpath = PathFor("boolean_from_number.xml"); var doc = XDocument.Load(xmlpath); var response = new RestResponse { Content = doc.ToString() }; var d = new XmlDeserializer(); var output = d.Deserialize<SampleClasses.BooleanTest>(response); Assert.True(output.Value); }
public void Can_Deserialize_Directly_To_Lists_Off_Root_Element() { var xmlpath = PathFor("directlists.xml"); var doc = XDocument.Load(xmlpath); var xml = new XmlDeserializer(); var output = xml.Deserialize<List<Database>>(new RestResponse { Content = doc.ToString() }); Assert.NotEmpty(output); Assert.Equal(2, output.Count); }
public void Can_Deserialize_Error() { var xmlpath = Environment.CurrentDirectory + @"\..\..\..\Lastfm.Tests\Responses\Error.xml"; var doc = XDocument.Load(xmlpath); var response = new RestResponse { Content = doc.ToString() }; var d = new XmlDeserializer(); var output = d.Deserialize<LastfmResponse<EventsList>>(response); Assert.IsNotNull(output.Error); Assert.AreEqual(6, output.Error.Code); }
public void DeserializeFrenchCultureTest() { // Sets the culture to French (France) Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR"); // Sets the UI culture to French (France) Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR"); XmlDeserializer target = new XmlDeserializer(); DotSpatial.Controls.Map map = new DotSpatial.Controls.Map(); string path = Path.Combine("Data", "DeserializeTest.map.xml"); target.Deserialize(map, File.ReadAllText(path)); }
public void Can_Use_DeserializeAs_Attribute() { const string content = "<oddball><sid>1</sid><friendlyName>Jackson</friendlyName><oddballPropertyName>oddball</oddballPropertyName></oddball>"; XmlDeserializer xml = new XmlDeserializer(); Oddball output = xml.Deserialize<Oddball>(new RestResponse { Content = content }); Assert.NotNull(output); Assert.AreEqual("1", output.Sid); Assert.AreEqual("Jackson", output.FriendlyName); Assert.AreEqual("oddball", output.GoodPropertyName); }
public void Can_Deserialize_Lists_of_Simple_Types() { var xmlpath = this.PathFor("xmllists.xml"); var doc = XDocument.Load(xmlpath); var xml = new XmlDeserializer(); var output = xml.Deserialize<SimpleTypesListSample>(new RestResponse { Content = doc.ToString() }); Assert.IsNotEmpty(output.Names); Assert.IsNotEmpty(output.Numbers); Assert.False(output.Names[0].Length == 0); Assert.False(output.Numbers.Sum() == 0); }
public static T DeserializeObject <T>(IRestResponse response) { XmlDeserializer xs = new XmlDeserializer(); return(xs.Deserialize <T>(response)); }
public void Deserialize_InvalidSerializationRepresentation_Extepcion() { var subject = new XmlDeserializer(); Assert.That(() => subject.Deserialize <Serializable>("invalid"), Throws.InstanceOf <InvalidOperationException>()); }
private async Task <ApiResponse <T> > ExecAsync <T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; RestClient client = new RestClient(baseUrl); client.ClearHandlers(); var existingDeserializer = req.JsonSerializer as IDeserializer; if (existingDeserializer != null) { client.AddHandler("application/json", () => existingDeserializer); client.AddHandler("text/json", () => existingDeserializer); client.AddHandler("text/x-json", () => existingDeserializer); client.AddHandler("text/javascript", () => existingDeserializer); client.AddHandler("*+json", () => existingDeserializer); } else { var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); client.AddHandler("text/javascript", () => customDeserializer); client.AddHandler("*+json", () => customDeserializer); } var xmlDeserializer = new XmlDeserializer(); client.AddHandler("application/xml", () => xmlDeserializer); client.AddHandler("text/xml", () => xmlDeserializer); client.AddHandler("*+xml", () => xmlDeserializer); client.AddHandler("*", () => xmlDeserializer); client.Timeout = configuration.Timeout; if (configuration.Proxy != null) { client.Proxy = configuration.Proxy; } if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; } if (configuration.ClientCertificates != null) { client.ClientCertificates = configuration.ClientCertificates; } InterceptRequest(req); IRestResponse <T> response; if (RetryConfiguration.AsyncRetryPolicy != null) { var policy = RetryConfiguration.AsyncRetryPolicy; var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false); response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize <T>(policyResult.Result) : new RestResponse <T> { Request = req, ErrorException = policyResult.FinalException }; } else { response = await client.ExecuteAsync <T>(req, cancellationToken).ConfigureAwait(false); } // if the response type is oneOf/anyOf, call FromJSON to deserialize the data if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) { response.Data = (T)typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content }); } else if (typeof(T).Name == "Stream") // for binary response { response.Data = (T)(object)new MemoryStream(response.RawBytes); } else if (typeof(T).Name == "Byte[]") // for byte response { response.Data = (T)(object)response.RawBytes; } InterceptResponse(req, response); var result = ToApiResponse(response); if (response.ErrorMessage != null) { result.ErrorText = response.ErrorMessage; } if (response.Cookies != null && response.Cookies.Count > 0) { if (result.Cookies == null) { result.Cookies = new List <Cookie>(); } foreach (var restResponseCookie in response.Cookies) { var cookie = new Cookie( restResponseCookie.Name, restResponseCookie.Value, restResponseCookie.Path, restResponseCookie.Domain ) { Comment = restResponseCookie.Comment, CommentUri = restResponseCookie.CommentUri, Discard = restResponseCookie.Discard, Expired = restResponseCookie.Expired, Expires = restResponseCookie.Expires, HttpOnly = restResponseCookie.HttpOnly, Port = restResponseCookie.Port, Secure = restResponseCookie.Secure, Version = restResponseCookie.Version }; result.Cookies.Add(cookie); } } return(result); }
private ApiResponse <T> Exec <T>(RestRequest req, IReadableConfiguration configuration) { RestClient client = new RestClient(_baseUrl); client.ClearHandlers(); var existingDeserializer = req.JsonSerializer as IDeserializer; if (existingDeserializer != null) { client.AddHandler("application/json", () => existingDeserializer); client.AddHandler("text/json", () => existingDeserializer); client.AddHandler("text/x-json", () => existingDeserializer); client.AddHandler("text/javascript", () => existingDeserializer); client.AddHandler("*+json", () => existingDeserializer); } else { var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); client.AddHandler("text/javascript", () => customDeserializer); client.AddHandler("*+json", () => customDeserializer); } var xmlDeserializer = new XmlDeserializer(); client.AddHandler("application/xml", () => xmlDeserializer); client.AddHandler("text/xml", () => xmlDeserializer); client.AddHandler("*+xml", () => xmlDeserializer); client.AddHandler("*", () => xmlDeserializer); client.Timeout = configuration.Timeout; if (configuration.Proxy != null) { client.Proxy = configuration.Proxy; } if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; } if (configuration.ClientCertificates != null) { client.ClientCertificates = configuration.ClientCertificates; } InterceptRequest(req); IRestResponse <T> response; if (RetryConfiguration.RetryPolicy != null) { var policy = RetryConfiguration.RetryPolicy; var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize <T>(policyResult.Result) : new RestResponse <T> { Request = req, ErrorException = policyResult.FinalException }; } else { response = client.Execute <T>(req); } InterceptResponse(req, response); var result = ToApiResponse(response); if (response.ErrorMessage != null) { result.ErrorText = response.ErrorMessage; } if (response.Cookies != null && response.Cookies.Count > 0) { if (result.Cookies == null) { result.Cookies = new List <Cookie>(); } foreach (var restResponseCookie in response.Cookies) { var cookie = new Cookie( restResponseCookie.Name, restResponseCookie.Value, restResponseCookie.Path, restResponseCookie.Domain ) { Comment = restResponseCookie.Comment, CommentUri = restResponseCookie.CommentUri, Discard = restResponseCookie.Discard, Expired = restResponseCookie.Expired, Expires = restResponseCookie.Expires, HttpOnly = restResponseCookie.HttpOnly, Port = restResponseCookie.Port, Secure = restResponseCookie.Secure, Version = restResponseCookie.Version }; result.Cookies.Add(cookie); } } return(result); }
internal static XmlRootDeserializer <T> Root <T>(XmlDeserializer <T> deserializer) => (XElement root, out T result) => deserializer(new XElement("artificialRoot", root), root.Name.LocalName, out result);
internal static XmlDeserializer <IDictionary <string, T> > CreateDictionaryXmlDeserializer <T>(XmlDeserializer <T> elementDeserializer) { return((XElement parent, string propertyName, out IDictionary <string, T> result) => { result = null; var childElement = parent.Element(propertyName); if (childElement == null) { return false; } result = new Dictionary <string, T>(); foreach (var element in childElement.Elements()) { T elementResult; if (!elementDeserializer(childElement, element.Name.LocalName, out elementResult)) { return false; } result.Add(element.Name.LocalName, elementResult); } return true; }); }
private async Task <ApiResponse <T> > ExecAsync <T>(RestRequest req, IReadableConfiguration configuration) { RestClient client = new RestClient(_baseUrl); client.ClearHandlers(); var existingDeserializer = req.JsonSerializer as IDeserializer; if (existingDeserializer != null) { AddHandlerHelper(client, existingDeserializer, new string[] { "application/json", "text/json", "text/x-json", "text/javascript", "*+json" }); } else { var customJsonHandler = new CustomJsonCodec(configuration); AddHandlerHelper(client, customJsonHandler, new string[] { "application/json", "text/json", "text/x-json", "text/javascript", "*+json" }); } var xmlDeserializer = new XmlDeserializer(); AddHandlerHelper(client, xmlDeserializer, new string[] { "application/xml", "text/xml", "*+xml", "*" }); client.Timeout = configuration.Timeout; if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; } if (configuration.ClientCertificates != null) { client.ClientCertificates = configuration.ClientCertificates; } InterceptRequest(req); var response = await client.ExecuteAsync <T>(req); InterceptResponse(req, response); var result = ToApiResponse(response); if (response.ErrorMessage != null) { result.ErrorText = response.ErrorMessage; } if (response.Cookies != null && response.Cookies.Count > 0) { if (result.Cookies == null) { result.Cookies = new List <Cookie>(); } foreach (var restResponseCookie in response.Cookies) { var cookie = new Cookie( restResponseCookie.Name, restResponseCookie.Value, restResponseCookie.Path, restResponseCookie.Domain ) { Comment = restResponseCookie.Comment, CommentUri = restResponseCookie.CommentUri, Discard = restResponseCookie.Discard, Expired = restResponseCookie.Expired, Expires = restResponseCookie.Expires, HttpOnly = restResponseCookie.HttpOnly, Port = restResponseCookie.Port, Secure = restResponseCookie.Secure, Version = restResponseCookie.Version }; result.Cookies.Add(cookie); } } return(result); }
private ApiResponse <T> Exec <T>(RestRequest req, IReadableConfiguration configuration) { RestClient client = new RestClient(_baseUrl); client.ClearHandlers(); var existingDeserializer = req.JsonSerializer as IDeserializer; if (existingDeserializer != null) { client.AddHandler("application/json", () => existingDeserializer); client.AddHandler("text/json", () => existingDeserializer); client.AddHandler("text/x-json", () => existingDeserializer); client.AddHandler("text/javascript", () => existingDeserializer); client.AddHandler("*+json", () => existingDeserializer); } else { var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration); client.AddHandler("application/json", () => customDeserializer); client.AddHandler("text/json", () => customDeserializer); client.AddHandler("text/x-json", () => customDeserializer); client.AddHandler("text/javascript", () => customDeserializer); client.AddHandler("*+json", () => customDeserializer); } var xmlDeserializer = new XmlDeserializer(); client.AddHandler("application/xml", () => xmlDeserializer); client.AddHandler("text/xml", () => xmlDeserializer); client.AddHandler("*+xml", () => xmlDeserializer); client.AddHandler("*", () => xmlDeserializer); client.Timeout = configuration.Timeout; if (configuration.Proxy != null) { client.Proxy = configuration.Proxy; } if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; } if (configuration.ClientCertificates != null) { client.ClientCertificates = configuration.ClientCertificates; } InterceptRequest(req); IRestResponse <T> response; if (RetryConfiguration.RetryPolicy != null) { var policy = RetryConfiguration.RetryPolicy; var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize <T>(policyResult.Result) : new RestResponse <T> { Request = req, ErrorException = policyResult.FinalException }; } else { response = client.Execute <T>(req); } // if the response type is oneOf/anyOf, call FromJSON to deserialize the data if (typeof(MyDataMyConsent.Sdk.Models.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) { try { response.Data = (T)typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content }); } catch (Exception ex) { throw ex.InnerException != null ? ex.InnerException : ex; } } else if (typeof(T).Name == "Stream") // for binary response { response.Data = (T)(object)new MemoryStream(response.RawBytes); } InterceptResponse(req, response); var result = ToApiResponse(response); if (response.ErrorMessage != null) { result.ErrorText = response.ErrorMessage; } if (response.Cookies != null && response.Cookies.Count > 0) { if (result.Cookies == null) { result.Cookies = new List <Cookie>(); } foreach (var restResponseCookie in response.Cookies) { var cookie = new Cookie( restResponseCookie.Name, restResponseCookie.Value, restResponseCookie.Path, restResponseCookie.Domain ) { Comment = restResponseCookie.Comment, CommentUri = restResponseCookie.CommentUri, Discard = restResponseCookie.Discard, Expired = restResponseCookie.Expired, Expires = restResponseCookie.Expires, HttpOnly = restResponseCookie.HttpOnly, Port = restResponseCookie.Port, Secure = restResponseCookie.Secure, Version = restResponseCookie.Version }; result.Cookies.Add(cookie); } } return(result); }