Advertisements

Solid.Http.Extensions.Xml

Solid.Http.Extensions.Xml

March 20, 2018 Packages 0

Introducing Solid.Http.Extensions.Xml

Ever used a web API that only returns xml? We have! It can be frustrating when you want to serialize to your model objects but there is no support for xml deserialization with the tools that you are using. That is why we created this extension. We wanted to have the pattern very similar when you are using the json feature included in Solid.Http, but have it an opt-in model since this is a feature you will rarely need. Under the hood we are using DataContractSerializer for serializing xml.

Examples

Adding Solid.Http.Extensions.Xml to you AspNetCore project is easy.

First, get Solid.Http on nuget, then get Solid.Http.Extensions.Xml

Then add the initialize the Solid.Http.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddSolidHttp()
            .AddXml();
    }
    public void Configure(IApplicationBuilder builder)
    {
        // Configuration removed for brevity
    }
}

This will initialize the default serialization config for DataContractSerializerSettings. You can set you own configuration when you initialize the extension.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var settings = new DataContractSerializerSettings();
        // set custom serializer settings
        services
            .AddSolidHttp()
            .AddXml(settings);
    }
    public void Configure(IApplicationBuilder builder)
    {
        // Configuration removed for brevity
    }
}

Now you can deserialize xml responses from xml web APIs. ( Note in this example we are telling mockbin to explicitly return xml with the Accept header )

public class ValuesController : Controller
{
    private SolidHttpClient _client;
    public ValuesController(ISolidHttpClientFactory factory)
    {
        _client = factory.CreateWithBaseAddress("http://mockbin.com");
    }
    [HttpGet]
    public async Task GetAsync()
    {
        var resp = await client
            .GetAsync("request")
            .WithHeader("Accept", "application/xml")
            .As<MockbinResponse>();
        return Ok(resp);
    }
}

Where the models could look something like this

    [DataContract(Name = "headers", Namespace = "")]
    public class MockbinHeaders
    {
        [DataMember(Name = "host")]
        public string Host { get; set; }
    }

    [DataContract(Name = "response", Namespace = "")]
    public class MockbinResponse
    {
        [DataMember(Name = "startedDateTime", Order = 1)]
        private DateTime Started { get; set; }
        [DataMember(Name = "clientIPAddress", Order = 2)]
        public string ClientIPAddress { get; set; }
        [DataMember(Name = "method", Order = 3)]
        public string Method { get; set; }
        [DataMember(Name = "url", Order = 4)]
        public string Url { get; set; }
        [DataMember(Name = "headers", Order = 5)]
        public MockbinHeaders Headers { get; set; }
    }

One thing to note, if you have a simple schema that lines up with your model, you don’t have to decorate it with DataContract and DataMember attributes ( that is not a feature with our library, just the default behavior of the DataContractSerializer )
That’s about it!

Advertisements