ODataHttpClient 1.8.1

There is a newer version of this package available.
See the version list below for details.
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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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
2.0.2 2,400 7/16/2024
2.0.1 5,846 8/17/2023
2.0.0 388 5/30/2023
1.8.1 4,738 12/6/2022
1.8.0 8,906 7/26/2022
1.7.2-preview.1 157 7/12/2022
1.7.1 10,631 8/11/2021
1.7.0 11,016 11/11/2020
1.6.11 5,574 7/30/2020
1.6.10 569 7/21/2020
1.6.10-preview1 330 7/17/2020
1.6.9-preview1 559 4/21/2020
1.6.8 147,490 1/22/2020
1.6.7 1,805 11/1/2019
1.6.6 5,076 3/7/2019
1.6.5 4,650 11/20/2018
1.6.4 745 11/20/2018
1.6.3 911 11/13/2018
1.6.2 836 9/24/2018
1.6.1 770 9/23/2018
1.6.1-preview3 646 9/23/2018
1.6.0 1,264 6/26/2018
1.5.3 958 6/19/2018
1.5.2 1,293 6/8/2018
1.5.1 1,033 6/8/2018
1.5.0 979 6/7/2018
1.5.0-preview 738 6/7/2018
1.4.2 963 6/1/2018
1.4.1 1,008 6/1/2018
1.4.0 1,013 5/31/2018
1.3.0 957 4/17/2018
1.2.0 975 4/16/2018
1.1.0 1,014 4/16/2018
1.1.0-preview1 881 4/16/2018
1.0.0 960 4/15/2018
1.0.0-preview1 819 4/15/2018