Coder Social home page Coder Social logo

marcnet80 / odatahttpclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iwate/odatahttpclient

0.0 2.0 0.0 94 KB

The simplest implementation of OData client.

Home Page: https://iwate.github.io/ODataHttpClient/

License: MIT License

C# 100.00%

odatahttpclient's Introduction

CircleCI 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 reqest = 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 reqest = 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

odatahttpclient's People

Contributors

iwate avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.