The simplest implementation of OData client.
$ dotnet add package ODataHttpClient
PS> Install-Package ODataHttpClient
- Not support XML (JSON only)
- Not support Query Builder
var client = new HttpClient();
var odata = new ODataClient(client);
var request = Request.Get($"{endpoint}/Products?$count=true");
var response = await odata.SendAsync(request);
if (response.Success)
{
var total = response.ReadAs<int>("$['@odata.count']");
var products = response.ReadAs<IEnumerable<Product>>("$.value");
}
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/Categories/$ref", new Dictionay<string, string>
{
["@odata.id"] = $"{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");
}
var client = new HttpClient();
var odata = new ODataClient(client);
var request = Request.Get($"{endpoint}/Products?$filter=ReleaseDate ge @Date", new { Date = new DateTimeOffset(new DateTime(2000, 1, 1)) });
var response = await odata.SendAsync(request);
if (response.Success)
{
...
}
And you can use @@
as escape for @
.
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")
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")
If you use for OData v3(v2), you have to change serializer and parametalizer.
ODataClient.UseHistoricalGlobal();
not yet. If you want, please create issue ticket.
var odata = new ODataClient(client, "username", "password");
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());
In default, ODataHttpClient use general json format. If you change OData V2 JSON Serialization Format, can select a way of three.
ODataHttpClient.Serializers.JsonSerializer.Default = ODataHttpClient.Serializers.JsonSerializer.Historical;
var odata = new ODataHttpClient(httpClient, ODataHttpClient.Serializers.JsonSerializer.Historical);
var request = odata.RequestFacotry.Get("..."); // MUST use RequestFactory
var serializer = ODataHttpClient.Serializers.JsonSerializer.Historical;
var request = Request.Get("...", serializer); // pass serializer
var response = await odata.SendAsync(request);
var data = response.ReadAs<dynamic>(serializer); // pass serializer
In default, ODataHttpClient decide 404 response code of GET and HEAD request to success. If you change to error, can select a way of followings.
var request = Request.Get("...", acceptNotFound: false);
var response = await odata.SendAsync(request);
When a response code of other HTTP methods, like as POST,PUT,PATCH and DELETE, is 404, ODataHttpClient set Response.Success
false.