Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map filter options from ODataQueryOptions to RestRequest

I need to be able to convert from ODataQueryOptions to RestRequest in order to be able to issue a RestRequest with specified filters, and have created the following helper class:

public static class ODataQueryFilterToRestClient
{
    public static RestRequest Map(ODataQueryOptions odataQuery)
    {
        var restRequest = new RestRequest();

        if (odataQuery.Filter != null)
        {
            restRequest.AddQueryParameter(@"$filter", odataQuery.Filter.RawValue);
        }

        if (odataQuery.Top != null)
        {
            restRequest.AddQueryParameter(@"$top", odataQuery.Top.RawValue);
        }

        if (odataQuery.Skip != null)
        {
            restRequest.AddQueryParameter(@"$skip", odataQuery.Skip.RawValue);
        }

        if (odataQuery.OrderBy != null)
        {
            restRequest.AddQueryParameter(@"$orderby", odataQuery.OrderBy.RawValue);
        }
        //etc
        return restRequest;
    }
}

Given that OdataQueryOptions supports the following:

enter image description here

Is there a simpler way to make the conversion between ODataQueryOptions to RestClient, or another rest client proxy?

On a side note, I don't know if there's a better way to accept parameters through a controller than ODataQueryOptions?

like image 669
Alex Gordon Avatar asked Apr 06 '17 21:04

Alex Gordon


1 Answers

There is no direct support of ODataQueryOptions in RestSharp.

There are other clients designed specifically for querying with OData, e.g. Simple.OData.Client. However it also doesn't operate with ODataQueryOptions for requests, providing fluent API.

Overall ODataQueryOptions is rather used on a server-side in OData compatible RESTful APIs. Clients (including RestSharp) just use their regular syntax to provide the data for request.

So answering your question (Is there a simpler way...) - No, there is not.

However your conversion method looks nice and pretty simple. If I had to make a call with RestSharp for given ODataQueryOptions, I'd do this exactly in the same way.

like image 57
CodeFuller Avatar answered Nov 18 '22 10:11

CodeFuller