6

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?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • As I understand, you want call another API on a different server. If that is the case than you should probably check the documentation of that API. If it is not OData I doubt that it will accept $filter, $select. – ErvinS Apr 13 '17 at 09:03
  • What exactly is the problem with your current approach?It's just a method call.How do you imagine something simpler? – George Vovos Apr 14 '17 at 17:56

1 Answers1

7

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.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79