Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pmorelli92/soaphttpclient

HttpClient wrapper for sending SOAP messages.
https://github.com/pmorelli92/soaphttpclient

async client http soap wrapper

Last synced: about 4 hours ago
JSON representation

HttpClient wrapper for sending SOAP messages.

Awesome Lists containing this project

README

        

# SoapHttpClient [![NuGet](https://img.shields.io/nuget/v/SoapHttpClient.svg)](https://www.nuget.org/packages/SoapHttpClient)

> A lightweight wrapper of an `HttpClient` (using `IHttpClientFactory`) for POSTing messages that allows the user to send the SOAP Body and Header (if needed) without caring about the envelope.

## Changelog

### 3.0.0
- Replaced HttpClient with HttpClientFactory.

### 2.2.1
- Added support for Cancellation Tokens.

### 2.2.0
- Updated codebase.
- Migrated test project to .net core app.
- Fixed an error of SOAPAction not being sent.

### 2.1.0
- Updated to NetStandardLibrary 2.0
- Fixed a bug where an extension method was calling himself recursively

### 2.0.0
- Major refactor to the codebase.
- Added the functionality of adding more than one header and/or body in the envelope.
- The ctor will no longer determine the SoapVersion, since it is a message property and the API should be ignorant about this.
- **[BREAKING CHANGE]: SoapVersion is now required for every message.**
- **[BREAKING CHANGE]: Removed methods where the endpoint was a string instead of an uri.**

## API

### Constructors

```csharp
SoapClient()
```

Initializes `SoapClient` with a default `IHttpClientFactory` that implements automatic decompression.

```csharp
SoapClient(IHttpClientFactory httpClientFactory)
```

Initializes `SoapClient` with a `IHttpClientFactory` provided by the caller.
The `IHttpClientFactory` is the new interface used to manage HttpClient introduced on .NET Core 2.1.

------------------

### Methods

> All **Methods** and **Extension Methods** returns a Task of [`HttpResponseMessage`][msdn-httpresponsemessage]

The interface makes the client implement the following method:

```csharp
Task PostAsync(
Uri endpoint,
SoapVersion soapVersion,
IEnumerable bodies,
IEnumerable headers = null,
string? action = null,
CancellationToken cancellationToken = CancellationToken.Default);
```

Allowing us to send the following calls:

- Uri / Version / Bodies
- Uri / Version / Bodies / Headers
- Uri / Version / Bodies / Headers / Action

------------------

Then there are sugar sintax extension methods:

```csharp
Task PostAsync(
this ISoapClient client,
Uri endpoint,
SoapVersion soapVersion,
XElement body,
XElement header = null,
string? action = null,
CancellationToken cancellationToken = CancellationToken.Default);

Task PostAsync(
this ISoapClient client,
Uri endpoint,
SoapVersion soapVersion,
IEnumerable bodies,
XElement header,
string? action = null,
CancellationToken cancellationToken = CancellationToken.Default);

Task PostAsync(
this ISoapClient client,
Uri endpoint,
SoapVersion soapVersion,
XElement body,
IEnumerable headers,
string? action = null,
CancellationToken cancellationToken = CancellationToken.Default);
```

Allowing us to send the following calls:

- Uri / Version / Body
- Uri / Version / Body / Header
- Uri / Version / Body / Header / Action
- Uri / Version / Bodies / Header
- Uri / Version / Bodies / Header / Action
- Uri / Version / Body / Headers
- Uri / Version / Body / Headers / Action

------------------

With all of these variants we can send a message with:

- 1 Body - 1 Header
- 1 Body - N Headers
- N Bodies - 1 Header
- N Bodies - N Headers

------------------

There are also extension methods for sync calls:
**However we always recommend using async programming when you are able**.

> Their method name is **Post** and their return type is [`HttpResponseMessage`][msdn-httpresponsemessage]

Finally, we have extensions methods for using bodies and headers as objects and using serialization the default or a custom `IXElementSerializer` to serialize those objects to XElement.

## Usage Examples

### Controlling the Media Type

As the `SoapHttpClient` wraps a `HttpClient` you can control all aspects of the HTTP Request using a `HttpMessageHandler`:

```csharp
public class ContentTypeChangingHandler : DelegatingHandler
{
public ContentTypeChangingHandler(HttpMessageHandler innerHandler) : base(innerHandler) { }

protected async override Task SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8");
return await base.SendAsync(request, cancellationToken);
}
}
```

### Call NASA

```csharp
async Task CallNasaAsync()
{
var soapClient = new SoapClient();
var ns = XNamespace.Get("http://helio.spdf.gsfc.nasa.gov/");

var result =
await soapClient.PostAsync(
new Uri("http://sscweb.gsfc.nasa.gov:80/WS/helio/1/HeliocentricTrajectoriesService"),
SoapVersion.Soap11,
body: new XElement(ns.GetName("getAllObjects")));

result.StatusCode.Should().Be(HttpStatusCode.OK);
}
```

#### Result of Calling NASA Heliocentric Trajectories Service

```xml




1993-01-01T00:00:00Z
0001
COMET GRIGG-SKJLP
1992-01-01T00:00:00Z


1996-03-02T00:00:00Z
0002
COMET H-M-P
1996-01-01T00:00:00Z


1997-12-31T00:00:00Z
0003
COMET HALE-BOPP
1997-01-01T00:00:00Z

.
.
.

```

[Soap Icon][nounproj-soap] Created by Jakob Vogel from the Noun Project

[msdn-httpresponsemessage]: https://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.118).aspx
[nounproj-soap]: https://thenounproject.com/icon/44504/