ODataHttpClient 1.8.1

.NET Standard 2.0
dotnet add package ODataHttpClient --version 1.8.1
NuGet\Install-Package ODataHttpClient -Version 1.8.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ODataHttpClient" Version="1.8.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ODataHttpClient --version 1.8.1
#r "nuget: ODataHttpClient, 1.8.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ODataHttpClient as a Cake Addin
#addin nuget:?package=ODataHttpClient&version=1.8.1

// Install ODataHttpClient as a Cake Tool
#tool nuget:?package=ODataHttpClient&version=1.8.1

Test codecov NuGet version

The simplest implementation of OData client.

Install

$ dotnet add package ODataHttpClient
PS> Install-Package  ODataHttpClient

Restrictions

  • Not support XML (JSON only)
  • Not support Query Builder

Usage

Simple Request

var client = new HttpClient();
var odata = new ODataClient(client);
var request = Request.Get($"{endpoint}/Products?$inlinecount=allpages");
var response = await odata.SendAsync(request);

if (response.Success) 
{
    var total = response.ReadAs<int>("$['odata.count']");
    var products = response.ReadAs<IEnumerable<Product>>("$.value");
}

Batch Request

var client = new HttpClient();
var odata = new ODataClient(client);
var batch = new BatchRequest($"{endpoint}/$batch")
{
    Requests = new []
    {
        Request.Post($"{endpoint}/Products", new 
        { 
            Name = "Sample"
        }),
        Request.Post($"{endpoint}/$1/$links/Categories", new 
        {
            url = $"{endpoint}/Categories(0)"
        })
    }
};

var responses = await odata.SendAsync(batch);
// You can also use BatchAsync. 
// var responses = await odata.BatchAsync(batch);

if (responses.All(res => res.Success)) 
{
    var id = response.First().ReadAs<int>("$.Id");
}

Parameterized Uri

var client = new HttpClient();
var odata = new ODataClient(client);
var request = Request.Get($"{endpoint}/Products?$filter=ReleaseDate ge @Date", new { Date = new DateTime(2000, 1, 1) });
var response = await odata.SendAsync(request);

if (response.Success) 
{
    ...
}

And you can use @@ as escape for @.

Set OData Element Type

In default, OData element type is not contained in payload. If you want add element type, you sould use type parameter of Request factory method.

Request.Post("...", payload, type: "ODataDemo.Product")

Change OData Element Type Key

In default, OData element type key is odata.type. If you want change key to other, you should use typeKey parameter of Request factory method.

Request.Post("...", payload, type: "ODataDemo.Product", typeKey: "@odata.type")

Use for OData v4

If you use for ODatav4, you have to change serializer and parametalizer.

1. Global level settings
ODataClient.UseV4Global();
2. Client level settings

not yet. If you want, please create issue ticket.

Credential

Basic Auth

var odata = new ODataClient(client, "username", "password");

Custom

You should modify default settings of client which be passed to ODataClient constructor or implemet ICredentialBuilder

public class CustomCredentialBuilder : ODataHttpClient.Credentials.ICredentialBuilder
{
    public void Build(HttpClient client, HttpRequestMessage message)
    {
        // implement custom credential logic.
    }
}

And pass the builder instance to 2nd parameter constructor.

var odata = new ODataClient(client, new CustomCredentialBuilder());

Json Serializer Settings

In default, ODataHttpClient use OData V2 JSON Serialization Format.
If you change general json format, can select a way of three.

1. Global level settings

ODataHttpClient.Serializers.JsonSerializer.Default = ODataHttpClient.Serializers.JsonSerializer.General;

2. Instance level settings

var odata = new ODataHttpClient(httpClient, ODataHttpClient.Serializers.JsonSerializer.General);
var request = odata.RequestFacotry.Get("..."); // MUST use RequestFactory

3. Request level settings

var serializer = ODataHttpClient.Serializers.JsonSerializer.General;
var request = Request.Get("...", serializer); // pass serializer
var response = await odata.SendAsync(request);
var data = response.ReadAs<dynamic>(serializer); // pass serializer

NotFound(404)

In default, ODataHttpClient decide 404 response code to success. If you change to error, can select a way of followings.

1. Global level settings

ODataHttpClient.DefaultNotFoundIsSuccess = false;

2. Instance level settings

var odata = new ODataHttpClient(httpClient) { NotFoundIsSuccess = false };

3. Request level settings

var response = await odata.SendAsync(request, notfoundIsSuccess: false);
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.8.1 367 12/6/2022
1.8.0 2,555 7/26/2022
1.7.2-preview.1 93 7/12/2022
1.7.1 7,502 8/11/2021
1.7.0 6,232 11/11/2020
1.6.11 4,517 7/30/2020
1.6.10 465 7/21/2020
1.6.10-preview1 285 7/17/2020
1.6.9-preview1 498 4/21/2020
1.6.8 95,667 1/22/2020
1.6.7 1,660 11/1/2019
1.6.6 4,399 3/7/2019
1.6.5 3,659 11/20/2018
1.6.4 623 11/20/2018
1.6.3 801 11/13/2018
1.6.2 723 9/24/2018
1.6.1 643 9/23/2018
1.6.1-preview3 560 9/23/2018
1.6.0 1,139 6/26/2018
1.5.3 832 6/19/2018
1.5.2 1,153 6/8/2018
1.5.1 906 6/8/2018
1.5.0 846 6/7/2018
1.5.0-preview 649 6/7/2018
1.4.2 828 6/1/2018
1.4.1 883 6/1/2018
1.4.0 874 5/31/2018
1.3.0 846 4/17/2018
1.2.0 831 4/16/2018
1.1.0 881 4/16/2018
1.1.0-preview1 813 4/16/2018
1.0.0 835 4/15/2018
1.0.0-preview1 741 4/15/2018